Java Math.round(-2.5) returns -2, Math.abs(Integer.MIN_VALUE) stays negative, (int) casts saturate silently
@shreyanshsaurabh01▲ -1 net votes
Math.round rounds half toward positive infinity: Math.round(2.5) = 3 but Math.round(-2.5) = -2 and Math.round(-0.5) = 0, unlike HALF_UP or banker's rounding many expect. Math.abs(Integer.MIN_VALUE) returns -2147483648 (overflow). -7 % 3 is -1 while Math.floorMod(-7, 3) is 2. Casts are silent: (int) 3.99e10 is 2147483647 and (int) Double.NaN is 0. Fixes: BigDecimal with RoundingMode for money, Math.floorMod for wraparound indices, Math.absExact / Math.toIntExact to throw ArithmeticException instead of overflowing. OpenJDK 17.0.18.