commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r1059749 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/math/Fraction.java test/java/org/apache/commons/lang3/math/FractionTest.java
Date Mon, 17 Jan 2011 04:30:05 GMT
Author: bayard
Date: Mon Jan 17 04:30:05 2011
New Revision: 1059749

URL: http://svn.apache.org/viewvc?rev=1059749&view=rev
Log:
Adding first method check from Math's MathUtils.gcd method; and unit tests showing that this
was needed. Bug reported and solved by Christian Semrau [LANG-662]

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

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/Fraction.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/Fraction.java?rev=1059749&r1=1059748&r2=1059749&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/Fraction.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/Fraction.java Mon
Jan 17 04:30:05 2011
@@ -579,8 +579,15 @@ public final class Fraction extends Numb
      * @return the greatest common divisor, never zero
      */
     private static int greatestCommonDivisor(int u, int v) {
-        //if either op. is abs 0 or 1, return 1:
-        if (Math.abs(u) <= 1 || Math.abs(v) <= 1) {
+        // From Commons Math:
+        if ((u == 0) || (v == 0)) {
+            if ((u == Integer.MIN_VALUE) || (v == Integer.MIN_VALUE)) {
+                throw new ArithmeticException("overflow: gcd is 2^31");
+            }
+            return Math.abs(u) + Math.abs(v);
+        }
+        //if either operand is abs 1, return 1:
+        if (Math.abs(u) == 1 || Math.abs(v) == 1) {
             return 1;
         }
         // keep u and v negative, as negative integers range down to

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/FractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/FractionTest.java?rev=1059749&r1=1059748&r2=1059749&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/FractionTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/FractionTest.java
Mon Jan 17 04:30:05 2011
@@ -330,6 +330,11 @@ public class FractionTest extends TestCa
             f = Fraction.getReducedFraction(-7, Integer.MIN_VALUE);  
             fail("Expecting ArithmeticException");
         } catch (ArithmeticException ex) {}      
+
+        // LANG-662
+        f = Fraction.getReducedFraction(Integer.MIN_VALUE, 2);
+        assertEquals(Integer.MIN_VALUE / 2, f.getNumerator());
+        assertEquals(1, f.getDenominator());
     }
 
     public void testFactory_double() {
@@ -643,6 +648,11 @@ public class FractionTest extends TestCa
         assertEquals(0, result.getNumerator());
         assertEquals(1, result.getDenominator());
         assertSame(result, Fraction.ZERO);
+
+        f = Fraction.getFraction(Integer.MIN_VALUE, 2);
+        result = f.reduce();
+        assertEquals(Integer.MIN_VALUE / 2, result.getNumerator());
+        assertEquals(1, result.getDenominator());
     }
     
     public void testInvert() {



Mime
View raw message