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 2015/08/21 00:30:34 UTC

[math] MATH-1261

Repository: commons-math
Updated Branches:
  refs/heads/MATH_3_X b1f4a30aa -> 0820703df


MATH-1261

Avoid unnecessary overflow.  Thanks to Osamu Ikeuchi.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0820703d
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0820703d
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0820703d

Branch: refs/heads/MATH_3_X
Commit: 0820703df043ca5df06fe808bc2998296e7bbcc5
Parents: b1f4a30
Author: Gilles <er...@apache.org>
Authored: Fri Aug 21 00:22:59 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Aug 21 00:22:59 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                                  |  3 +++
 .../java/org/apache/commons/math3/fraction/Fraction.java |  4 ++--
 .../org/apache/commons/math3/fraction/FractionTest.java  | 11 +++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/0820703d/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 24a288f..6688fd2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
   </properties>
   <body>
     <release version="3.6" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="fix" issue="MATH-1261" due-to="Osamu Ikeuchi">
+        Avoid overflow in "Fraction" (multiplication or division by an int).
+      </action>
       <action dev="oertl" type="fix" issue="MATH-1258" due-to="Gunel Jahangirova">
         Added check for equal array lengths to distance measure functions.
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/0820703d/src/main/java/org/apache/commons/math3/fraction/Fraction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/fraction/Fraction.java b/src/main/java/org/apache/commons/math3/fraction/Fraction.java
index 1752043..92a5a99 100644
--- a/src/main/java/org/apache/commons/math3/fraction/Fraction.java
+++ b/src/main/java/org/apache/commons/math3/fraction/Fraction.java
@@ -559,7 +559,7 @@ public class Fraction
      * @return this * i
      */
     public Fraction multiply(final int i) {
-        return new Fraction(numerator * i, denominator);
+        return multiply(new Fraction(i));
     }
 
     /**
@@ -589,7 +589,7 @@ public class Fraction
      * @return this * i
      */
     public Fraction divide(final int i) {
-        return new Fraction(numerator, denominator * i);
+        return divide(new Fraction(i));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-math/blob/0820703d/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/fraction/FractionTest.java b/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
index 132fd0c..5f5c665 100644
--- a/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
+++ b/src/test/java/org/apache/commons/math3/fraction/FractionTest.java
@@ -261,6 +261,17 @@ public class FractionTest {
     }
 
     @Test
+    public void testMath1261() {
+        final Fraction a = new Fraction(Integer.MAX_VALUE, 2);
+        final Fraction b = a.multiply(2);
+        Assert.assertTrue(b.equals(new Fraction(Integer.MAX_VALUE)));
+
+        final Fraction c = new Fraction(2, Integer.MAX_VALUE);
+        final Fraction d = c.divide(2);
+        Assert.assertTrue(d.equals(new Fraction(1, Integer.MAX_VALUE)));
+    }
+
+    @Test
     public void testReciprocal() {
         Fraction f = null;