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:
Stephen Colebourne 2005-09-10 09:55:12 +00:00
parent bf54dcd449
commit b0082bc606
2 changed files with 35 additions and 9 deletions

View File

@ -189,9 +189,12 @@ public static Fraction getFraction(int whole, int numerator, int denominator) {
} }
/** /**
* <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> * 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> * <p>Any negative signs are resolved to be on the numerator.</p>
* *
* @param numerator the numerator, for example the three in 'three sevenths' * @param numerator the numerator, for example the three in 'three sevenths'
@ -448,12 +451,18 @@ public double doubleValue() {
/** /**
* <p>Reduce the fraction to the smallest values for the numerator and * <p>Reduce the fraction to the smallest values for the numerator and
* denominator, returning the result..</p> * denominator, returning the result.</p>
* *
* @return a new reduce fraction instance, or this if no simplification possible * <p>For example, if this fraction represents 2/4, then the result
* will be 1/2.</p>
*
* @return a new reduced fraction instance, or this if no simplification possible
*/ */
public Fraction reduce() { public Fraction reduce() {
int gcd = greatestCommonDivisor(Math.abs(numerator), denominator); int gcd = greatestCommonDivisor(Math.abs(numerator), denominator);
if (gcd == 1) {
return this;
}
return Fraction.getFraction(numerator / gcd, denominator / gcd); return Fraction.getFraction(numerator / gcd, denominator / gcd);
} }

View File

@ -615,14 +615,31 @@ public void testReduce() {
Fraction f = null; Fraction f = null;
f = Fraction.getFraction(50, 75); f = Fraction.getFraction(50, 75);
f = f.reduce(); Fraction result = f.reduce();
assertEquals(2, f.getNumerator()); assertEquals(2, result.getNumerator());
assertEquals(3, f.getDenominator()); 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 = Fraction.getFraction(2, 3);
f = f.reduce(); result = f.reduce();
assertEquals(2, f.getNumerator()); assertEquals(2, result.getNumerator());
assertEquals(3, f.getDenominator()); assertEquals(3, result.getDenominator());
assertSame(f, result);
} }
public void testInvert() { public void testInvert() {