arithmetic: companion code for the Add Two Numbers Without Overflow post
Silent int/long overflow reproduced with real numbers, the Math *Exact() family (addExact/subtractExact/multiplyExact/incrementExact/decrementExact/ negateExact/toIntExact/absExact) with the abs(MIN_VALUE) trap absExact exists to catch, widening to long vs BigInteger, and the XOR-based overflow bit trick Math.addExact uses internally, cross-checked against BigInteger ground truth over 100k random int pairs per JUnit repeat. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FhzLY5p6okFva3qsnsRyvM
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
$ java OverflowWrapDemo
|
||||
|
||||
=== int addition wraps silently past Integer.MAX_VALUE ===
|
||||
Integer.MAX_VALUE = 2147483647
|
||||
Integer.MAX_VALUE + 1 = -2147483648 (wrapped to Integer.MIN_VALUE, no exception)
|
||||
Integer.MIN_VALUE = -2147483648
|
||||
|
||||
=== A realistic case: summing item prices in cents overflows int ===
|
||||
priceCents * quantity = 1800000000 * 2 = -694967296 (should be 3,600,000,000 - this is wrong, and no exception was thrown)
|
||||
|
||||
=== Multiplication overflows just as silently as addition ===
|
||||
100000 * 100000 = 1410065408 (true value is 10,000,000,000)
|
||||
|
||||
=== long has the same problem, just a much higher ceiling ===
|
||||
Long.MAX_VALUE = 9223372036854775807
|
||||
Long.MAX_VALUE + 1 = -9223372036854775808 (wrapped to Long.MIN_VALUE)
|
||||
@@ -0,0 +1,24 @@
|
||||
$ java ExactArithmeticDemo
|
||||
|
||||
=== addExact / subtractExact / multiplyExact: normal case ===
|
||||
Math.addExact(20, 22) = 42
|
||||
Math.subtractExact(50, 8) = 42
|
||||
Math.multiplyExact(6, 7) = 42
|
||||
|
||||
=== addExact / multiplyExact: overflow case - throws instead of wrapping ===
|
||||
Math.addExact(Integer.MAX_VALUE, 1) threw: integer overflow
|
||||
Math.multiplyExact(100000, 100000) threw: integer overflow
|
||||
|
||||
=== incrementExact / decrementExact / negateExact ===
|
||||
Math.incrementExact(41) = 42
|
||||
Math.decrementExact(43) = 42
|
||||
Math.negateExact(-42) = 42
|
||||
Math.incrementExact(Integer.MAX_VALUE) threw: integer overflow
|
||||
|
||||
=== toIntExact: the safe narrowing cast from long back to int ===
|
||||
Math.toIntExact(2000000000L) = 2000000000
|
||||
Math.toIntExact(3000000000L) threw: integer overflow
|
||||
|
||||
=== The classic trap: Math.abs(Integer.MIN_VALUE) does NOT throw, and is wrong ===
|
||||
Math.abs(Integer.MIN_VALUE) = -2147483648 (still negative! +2147483648 doesn't fit in an int)
|
||||
Math.absExact(Integer.MIN_VALUE) threw: Overflow to represent absolute value of Integer.MIN_VALUE
|
||||
@@ -0,0 +1,13 @@
|
||||
$ java WideningAndBigIntegerDemo
|
||||
|
||||
=== Widening to long fixes the int-range overflow from OverflowWrapDemo ===
|
||||
int: Integer.MAX_VALUE + 1 = -2147483648 (wrong)
|
||||
long: (long) Integer.MAX_VALUE + 1 = 2147483648 (correct)
|
||||
|
||||
=== Widening only raises the ceiling - long still overflows eventually ===
|
||||
Long.MAX_VALUE + 1 = -9223372036854775808 (wrapped to Long.MIN_VALUE - same bug, bigger numbers)
|
||||
|
||||
=== BigInteger never overflows - it grows to fit the true value ===
|
||||
BigInteger.valueOf(Long.MAX_VALUE).add(ONE) = 9223372036854775808
|
||||
BigInteger.valueOf(2).pow(100) = 1267650600228229401496703205376
|
||||
(that value has no exact long or int representation at all)
|
||||
@@ -0,0 +1,15 @@
|
||||
$ java OverflowBitTrickDemo
|
||||
|
||||
=== Hand-rolled overflow detector vs. known cases ===
|
||||
2000000000 + 2000000000 -> raw int result -294967296, overflow detected: true
|
||||
2000000000 + -1000000000 -> raw int result 1000000000, overflow detected: false
|
||||
2147483647 + 1 -> raw int result -2147483648, overflow detected: true
|
||||
-2147483648 + -1 -> raw int result 2147483647, overflow detected: true
|
||||
100 + 200 -> raw int result 300, overflow detected: false
|
||||
|
||||
=== Cross-checked against Math.addExact() on the same inputs ===
|
||||
2000000000 + 2000000000: bit-trick says overflow=true, Math.addExact() agrees=true
|
||||
2000000000 + -1000000000: bit-trick says overflow=false, Math.addExact() agrees=true
|
||||
2147483647 + 1: bit-trick says overflow=true, Math.addExact() agrees=true
|
||||
-2147483648 + -1: bit-trick says overflow=true, Math.addExact() agrees=true
|
||||
100 + 200: bit-trick says overflow=false, Math.addExact() agrees=true
|
||||
@@ -0,0 +1,4 @@
|
||||
-------------------------------------------------------------------------------
|
||||
Test set: com.ankurm.arithmetic.ArithmeticClaimsTest
|
||||
-------------------------------------------------------------------------------
|
||||
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.438 s -- in com.ankurm.arithmetic.ArithmeticClaimsTest
|
||||
Reference in New Issue
Block a user