Repository navigation
fix: reduce ADTFraction to canonical form and avoid int overflow - #7801
Merged
DenizAltunkapan merged 2 commits intoDec 6, 2026
Merged
DenizAltunkapan merged 2 commits into
DenizAltunkapan merged 2 commits into
Conversation
…rithmetic The compact constructor only rejected a zero denominator, so mathematically equal fractions were unequal objects (1/2 and 2/4), a negative denominator printed as "1/-2", and plus() and times() multiplied the parts as ints without cancelling, which overflowed even when the reduced result was small. Divide both parts by their greatest common divisor and keep the sign in the numerator, so the denominator is always positive. Build sums from the least common multiple and products with cross cancellation in long, and raise an ArithmeticException only when the reduced result really does not fit into an int. Integer.MIN_VALUE parts are handled as long values, which keeps the sign change and the absolute value out of the int range. Add tests for reduction, sign normalization, equals/hashCode, the Integer.MIN_VALUE corner cases and for sums and products that overflow when the parts are multiplied without reduction. Signed-off-by: TAEHEON KIM <104629952+KTH1007@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7801 +/- ##
============================================
+ Coverage 82.99% 83.01% +0.01%
- Complexity 8689 8696 +7
============================================
Files 849 849
Lines 26549 26583 +34
Branches 5176 5179 +3
============================================
+ Hits 22035 22068 +33
Misses 3726 3726
- Partials 788 789 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
KTH1007
marked this pull request as ready for review
December 5, 2026 20:41
KTH1007
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
December 5, 2026 20:41
DenizAltunkapan
approved these changes
Dec 6, 2026
DenizAltunkapan
enabled auto-merge (squash)
December 6, 2026 09:17
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.
clang-format -i --style=file path/to/your/file.javaDescription
ADTFractionis a record, soequals()andhashCode()compare its two parts directly. The compact constructor only rejected a zero denominator and left the fraction in whatever shape the caller passed in. Equal fractions were therefore unequal objects, and a negative denominator produced values like1/-2.The arithmetic methods also multiplied the parts as
intbefore any reduction. Two fractions with large but cancelable parts wrapped around even when the reduced result fits into aninteasily.Changes
new ADTFraction(2, 4)now equalsnew ADTFraction(1, 2), andnew ADTFraction(1, -2)prints as-1/2.plus()adds the fractions over the least common multiple of the denominators instead of their product.times()cancels each numerator against the opposite denominator before multiplying.long. When a reduced result does not fit into anint, they throwArithmeticExceptioninstead of returning a wrapped value.Integer.MIN_VALUEare handled aslong, so a denominator ofInteger.MIN_VALUEis reduced and normalized whenever its canonical form is representable, and the constructor throwsArithmeticExceptionwhen it is not, for examplenew ADTFraction(1, Integer.MIN_VALUE).reciprocal()goes through the same path, so the reciprocal of zero still raisesIllegalArgumentExceptionand a reciprocal that cannot be represented raisesArithmeticException.Tests
ADTFractionTestgrew from 7 to 30 tests. The new tests cover reduction, sign normalization, equality and hash code of equivalent fractions,Integer.MIN_VALUEparts, sums and products with reducible parts, and the cases where the intermediate product or the final result really is too large for anint.Run against the previous implementation, the new test class reports 19 failures and 1 error. With the change all 30 tests pass.
Fixes #7764