commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r906251 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/fraction/BigFraction.java site/xdoc/changes.xml test/java/org/apache/commons/math/fraction/BigFractionTest.java
Date Wed, 03 Feb 2010 21:19:55 GMT
Author: luc
Date: Wed Feb  3 21:19:54 2010
New Revision: 906251

URL: http://svn.apache.org/viewvc?rev=906251&view=rev
Log:
Fixed an error in BigFraction multiplication for large numerators that don't fit in a primitive
int.

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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java?rev=906251&r1=906250&r2=906251&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java
Wed Feb  3 21:19:54 2010
@@ -900,13 +900,12 @@
      *             if the fraction is <code>null</code>.
      */
     public BigFraction multiply(final BigFraction fraction) {
-        BigFraction ret = ZERO;
-
-        if (getNumeratorAsInt() != 0 && fraction.getNumeratorAsInt() != 0) {
-            ret = new BigFraction(numerator.multiply(fraction.numerator), denominator.multiply(fraction.denominator));
+        if (numerator.equals(BigInteger.ZERO) ||
+            fraction.numerator.equals(BigInteger.ZERO)) {
+            return ZERO;
         }
-
-        return ret;
+        return new BigFraction(numerator.multiply(fraction.numerator),
+                               denominator.multiply(fraction.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=906251&r1=906250&r2=906251&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Feb  3 21:19:54 2010
@@ -39,6 +39,10 @@
   </properties>
   <body>
     <release version="2.1" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-340" >
+        Fixed an error in BigFraction multiplication for large numerators that don't
+        fit in a primitive int.
+      </action>
       <action dev="luc" type="fix" >
         Fixed a spurious exception in EigenDecompositionImpl when a 3x3 block
         had two identical eigenvalues.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java?rev=906251&r1=906250&r2=906251&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java
Wed Feb  3 21:19:54 2010
@@ -551,6 +551,15 @@
         assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l)));
     }
 
+    public void testMath340() {
+        BigFraction fractionA = new BigFraction(0.00131);
+        BigFraction fractionB = new BigFraction(.37).reciprocal();
+        BigFraction errorResult = fractionA.multiply(fractionB);
+        BigFraction correctResult = new BigFraction(fractionA.getNumerator().multiply(fractionB.getNumerator()),
+                                                    fractionA.getDenominator().multiply(fractionB.getDenominator()));
+        assertEquals(correctResult, errorResult);
+    }
+
     public void testSerial() throws FractionConversionException {
         BigFraction[] fractions = {
             new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO,



Mime
View raw message