Skip to content

Instantly share code, notes, and snippets.

@IanConnolly
Created March 1, 2014 18:45
Show Gist options
  • Save IanConnolly/9295006 to your computer and use it in GitHub Desktop.
Save IanConnolly/9295006 to your computer and use it in GitHub Desktop.
MAX_32_UINT = 4294967295
MAX_32_SINT = 2147483648
MIN_32_SINT = -2147483647
def would_overflow_multiplication(in_type, a, b, sign=1):
"""
Checks if a * b would overflow
"""
if in_type is "deci":
if sign is 1:
if a > (MAX_32_SINT / b):
return True
else:
if abs(a) > abs(MIN_32_SINT) / abs(b):
return True
else:
if a > (MAX_32_UINT / b):
return True
return False
def would_overflow_addition(in_type, a, b, sign=1):
"""
Checks if a + b would overflow
"""
if in_type is "deci":
if sign is 1:
if a > (MAX_32_SINT - b):
return True
else:
if abs(a) > abs(MIN_32_SINT) - abs(b):
return True
else:
if a > (MAX_32_UINT - b):
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment