Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/main/java/org/apache/commons/lang3/math/Fraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,10 @@ private Fraction addSub(final Fraction fraction, final boolean isAdd) {
Objects.requireNonNull(fraction, "fraction");
// zero is identity for addition.
if (numerator == 0) {
return isAdd ? fraction : fraction.negate();
return isAdd ? fraction.reduce() : fraction.reduce().negate();
}
if (fraction.numerator == 0) {
return this;
return reduce();
}
// Knuth 4.5.1 assumes operands in lowest terms and this class does not reduce on
// construction, so reduce both first, as multiplyBy does.
Expand Down
28 changes: 28 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 @@ -118,6 +118,34 @@ void testAddSubtractUnreducedOperands() {
assertThrows(ArithmeticException.class, () -> maxValue.add(maxValue));
}

@Test
void testAddSubtractZeroOperand() {
// A zero operand returns the other operand in reduced form.
Fraction f = Fraction.ZERO.add(Fraction.getFraction(2, 4));
assertEquals(1, f.getNumerator());
assertEquals(2, f.getDenominator());

f = Fraction.getFraction(2, 4).add(Fraction.ZERO);
assertEquals(1, f.getNumerator());
assertEquals(2, f.getDenominator());

f = Fraction.ZERO.subtract(Fraction.getFraction(2, 4));
assertEquals(-1, f.getNumerator());
assertEquals(2, f.getDenominator());

f = Fraction.getFraction(2, 4).subtract(Fraction.ZERO);
assertEquals(1, f.getNumerator());
assertEquals(2, f.getDenominator());

// Integer.MIN_VALUE/2 reduces to -1073741824/1, whose negation fits an int.
f = Fraction.ZERO.subtract(Fraction.getFraction(Integer.MIN_VALUE, 2));
assertEquals(1073741824, f.getNumerator());
assertEquals(1, f.getDenominator());

// Integer.MIN_VALUE/1 is in lowest terms and still cannot be negated.
assertThrows(ArithmeticException.class, () -> Fraction.ZERO.subtract(Fraction.getFraction(Integer.MIN_VALUE, 1)));
}

@Test
void testAdd() {
Fraction f;
Expand Down
Loading