You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2012/02/14 14:16:57 UTC

svn commit: r1243912 - 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

Author: erans
Date: Tue Feb 14 13:16:57 2012
New Revision: 1243912

URL: http://svn.apache.org/viewvc?rev=1243912&view=rev
Log:
MATH-744
Fixed "doubleValue()" and "floatValue()" when numerator and denominator are
larger than the range of the corresponding primitive type.

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=1243912&r1=1243911&r2=1243912&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 Tue Feb 14 13:16:57 2012
@@ -682,7 +682,16 @@ public class BigFraction
      */
     @Override
     public double doubleValue() {
-        return numerator.doubleValue() / denominator.doubleValue();
+        double result = numerator.doubleValue() / denominator.doubleValue();
+        if (Double.isNaN(result)) {
+            // Numerator and/or denominator must be out of range:
+            // Calculate how far to shift them to put them in range.
+            int shift = Math.max(numerator.bitLength(),
+                                 denominator.bitLength()) - Double.MAX_EXPONENT;
+            result = numerator.shiftRight(shift).doubleValue() /
+                denominator.shiftRight(shift).doubleValue();
+        }
+        return result;
     }
 
     /**
@@ -726,7 +735,16 @@ public class BigFraction
      */
     @Override
     public float floatValue() {
-        return numerator.floatValue() / denominator.floatValue();
+        float result = numerator.floatValue() / denominator.floatValue();
+        if (Double.isNaN(result)) {
+            // Numerator and/or denominator must be out of range:
+            // Calculate how far to shift them to put them in range.
+            int shift = Math.max(numerator.bitLength(),
+                                 denominator.bitLength()) - Float.MAX_EXPONENT;
+            result = numerator.shiftRight(shift).floatValue() /
+                denominator.shiftRight(shift).floatValue();
+        }
+        return result;
     }
 
     /**

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=1243912&r1=1243911&r2=1243912&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Tue Feb 14 13:16:57 2012
@@ -52,6 +52,11 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="erans" type="fix" issue="MATH-744" due-to="Thundre">
+        Fixed "doubleValue" and "floatValue" method in "BigFraction" when
+        numerator and denominator are larger than the range of the
+        corresponding primitive type.
+      </action>
       <action dev="erans" type="update" issue="MATH-488">
         Removed "MathException" (from package "o.a.c.math").
       </action>

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=1243912&r1=1243911&r2=1243912&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 Tue Feb 14 13:16:57 2012
@@ -198,6 +198,30 @@ public class BigFractionTest {
         Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
     }
 
+    // MATH-744
+    @Test
+    public void testDoubleValueForLargeNumeratorAndDenominator() {
+        final BigInteger pow400 = BigInteger.TEN.pow(400);
+        final BigInteger pow401 = BigInteger.TEN.pow(401);
+        final BigInteger two = new BigInteger("2");
+        final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE),
+                                                  pow400.multiply(two));
+
+        Assert.assertEquals(5, large.doubleValue(), 1e-15);
+    }
+
+    // MATH-744
+    @Test
+    public void testFloatValueForLargeNumeratorAndDenominator() {
+        final BigInteger pow400 = BigInteger.TEN.pow(400);
+        final BigInteger pow401 = BigInteger.TEN.pow(401);
+        final BigInteger two = new BigInteger("2");
+        final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE),
+                                                  pow400.multiply(two));
+
+        Assert.assertEquals(5, large.floatValue(), 1e-15);
+    }
+
     @Test
     public void testFloatValue() {
         BigFraction first = new BigFraction(1, 2);