Document and fix fraction reduction
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@279983 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bf54dcd449
commit
b0082bc606
|
@ -189,9 +189,12 @@ public final class Fraction extends Number implements Serializable, Comparable {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a <code>Fraction</code> instance with the 2 parts
|
||||
* <p>Creates a reduced <code>Fraction</code> instance with the 2 parts
|
||||
* of a fraction Y/Z.</p>
|
||||
*
|
||||
* <p>For example, if the input parameters represent 2/4, then the created
|
||||
* fraction will be 1/2.</p>
|
||||
*
|
||||
* <p>Any negative signs are resolved to be on the numerator.</p>
|
||||
*
|
||||
* @param numerator the numerator, for example the three in 'three sevenths'
|
||||
|
@ -448,12 +451,18 @@ public final class Fraction extends Number implements Serializable, Comparable {
|
|||
|
||||
/**
|
||||
* <p>Reduce the fraction to the smallest values for the numerator and
|
||||
* denominator, returning the result..</p>
|
||||
* denominator, returning the result.</p>
|
||||
*
|
||||
* <p>For example, if this fraction represents 2/4, then the result
|
||||
* will be 1/2.</p>
|
||||
*
|
||||
* @return a new reduce fraction instance, or this if no simplification possible
|
||||
* @return a new reduced fraction instance, or this if no simplification possible
|
||||
*/
|
||||
public Fraction reduce() {
|
||||
int gcd = greatestCommonDivisor(Math.abs(numerator), denominator);
|
||||
if (gcd == 1) {
|
||||
return this;
|
||||
}
|
||||
return Fraction.getFraction(numerator / gcd, denominator / gcd);
|
||||
}
|
||||
|
||||
|
|
|
@ -615,14 +615,31 @@ public class FractionTest extends TestCase {
|
|||
Fraction f = null;
|
||||
|
||||
f = Fraction.getFraction(50, 75);
|
||||
f = f.reduce();
|
||||
assertEquals(2, f.getNumerator());
|
||||
assertEquals(3, f.getDenominator());
|
||||
Fraction result = f.reduce();
|
||||
assertEquals(2, result.getNumerator());
|
||||
assertEquals(3, result.getDenominator());
|
||||
|
||||
f = Fraction.getFraction(-2, -3);
|
||||
result = f.reduce();
|
||||
assertEquals(2, result.getNumerator());
|
||||
assertEquals(3, result.getDenominator());
|
||||
|
||||
f = Fraction.getFraction(2, -3);
|
||||
result = f.reduce();
|
||||
assertEquals(-2, result.getNumerator());
|
||||
assertEquals(3, result.getDenominator());
|
||||
|
||||
f = Fraction.getFraction(-2, 3);
|
||||
result = f.reduce();
|
||||
assertEquals(-2, result.getNumerator());
|
||||
assertEquals(3, result.getDenominator());
|
||||
assertSame(f, result);
|
||||
|
||||
f = Fraction.getFraction(2, 3);
|
||||
f = f.reduce();
|
||||
assertEquals(2, f.getNumerator());
|
||||
assertEquals(3, f.getDenominator());
|
||||
result = f.reduce();
|
||||
assertEquals(2, result.getNumerator());
|
||||
assertEquals(3, result.getDenominator());
|
||||
assertSame(f, result);
|
||||
}
|
||||
|
||||
public void testInvert() {
|
||||
|
|
Loading…
Reference in New Issue