Created
March 1, 2014 18:45
-
-
Save IanConnolly/9295006 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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