You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/04/01 14:15:31 UTC

svn commit: r760867 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/fraction/BigFraction.java java/org/apache/commons/math/fraction/Fraction.java site/xdoc/changes.xml test/org/apache/commons/math/fraction/FractionTest.java

Author: luc
Date: Wed Apr  1 12:15:29 2009
New Revision: 760867

URL: http://svn.apache.org/viewvc?rev=760867&view=rev
Log:
Added  add/subtract/multiply/divide functions with integer parameters to Fraction

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/BigFraction.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/BigFraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/BigFraction.java?rev=760867&r1=760866&r2=760867&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/BigFraction.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/BigFraction.java Wed Apr  1 12:15:29 2009
@@ -31,11 +31,17 @@
  */
 public class BigFraction extends Number implements Comparable<BigFraction> {
 
+    /** A fraction representing "2 / 1". */
+    public static final BigFraction TWO = new BigFraction(2);
+
     /** A fraction representing "1". */
-    public static final BigFraction ONE = new BigFraction(1, 1);
+    public static final BigFraction ONE = new BigFraction(1);
 
     /** A fraction representing "0". */
-    public static final BigFraction ZERO = new BigFraction(0, 1);
+    public static final BigFraction ZERO = new BigFraction(0);
+
+    /** A fraction representing "-1 / 1". */
+    public static final BigFraction MINUS_ONE = new BigFraction(-1);
 
     /** A fraction representing "4/5". */
     public static final BigFraction FOUR_FIFTHS = new BigFraction(4, 5);
@@ -67,11 +73,8 @@
     /** A fraction representing "2/3". */
     public static final BigFraction TWO_THIRDS = new BigFraction(2, 3);
 
-    /** A fraction representing "-1 / 1". */
-    public static final BigFraction MINUS_ONE = new BigFraction(-1, 1);
-
     /** Serializable version identifier. */
-    private static final long serialVersionUID = -5984892138972589598L;
+    private static final long serialVersionUID = -130662482360701382L;
 
     /** <code>BigInteger</code> representation of 100. */
     private static final BigInteger ONE_HUNDRED_DOUBLE = BigInteger.valueOf(100);

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java?rev=760867&r1=760866&r2=760867&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/Fraction.java Wed Apr  1 12:15:29 2009
@@ -32,12 +32,42 @@
     /** A fraction representing "2 / 1". */
     public static final Fraction TWO = new Fraction(2, 1);
 
-    /** A fraction representing "1 / 1". */
+    /** A fraction representing "1". */
     public static final Fraction ONE = new Fraction(1, 1);
 
-    /** A fraction representing "0 / 1". */
+    /** A fraction representing "0". */
     public static final Fraction ZERO = new Fraction(0, 1);
 
+    /** A fraction representing "4/5". */
+    public static final Fraction FOUR_FIFTHS = new Fraction(4, 5);
+
+    /** A fraction representing "1/5". */
+    public static final Fraction ONE_FIFTH = new Fraction(1, 5);
+
+    /** A fraction representing "1/2". */
+    public static final Fraction ONE_HALF = new Fraction(1, 2);
+
+    /** A fraction representing "1/4". */
+    public static final Fraction ONE_QUARTER = new Fraction(1, 4);
+
+    /** A fraction representing "1/3". */
+    public static final Fraction ONE_THIRD = new Fraction(1, 3);
+
+    /** A fraction representing "3/5". */
+    public static final Fraction THREE_FIFTHS = new Fraction(3, 5);
+
+    /** A fraction representing "3/4". */
+    public static final Fraction THREE_QUARTERS = new Fraction(3, 4);
+
+    /** A fraction representing "4/5". */
+    public static final Fraction TWO_FIFTHS = new Fraction(4, 5);
+
+    /** A fraction representing "2/4". */
+    public static final Fraction TWO_QUARTERS = new Fraction(2, 4);
+
+    /** A fraction representing "2/3". */
+    public static final Fraction TWO_THIRDS = new Fraction(2, 3);
+
     /** A fraction representing "-1 / 1". */
     public static final Fraction MINUS_ONE = new Fraction(-1, 1);
 
@@ -199,6 +229,15 @@
     }
     
     /**
+     * Create a fraction from an int. 
+     * The fraction is num / 1.
+     * @param num the numerator.
+     */
+    public Fraction(int num) {
+        this(num, 1);
+    }
+    
+    /**
      * Create a fraction given the numerator and denominator.  The fraction is
      * reduced to lowest terms.
      * @param num the numerator.
@@ -206,7 +245,6 @@
      * @throws ArithmeticException if the denominator is <code>zero</code>
      */
     public Fraction(int num, int den) {
-        super();
         if (den == 0) {
             throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
                                                                  num, den);
@@ -220,7 +258,7 @@
             den = -den;
         }
         // reduce numerator and denominator by greatest common denominator.
