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 2019/07/01 17:24:54 UTC

[commons-numbers] branch master updated (39c83ad -> f8e93c2)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git.


    from 39c83ad  NUMBERS-123: Javadoc
     new 4858d9b  NUMBERS-125: Delete Fraction.getReducedFraction(int, int)
     new fbe05a1  NUMBERS-125: Delete BigFraction.reduce()
     new 3fd1954  NUMBERS-125: Clarify numerator Javadoc
     new 8d42ef4  Formatting.
     new a556dd7  Merge branch 'NUMBERS-125__heinrich'
     new ac1b273  NUMBERS-127: Fraction(int, int): Negate numerator and denominator only after reduction to lowest terms
     new f8e93c2  Merge branch 'NUMBERS-127__heinrich'

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/numbers/fraction/BigFraction.java      | 30 +++-------
 .../apache/commons/numbers/fraction/Fraction.java  | 69 +++++++---------------
 .../commons/numbers/fraction/CommonTestCases.java  |  3 +
 .../commons/numbers/fraction/FractionTest.java     | 18 ------
 4 files changed, 30 insertions(+), 90 deletions(-)


[commons-numbers] 05/07: Merge branch 'NUMBERS-125__heinrich'

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit a556dd78eb978045f064ba57fec6545c3f62de93
Merge: 39c83ad 8d42ef4
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Mon Jul 1 19:17:52 2019 +0200

    Merge branch 'NUMBERS-125__heinrich'
    
    Closes #61.

 .../commons/numbers/fraction/BigFraction.java      | 30 ++++-----------
 .../apache/commons/numbers/fraction/Fraction.java  | 45 ++--------------------
 .../commons/numbers/fraction/FractionTest.java     | 18 ---------
 3 files changed, 11 insertions(+), 82 deletions(-)


[commons-numbers] 02/07: NUMBERS-125: Delete BigFraction.reduce()

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit fbe05a1b79fb9b872666678d8d455cd1be7a0232
Author: Schamschi <he...@gmx.at>
AuthorDate: Mon Jul 1 13:29:57 2019 +0200

    NUMBERS-125: Delete BigFraction.reduce()
---
 .../commons/numbers/fraction/BigFraction.java      | 27 ++++------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 11c6a7b..3971525 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -42,10 +42,10 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
     /** Parameter name for BigIntegers (to satisfy checkstyle). */
     private static final String PARAM_NAME_BG = "bg";
 
-    /** The numerator. */
+    /** The numerator of this fraction reduced to lowest terms. Possibly negative*/
     private final BigInteger numerator;
 
-    /** The denominator. */
+    /** The denominator of this fraction reduced to lowest terms. Always positive*/
     private final BigInteger denominator;
 
     /**
@@ -706,9 +706,8 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
         if (this == other) {
             ret = true;
         } else if (other instanceof BigFraction) {
-            BigFraction rhs = ((BigFraction) other).reduce();
-            BigFraction thisOne = this.reduce();
-            ret = thisOne.numerator.equals(rhs.numerator) && thisOne.denominator.equals(rhs.denominator);
+            BigFraction rhs = (BigFraction) other;
+            ret = numerator.equals(rhs.numerator) && denominator.equals(rhs.denominator);
         }
 
         return ret;
@@ -1038,24 +1037,6 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
 
     /**
      * <p>
-     * Reduce this <code>BigFraction</code> to its lowest terms.
-     * </p>
-     *
-     * @return the reduced <code>BigFraction</code>. It doesn't change anything if
-     *         the fraction can be reduced.
-     */
-    public BigFraction reduce() {
-        final BigInteger gcd = numerator.gcd(denominator);
-
-        if (BigInteger.ONE.compareTo(gcd) < 0) {
-            return new BigFraction(numerator.divide(gcd), denominator.divide(gcd));
-        } else {
-            return this;
-        }
-    }
-
-    /**
-     * <p>
      * Subtracts the value of an {@link BigInteger} from the value of this
      * {@code BigFraction}, returning the result in reduced form.
      * </p>


[commons-numbers] 01/07: NUMBERS-125: Delete Fraction.getReducedFraction(int, int)

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 4858d9bfdb617412ae6fba99bf551cb36db2757d
Author: Schamschi <he...@gmx.at>
AuthorDate: Mon Jul 1 13:01:23 2019 +0200

    NUMBERS-125: Delete Fraction.getReducedFraction(int, int)
---
 .../apache/commons/numbers/fraction/Fraction.java  | 41 ++--------------------
 .../commons/numbers/fraction/FractionTest.java     | 18 ----------
 2 files changed, 2 insertions(+), 57 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index f741c52..481df2f 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -504,8 +504,8 @@ public class Fraction
         // make sure we don't overflow unless the result *must* overflow.
         int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
         int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
-        return getReducedFraction
-        (Math.multiplyExact(numerator/d1, fraction.numerator/d2),
+        return of(
+                Math.multiplyExact(numerator/d1, fraction.numerator/d2),
                 Math.multiplyExact(denominator/d2, fraction.denominator/d1));
     }
 
@@ -548,43 +548,6 @@ public class Fraction
     }
 
     /**
-     * <p>Creates a {@code Fraction} instance with the 2 parts
-     * of a fraction Y/Z.</p>
-     *
-     * <p>Any negative signs are resolved to be on the numerator.</p>
-     *
-     * @param numerator  the numerator, for example the three in 'three sevenths'
-     * @param denominator  the denominator, for example the seven in 'three sevenths'
-     * @return a new fraction instance, with the numerator and denominator reduced
-     * @throws ArithmeticException if the denominator is {@code zero}
-     */
-    public static Fraction getReducedFraction(int numerator, int denominator) {
-        if (denominator == 0) {
-            throw new FractionException(FractionException.ERROR_ZERO_DENOMINATOR);
-        }
-        if (numerator==0) {
-            return ZERO; // normalize zero.
-        }
-        // allow 2^k/-2^31 as a valid fraction (where k>0)
-        if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
-            numerator/=2; denominator/=2;
-        }
-        if (denominator < 0) {
-            if (numerator==Integer.MIN_VALUE ||
-                    denominator==Integer.MIN_VALUE) {
-                throw new FractionException(FractionException.ERROR_NEGATION_OVERFLOW, numerator, denominator);
-            }
-            numerator = -numerator;
-            denominator = -denominator;
-        }
-        // simplify fraction.
-        int gcd = ArithmeticUtils.gcd(numerator, denominator);
-        numerator /= gcd;
-        denominator /= gcd;
-        return new Fraction(numerator, denominator);
-    }
-
-    /**
      * @param n Power.
      * @return {@code this^n}
      */
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
index 42c1b35..2cf711c 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -481,24 +481,6 @@ public class FractionTest {
     }
 
     @Test
