commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1368253 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/fraction/Fraction.java test/java/org/apache/commons/math3/fraction/FractionTest.java
Date Wed, 01 Aug 2012 21:24:27 GMT
Author: tn
Date: Wed Aug  1 21:24:27 2012
New Revision: 1368253

URL: http://svn.apache.org/viewvc?rev=1368253&view=rev
Log:
[MATH-836] Improve overflow check for negative values in Fraction constructor.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fraction/Fraction.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fraction/FractionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fraction/Fraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fraction/Fraction.java?rev=1368253&r1=1368252&r2=1368253&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fraction/Fraction.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fraction/Fraction.java
Wed Aug  1 21:24:27 2012
@@ -178,7 +178,7 @@ public class Fraction
         long overflow = Integer.MAX_VALUE;
         double r0 = value;
         long a0 = (long)FastMath.floor(r0);
-        if (a0 > overflow) {
+        if (FastMath.abs(a0) > overflow) {
             throw new FractionConversionException(value, a0, 1l);
         }
 
@@ -206,7 +206,7 @@ public class Fraction
             long a1 = (long)FastMath.floor(r1);
             p2 = (a1 * p1) + p0;
             q2 = (a1 * q1) + q0;
-            if ((p2 > overflow) || (q2 > overflow)) {
+            if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) {
                 throw new FractionConversionException(value, p2, q2);
             }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fraction/FractionTest.java?rev=1368253&r1=1368252&r2=1368253&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
Wed Aug  1 21:24:27 2012
@@ -60,6 +60,7 @@ public class FractionTest {
         } catch (MathArithmeticException ex) {
             // success
         }
+
         assertFraction(0, 1, new Fraction(0.00000000000001));
         assertFraction(2, 5, new Fraction(0.40000000000001));
         assertFraction(15, 1, new Fraction(15.0000000000001));
@@ -134,6 +135,8 @@ public class FractionTest {
     public void testIntegerOverflow() {
         checkIntegerOverflow(0.75000000001455192);
         checkIntegerOverflow(1.0e10);
+        checkIntegerOverflow(-1.0e10);
+        checkIntegerOverflow(-43979.60679604749);
     }
 
     private void checkIntegerOverflow(double a) {



Mime
View raw message