Fix spurious overflow in Fraction.add/subtract for coprime denominators#1709
Merged
Conversation
garydgregory
left a comment
Member
There was a problem hiding this comment.
Hello @alhudz
Isn't there a way to do this using Java's own java.lang.Math.addExact(int, int) and java.lang.Math.subtractExact(int, int), and so on?
Contributor
Author
|
Makes sense, done. Used The one catch is the |
garydgregory
added a commit
that referenced
this pull request
Jun 17, 2026
Member
|
FYI I tested that the new tests fail without the main changes, merged 🚀 TY @alhudz |
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.
Thanks for your contribution to Apache Commons! Your help is appreciated!
Before you push a pull request, review this list:
mvn; that'smvnon the command line by itself.Fraction.add/subtractgo throughaddSub, whosed1 == 1fast path (coprime denominators) builds the result numerator withmulAndCheck(numerator, fraction.denominator)andmulAndCheck(fraction.numerator, denominator). Those cross products overflow aninteven when the reduced result is comfortably in range, soFraction.getFraction(Integer.MAX_VALUE, 2).add(Fraction.getFraction(-Integer.MAX_VALUE, 1))throwsArithmeticException: overflow: mul. Expected-2147483647/2, actual an exception. The siblingd1 != 1branch already computes the numerator in wider precision and only rejects the final reduced value.Compute the two cross products and their sum/difference in
long(both denominators are positive and below2^31, so the intermediate values cannot overflowlong) and reject only when the resulting numerator does not fit anint, matching the wider branch. Keeping it insideaddSubcoversadd,subtractanddivideBy. The two helpersaddAndCheck/subAndCheckwere only used by this branch and are removed. Regression cases added toFractionTestfail withoverflow: mulbefore the change.