commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r279983 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/math/Fraction.java test/org/apache/commons/lang/math/FractionTest.java
Date Sat, 10 Sep 2005 09:55:18 GMT
Author: scolebourne
Date: Sat Sep 10 02:55:12 2005
New Revision: 279983

URL: http://svn.apache.org/viewcvs?rev=279983&view=rev
Log:
Document and fix fraction reduction

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/Fraction.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/FractionTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/Fraction.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/Fraction.java?rev=279983&r1=279982&r2=279983&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/Fraction.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/Fraction.java
Sat Sep 10 02:55:12 2005
@@ -189,9 +189,12 @@
     }
 
     /**
-     * <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 @@
 
     /**
      * <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);
     }
 

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/FractionTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/FractionTest.java?rev=279983&r1=279982&r2=279983&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/FractionTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/FractionTest.java
Sat Sep 10 02:55:12 2005
@@ -615,14 +615,31 @@
         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() {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message