-    public void testGetReducedFraction() {
-        Fraction threeFourths = Fraction.of(3, 4);
-        Assertions.assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8)));
-        Assertions.assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1)));
-        Assertions.assertThrows(ArithmeticException.class,
-                () -> Fraction.getReducedFraction(1, 0)
-        );
-        Assertions.assertEquals(
-                -1,
-                Fraction.getReducedFraction(2, Integer.MIN_VALUE).getNumerator()
-        );
-        Assertions.assertEquals(
-                -1,
-                Fraction.getReducedFraction(1, -1).getNumerator()
-        );
-    }
-
-    @Test
     public void testToString() {
         Assertions.assertEquals("0", Fraction.of(0, 3).toString());
         Assertions.assertEquals("3", Fraction.of(6, 2).toString());


[commons-numbers] 07/07: Merge branch 'NUMBERS-127__heinrich'

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit f8e93c2e8b04b10cb76136746e828a2dfe2f4c36
Merge: a556dd7 ac1b273
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Mon Jul 1 19:23:07 2019 +0200

    Merge branch 'NUMBERS-127__heinrich'
    
    Closes #62.

 .../apache/commons/numbers/fraction/Fraction.java  | 24 ++++++++++++++--------
 .../commons/numbers/fraction/CommonTestCases.java  |  3 +++
 2 files changed, 19 insertions(+), 8 deletions(-)


[commons-numbers] 03/07: NUMBERS-125: Clarify numerator Javadoc

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 3fd195422205fd454c1e644f40c96c45e7a4ed9c
Author: Schamschi <he...@gmx.at>
AuthorDate: Mon Jul 1 15:18:56 2019 +0200

    NUMBERS-125: Clarify numerator Javadoc
---
 .../main/java/org/apache/commons/numbers/fraction/BigFraction.java | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 3971525..4cf1ecf 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -42,10 +42,13 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
     /** Parameter name for BigIntegers (to satisfy checkstyle). */
     private static final String PARAM_NAME_BG = "bg";
 
-    /** The numerator of this fraction reduced to lowest terms. Possibly negative*/
+    /**
+     * The numerator of this fraction reduced to lowest terms. Negative if this
+     * fraction's value is negative.
+     */
     private final BigInteger numerator;
 
-    /** The denominator of this fraction reduced to lowest terms. Always positive*/
+    /** The denominator of this fraction reduced to lowest terms. Always positive. */
     private final BigInteger denominator;
 
     /**


[commons-numbers] 04/07: Formatting.

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 8d42ef4ca161546b6e71f8d0e7340b3412c4edb8
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Mon Jul 1 19:17:08 2019 +0200

    Formatting.
---
 .../main/java/org/apache/commons/numbers/fraction/Fraction.java   | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index 481df2f..d8dbea4 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -497,16 +497,16 @@ public class Fraction
         if (fraction == null) {
             throw new NullPointerException(PARAM_NAME_FRACTION);
         }
-        if (numerator == 0 || fraction.numerator == 0) {
+        if (numerator == 0 ||
+            fraction.numerator == 0) {
             return ZERO;
         }
         // knuth 4.5.1
         // make sure we don't overflow unless the result *must* overflow.
         int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
         int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
-        return of(
-                Math.multiplyExact(numerator/d1, fraction.numerator/d2),
-                Math.multiplyExact(denominator/d2, fraction.denominator/d1));
+        return of(Math.multiplyExact(numerator / d1, fraction.numerator / d2),
+                  Math.multiplyExact(denominator / d2, fraction.denominator / d1));
     }
 
     /**


[commons-numbers] 06/07: NUMBERS-127: Fraction(int, int): Negate numerator and denominator only after reduction to lowest terms

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit ac1b27356853d98acfab98db2b9f535325adacf8
Author: Schamschi <he...@gmx.at>
AuthorDate: Mon Jul 1 14:57:58 2019 +0200

    NUMBERS-127: Fraction(int, int): Negate numerator and denominator only after reduction to lowest terms
---
 .../apache/commons/numbers/fraction/Fraction.java  | 24 ++++++++++++++--------
 .../commons/numbers/fraction/CommonTestCases.java  |  3 +++
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index d8dbea4..9920d03 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -162,15 +162,18 @@ public class Fraction
         if (den == 0) {
             throw new ArithmeticException("division by zero");
         }
-        if (den < 0) {
-            if (num == Integer.MIN_VALUE ||
-                den == Integer.MIN_VALUE) {
-                throw new FractionException(FractionException.ERROR_NEGATION_OVERFLOW, num, den);
-            }
-            num = -num;
-            den = -den;
+
+        /*
+         * if num and den are both 2^-31, or if one is 0 and the other is 2^-31,
+         * the calculation of the gcd below will fail. Ensure that this does not
+         * happen by dividing both by 2 in case both are even.
+         */
+        if (((num | den) & 1) == 0) {
+            num >>= 1;
+            den >>= 1;
         }
-        // reduce numerator and denominator by greatest common denominator.
+
+        // reduce numerator and denominator by greatest common divisor.
         final int d = ArithmeticUtils.gcd(num, den);
         if (d > 1) {
             num /= d;
@@ -179,9 +182,14 @@ public class Fraction
 
         // move sign to numerator.
         if (den < 0) {
+            if (num == Integer.MIN_VALUE ||
+                den == Integer.MIN_VALUE) {
+                throw new FractionException(FractionException.ERROR_NEGATION_OVERFLOW, num, den);
+            }
             num = -num;
             den = -den;
         }
+
         this.numerator   = num;
         this.denominator = den;
     }
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
index 3b71c72..d28ef63 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
@@ -86,6 +86,9 @@ class CommonTestCases {
         testCases.add(new UnaryOperatorTestCase(-2, 4, -1, 2));
         testCases.add(new UnaryOperatorTestCase(2, -4, -1, 2));
 
+        testCases.add(new UnaryOperatorTestCase(2, Integer.MIN_VALUE, -1, - (Integer.MIN_VALUE / 2)));
+        testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, -2, - (Integer.MIN_VALUE / 2), 1));
+
         return testCases;
     }