-        int d = MathUtils.gcd(num, den);
+        final int d = MathUtils.gcd(num, den);
         if (d > 1) {
             num /= d;
             den /= d;
@@ -228,10 +266,10 @@
         
         // move sign to numerator.
         if (den < 0) {
-            num *= -1;
-            den *= -1;
+            num = -num;
+            den = -den;
         }
-        this.numerator = num;
+        this.numerator   = num;
         this.denominator = den;
     }
     
@@ -260,7 +298,7 @@
         long dOn = ((long) denominator) * object.numerator;
         return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0);
     }
-    
+
     /**
      * Gets the fraction as a <tt>double</tt>. This calculates the fraction as
      * the numerator divided by denominator.
@@ -388,6 +426,15 @@
     }
 
     /**
+     * Add an integer to the fraction.
+     * @param i the <tt>integer</tt> to add.
+     * @return this + i
+     */
+    public Fraction add(final int i) {
+        return new Fraction(numerator + i * denominator, denominator);
+    }
+
+    /**
      * <p>Subtracts the value of another fraction from the value of this one, 
      * returning the result in reduced form.</p>
      *
@@ -401,6 +448,15 @@
         return addSub(fraction, false /* subtract */);
     }
 
+    /**
+     * Subtract an integer from the fraction.
+     * @param i the <tt>integer</tt> to subtract.
+     * @return this - i
+     */
+    public Fraction subtract(final int i) {
+        return new Fraction(numerator - i * denominator, denominator);
+    }
+
     /** 
      * Implement add and subtract using algorithm described in Knuth 4.5.1.
      * 
@@ -485,6 +541,15 @@
     }
 
     /**
+     * Multiply the fraction by an integer.
+     * @param i the <tt>integer</tt> to multiply by.
+     * @return this * i
+     */
+    public Fraction multiply(final int i) {
+        return new Fraction(numerator * i, denominator);
+    }
+
+    /**
      * <p>Divide the value of this fraction by another.</p>
      *
      * @param fraction  the fraction to divide by, must not be <code>null</code>
@@ -505,7 +570,16 @@
         }
         return multiply(fraction.reciprocal());
     }
-    
+
+    /**
+     * Divide the fraction by an integer.
+     * @param i the <tt>integer</tt> to divide by.
+     * @return this * i
+     */
+    public Fraction divide(final int i) {
+        return new Fraction(numerator, denominator * i);
+    }
+
     /**
      * <p>Creates a <code>Fraction</code> instance with the 2 parts
      * of a fraction Y/Z.</p>
@@ -546,4 +620,5 @@
         denominator /= gcd;
         return new Fraction(numerator, denominator);
     }
+
 }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=760867&r1=760866&r2=760867&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Apr  1 12:15:29 2009
@@ -40,6 +40,9 @@
   <body>
     <release version="2.0" date="TBD" description="TBD">
       <action dev="luc" type="add">
+        Added  add/subtract/multiply/divide functions with integer parameters to Fraction
+      </action>
+      <action dev="luc" type="add">
         Added some utility functions to compute powers with integral types (int, long, BigInteger).
       </action>
       <action dev="luc" type="fix" issue="MATH-252">

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java?rev=760867&r1=760866&r2=760867&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java Wed Apr  1 12:15:29 2009
@@ -308,6 +308,9 @@
         Fraction f = f1.add(f2);
         assertEquals(Integer.MAX_VALUE, f.getNumerator());
         assertEquals(1, f.getDenominator());
+        f = f1.add(1);
+        assertEquals(Integer.MAX_VALUE, f.getNumerator());
+        assertEquals(1, f.getDenominator());
         
         f1 = new Fraction(-1, 13*13*2*2);
         f2 = new Fraction(-2, 13*17*2);
@@ -426,6 +429,12 @@
             f = f1.divide(f1.reciprocal());  // should overflow
             fail("expecting ArithmeticException");
         } catch (ArithmeticException ex) {}
+
+        f1 = new Fraction(6, 35);
+        f  = f1.divide(15);
+        assertEquals(2, f.getNumerator());
+        assertEquals(175, f.getDenominator());
+
     }
     
     public void testMultiply() {
@@ -447,6 +456,11 @@
             f.multiply(null);
             fail("expecting IllegalArgumentException");
         } catch (IllegalArgumentException ex) {}
+
+        f1 = new Fraction(6, 35);
+        f  = f1.multiply(15);
+        assertEquals(18, f.getNumerator());
+        assertEquals(7, f.getDenominator());
     }
     
     public void testSubtract() {
@@ -483,6 +497,9 @@
         f = f1.subtract(f2);
         assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
         assertEquals(1, f.getDenominator());
+        f = f1.subtract(1);
+        assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
+        assertEquals(1, f.getDenominator());
 
         try {
             f1 = new Fraction(1, Integer.MAX_VALUE);