alhudz opened a new pull request, #1769: URL: https://github.com/apache/commons-lang/pull/1769
`Repro`: `Fraction.getFraction(-1, 46341).multiplyBy(Fraction.getFraction(100, 1000000))` throws `ArithmeticException: overflow: mulPos`, yet the same value with a reduced operand, `Fraction.getFraction(-1, 46341).multiplyBy(Fraction.getFraction(1, 10000))`, returns `-1/463410000`. `divideBy` and `pow` route through `multiplyBy` and behave the same way. `Cause`: the Knuth 4.5.1 cross-gcd (`d1 = gcd(numerator, fraction.denominator)`, `d2 = gcd(fraction.numerator, denominator)`) cancels the cross terms only and assumes both operands are already in lowest terms. A `Fraction` from `getFraction` is not reduced, so a factor shared inside an operand survives into the `mulAndCheck`/`mulPosAndCheck` product and overflows an `int` before `getReducedFraction` can cancel it, although the resulting numerator (`-1`) and denominator (`463410000`) both fit. The Javadoc only permits a throw when the resulting numerator or denominator exceeds `Integer.MAX_VALUE`. `Fix`: reduce both operands before the cross-gcd multiply. The product still passes through `getReducedFraction`, so the value is unchanged and the operation now overflows only when the reduced result genuinely exceeds `int`. `add`/`subtract` are untouched; they return unreduced results by design. - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project. - [ ] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI). - [ ] I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute? - [x] Run a successful build using the default [Maven](https://maven.apache.org/) 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
