Give me an algorithm for determining whether a 64-bit sized integer multiplication…
- User
Give me an algorithm for determining whether a 64-bit sized integer multiplication resulted in underflow or overflow using only the two operands and their (potentially incorrect) product.
Assistant
To determine whether a 64-bit integer multiplication resulted in overflow or underflow using only the two operands (
a,b) and their (potentially incorrect) product (p), you can use the following algorithm. This works for both signed and unsigned 64-bit integers, but the logic differs slightly between the two cases.Key Observations:
-
Overflow/Underflow in Unsigned Multiplication:
- For unsigned 64-bit integers, overflow occurs if the true product is ≥ (2^{64}).
- The true product can be checked by comparing
p / awithb(orp / bwitha). If they don't match, overflow occurred.
-
Overflow/Underflow in Signed Multiplication:
- For signed 64-bit integers, overflow occurs if the true product is outside the range ([-2^{63}, 2^{63} - 1]).
- The true product can be checked by comparing
p / awithb(orp / bwitha), but you must account for division truncation and signs.
Algorithm (Pseudocode):
Here’s a general approach that works for both signed and unsigned integers:
For Unsigned 64-bit Integers:
def has_unsigned_overflow(a: uint64, b: uint64, p: uint64) -> bool: if a == 0 or b == 0: return False # Product is 0, no overflow # Check if p / a == b (or p / b == a) # If not, overflow occurred return (p // a != b) and (p // b != a)For Signed 64-bit Integers:
def has_signed_overflow(a: int64, b: int64, p: int64) -> bool: if a == 0 or b == 0: return False # Product is 0, no overflow # Check if p / a == b (or p / b == a) # Need to handle division truncation and signs if a > 0: if b > 0: return p < 0 or (p // a != b) else: return p > 0 or (p // a != b) else: # a < 0 if b > 0: return p > 0 or (p // a != b) else: # b < 0 return p < 0 or (p // a != b)Explanation:
-
Unsigned Case:
- If
aorbis0, the product is0, so no overflow. - Otherwise, if
p // a != bandp // b != a, thenpis not the true product, so overflow occurred.
- If
-
Signed Case:
- If
aorbis0, the product is0, so no overflow. - Otherwise, check the signs of
aandb:- If
aandbare both positive, the product should be positive. Ifpis negative orp // a != b, overflow occurred. - If
ais positive andbis negative, the product should be negative. Ifpis positive orp // a != b, overflow occurred. - Similarly for other sign combinations.
- If
- If
Edge Cases:
- If
aorbis0, no overflow can occur. - If
aorbis-1and the other isINT64_MIN, the true product isINT64_MAX + 1, which overflows. The algorithm correctly detects this becausep(the incorrect product) will not satisfyp // a == b.
Example:
-
Unsigned Overflow:
a = 18446744073709551615(UINT64_MAX),b = 2,p = 18446744073709551614(incorrect due to overflow).p // a = 0 != b, so overflow is detected.
-
Signed Overflow:
a = -9223372036854775808(INT64_MIN),b = -1,p = -9223372036854775808(incorrect due to overflow).p // a = 1 != b, so overflow is detected.
This algorithm efficiently checks for overflow/underflow without needing additional bits or wider arithmetic.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.