Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/apache/commons/lang3/math/Fraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public static Fraction getReducedFraction(int numerator, int denominator) {
numerator /= 2;
denominator /= 2;
}
// allow -2^31/2^k as a valid fraction (where k>0)
if (numerator == Integer.MIN_VALUE && (denominator & 1) == 0) {
numerator /= 2;
denominator /= 2;
}
if (denominator < 0) {
if (numerator == Integer.MIN_VALUE || denominator == Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: can't negate");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/apache/commons/lang3/math/FractionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1274,4 +1274,10 @@
f = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
assertEquals("-2147483648/2147483647", f.toString());
}
@Test
public void testMyReducedFractionFix() {
Fraction f = Fraction.getReducedFraction(Integer.MIN_VALUE, -2);

Check failure on line 1279 in src/test/java/org/apache/commons/lang3/math/FractionTest.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8, false)

(coding) FinalLocalVariable: Variable 'f' should be declared final.

Check failure on line 1279 in src/test/java/org/apache/commons/lang3/math/FractionTest.java

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8, false)

(coding) FinalLocalVariable: Variable 'f' should be declared final.

Check failure on line 1279 in src/test/java/org/apache/commons/lang3/math/FractionTest.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 11, false)

(coding) FinalLocalVariable: Variable 'f' should be declared final.

Check failure on line 1279 in src/test/java/org/apache/commons/lang3/math/FractionTest.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 21, false)

(coding) FinalLocalVariable: Variable 'f' should be declared final.

Check failure on line 1279 in src/test/java/org/apache/commons/lang3/math/FractionTest.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 26, false)

(coding) FinalLocalVariable: Variable 'f' should be declared final.
assertEquals(1073741824, f.getNumerator());
assertEquals(1, f.getDenominator());
}
}
Loading