Repository navigation
Conversation
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.
aherbert
requested changes
Nov 4, 2026
| // twice to round the quotient to even, as in fdlibm __ieee754_remainder. | ||
| // All steps are exact: x + x only overflows when x + x > p holds anyway. | ||
| final double p = Math.abs(divisor); | ||
| double x = Math.abs(dividend) % (p + p); |
Contributor
There was a problem hiding this comment.
Can this use 2*p instead of p+p
Contributor
Author
There was a problem hiding this comment.
Yes, it's the same value. Doubling a double is exact, and 2 * p overflows to infinity for exactly the same p as p + p, so the overflow path where % leaves x unchanged behaves as before. Switched in 319201c and AccurateMathTest still passes. The two x + x comparisons below are the same case if you'd prefer those as 2 * x as well.
Contributor
There was a problem hiding this comment.
Please change all cases of (x + x) to 2 * x. Thanks.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With the default
JdkMathbackend (AccurateMath), on master:The general case evaluates
dividend - divisor * rint(dividend / divisor)in doubles. Past 2^53 the rounded quotient is not the nearest integer,divisor * nis itself rounded so the error scales with the dividend rather than the remainder, and a large quotient overflows the product. Even with |x/y| <= 1000, 974017 of 1000000 random pairs differ fromStrictMath.DerivativeStructure,SparseGradientandDSCompilerinherit this throughJdkMath.The fix reduces |dividend| modulo 2|divisor| with
%, which is exact for doubles, then subtracts |divisor| at most twice to round the quotient to even, as fdlibm's__ieee754_remainderdoes. Every step is exact and the method stays pure Java. It matchesStrictMath.IEEEremainderbit for bit on 100M pairs covering random bit patterns, subnormal divisors, ties and the overflow edges, on JDK 21 and 27. The javadoc bullet claiming the result always has the dividend's sign now applies to a zero result only, since IEEEremainder(3, 2) is -1.testIEEEremaindercompares against an exact BigDecimal reference; on master:mvn clean verifyis green across the reactor on JDK 21.