This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 355f24676 Fix premature overflow in Fraction.divideBy (#1865)
355f24676 is described below
commit 355f246760cad21ca597424b49bb1151c0a63a96
Author: Amar Ghugare <[email protected]>
AuthorDate: Sun Nov 15 17:53:02 2026 +0530
Fix premature overflow in Fraction.divideBy (#1865)
Compute and reduce the quotient using long products before checking int
limits. This avoids constructing an unrepresentable reciprocal when the
final quotient is representable, including Integer.MIN_VALUE divisors.
Add regression coverage for cancellation, zero dividends, signs and
genuine overflow while preserving null and zero-divisor checks.
---
.../org/apache/commons/lang3/math/Fraction.java | 19 ++++++++-
.../apache/commons/lang3/math/FractionTest.java | 46 ++++++++++++++++++++++
2 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/lang3/math/Fraction.java
b/src/main/java/org/apache/commons/lang3/math/Fraction.java
index 7a8807074..0d7ebbe40 100644
--- a/src/main/java/org/apache/commons/lang3/math/Fraction.java
+++ b/src/main/java/org/apache/commons/lang3/math/Fraction.java
@@ -632,7 +632,24 @@ public Fraction divideBy(final Fraction fraction) {
if (fraction.numerator == 0) {
throw new ArithmeticException("The fraction to divide by must not
be zero");
}
- return multiplyBy(fraction.invert());
+ if (numerator == 0) {
+ return ZERO;
+ }
+ // Products of int values fit in a long. Reduce before narrowing to
avoid overflow in an intermediate reciprocal.
+ long resultNumerator = (long) numerator * fraction.denominator;
+ long resultDenominator = (long) denominator * fraction.numerator;
+ if (resultDenominator < 0) {
+ resultNumerator = -resultNumerator;
+ resultDenominator = -resultDenominator;
+ }
+ long gcd = Math.abs(resultNumerator);
+ long remainder = resultDenominator;
+ while (remainder != 0) {
+ final long next = gcd % remainder;
+ gcd = remainder;
+ remainder = next;
+ }
+ return new Fraction(Math.toIntExact(resultNumerator / gcd),
Math.toIntExact(resultDenominator / gcd));
}
/**
diff --git a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
index dca860ea7..e9c917623 100644
--- a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
@@ -396,6 +396,52 @@ void testDivide() {
assertEquals(463410000, f.getDenominator());
}
+ @ParameterizedTest
+ @CsvSource({
+ "-2147483648, 1, -2147483648, 1, 1, 1",
+ "-2147483648, 1, -2147483648, 2147483647, 2147483647, 1",
+ "-2147483648, 2147483647, -2147483648, 2147483647, 1, 1",
+ "-2147483648, 2, -2147483648, 4, 2, 1",
+ "-2147483648, 3, -2147483648, 5, 5, 3",
+ "-2147483648, 6, -2147483648, 10, 5, 3",
+ "-1, 1, -2147483648, 2, 1, 1073741824",
+ "1, 1, -2147483648, 2, -1, 1073741824",
+ "2, 1, -2147483648, 1, -1, 1073741824",
+ "-2147483648, 2, -1, 1, 1073741824, 1",
+ "-2147483648, 1, 1, 1, -2147483648, 1"
+ })
+ void testDivideIntegerMinValue(final int numerator, final int denominator,
final int divisorNumerator, final int divisorDenominator,
+ final int expectedNumerator, final int expectedDenominator) {
+ final Fraction quotient = Fraction.getFraction(numerator,
denominator).divideBy(Fraction.getFraction(divisorNumerator,
divisorDenominator));
+ assertEquals(expectedNumerator, quotient.getNumerator());
+ assertEquals(expectedDenominator, quotient.getDenominator());
+ }
+
+ @ParameterizedTest
+ @CsvSource({
+ "1, 1, -2147483648, 1",
+ "-1, 1, -2147483648, 1",
+ "-2147483648, 1, -1, 1",
+ "-2147483648, 1, 1, 2",
+ "1, 2147483647, 2147483647, 1",
+ "2147483647, 1, 1, 2147483647",
+ "2, 3, -2147483648, 1"
+ })
+ void testDivideOverflow(final int numerator, final int denominator, final
int divisorNumerator, final int divisorDenominator) {
+ final Fraction dividend = Fraction.getFraction(numerator, denominator);
+ final Fraction divisor = Fraction.getFraction(divisorNumerator,
divisorDenominator);
+ assertThrows(ArithmeticException.class, () ->
dividend.divideBy(divisor));
+ }
+
+ @Test
+ void testDivideZeroByIntegerMinValue() {
+ final Fraction divisor = Fraction.getFraction(Integer.MIN_VALUE, 1);
+ assertSame(Fraction.ZERO, Fraction.ZERO.divideBy(divisor));
+ assertSame(Fraction.ZERO, Fraction.getFraction(0,
5).divideBy(divisor));
+ assertNullPointerException(() -> Fraction.ZERO.divideBy(null));
+ assertThrows(ArithmeticException.class, () ->
Fraction.ZERO.divideBy(Fraction.ZERO));
+ }
+
@Test
void testEquals() {
Fraction f1;