From cac545754e2d492fa3547e6a904b832f96f3c957 Mon Sep 17 00:00:00 2001 From: Mahammad Insaf Date: Tue, 3 Nov 2026 21:07:54 +0530 Subject: [PATCH 1/2] fix overflow and precision loss in AccurateMath.IEEEremainder The general case computed dividend - divisor * rint(dividend / divisor) in double arithmetic. Once the quotient passes 2^53 rint no longer sees the nearest integer, divisor * n is itself rounded, and for large quotients the product overflows, so IEEEremainder(Double.MAX_VALUE, 1.5) returned -Infinity. Reduce |dividend| modulo 2 |divisor| with the exact % operator and subtract |divisor| at most twice to round the quotient to even, as fdlibm does. The result now matches StrictMath.IEEEremainder. --- .../math4/core/jdkmath/AccurateMath.java | 19 +++++-- .../legacy/core/jdkmath/AccurateMathTest.java | 53 +++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java index bcbff70ad..09eee2861 100644 --- a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java +++ b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java @@ -3691,7 +3691,7 @@ public static double hypot(final double x, final double y) { * {@code n} is the integer that is even. *