Skip to content

Fix LANG-1834: getReducedFraction throws for reducible Integer.MIN_VALUE numerator - #1790

Open
aakarsh-34 wants to merge 1 commit into
apache:masterfrom
aakarsh-34:fix-lang-1834
Open

aakarsh-34 wants to merge 1 commit into
apache:masterfrom
aakarsh-34:fix-lang-1834

Conversation

@aakarsh-34

Copy link
Copy Markdown

…LUE numerator

Fixes LANG-1834.

Fraction.getReducedFraction(Integer.MIN_VALUE, -2) previously threw an
ArithmeticException, even though the fraction can be safely reduced to
1073741824/1 before sign normalization.

This adds a special case for when the numerator is Integer.MIN_VALUE and
the denominator is even, mirroring the existing special case already
handled for the denominator.

Added a test verifying the fix.

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • [x ] Read the contribution guidelines for this project.
  • [ x] Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • [x ] I used AI to create part of this pull request. I used Claude to help me understand the existing code, work through the logic for the fix, and write the test case.
  • [ x] Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • [x ] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • [x ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • [ x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@garydgregory

Copy link
Copy Markdown
Member

@aakarsh-34
Don't bother pushing until you have a successful local build. Read the PR template please.

@garydgregory

Copy link
Copy Markdown
Member

Hi @aakarsh-34

Here are some notes based on local testing:

  • Test coverage: The new test correctly reproduces the reported bug and checks both result components. It does not cover the full boundary behavior. Add cases for MIN_VALUE/-6, MIN_VALUE/MIN_VALUE, and retained exceptions for MIN_VALUE/-1 and MIN_VALUE/-3. The existing suite already covers MIN_VALUE/2, exercising the new branch with a positive denominator.
  • Comment accuracy: The new comment describes a power-of-two denominator, but the condition accepts any even denominator. Prefer: // Halve both operands before sign normalization to avoid negating Integer.MIN_VALUE. The neighboring existing comment has a similar imprecision.
  • Test conventions: Rename testMyReducedFractionFix to something descriptive, such as testReducedFactory_minValueNumeratorNegativeEvenDenominator. Match neighboring tests by removing public, inserting a blank line before @Test, and placing it beside the reduced-factory tests.

@garydgregory

Copy link
Copy Markdown
Member

@aakarsh-34 ping 🔔

1 similar comment
@garydgregory

Copy link
Copy Markdown
Member

@aakarsh-34 ping 🔔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants