commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [commons-numbers] branch master updated: Fixed method "compareTo(BigFraction)".
Date Sat, 02 Nov 2019 23:11:14 GMT
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


The following commit(s) were added to refs/heads/master by this push:
     new bfa6fcc  Fixed method "compareTo(BigFraction)".
bfa6fcc is described below

commit bfa6fcc4fa63bc0fb1726bb1aaf851e7894900be
Author: Gilles Sadowski <gilles@harfang.homelinux.org>
AuthorDate: Sun Nov 3 00:07:59 2019 +0100

    Fixed method "compareTo(BigFraction)".
    
    Implementation still assumed that only the numerator can be negative.
---
 .../commons/numbers/fraction/BigFraction.java      | 18 +++++++--------
 .../commons/numbers/fraction/BigFractionTest.java  | 27 ++++++++++++++--------
 2 files changed, 26 insertions(+), 19 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 89a7e3d..fd1ff52 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
@@ -535,16 +535,16 @@ public class BigFraction
      * Compares this object to another based on size.
      * </p>
      *
-     * @param object
-     *            the object to compare to, must not be <code>null</code>.
+     * @param other Object to compare to, must not be {@code null}.
      * @return -1 if this is less than {@code object}, +1 if this is greater
-     *         than {@code object}, 0 if they are equal.
-     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     * than {@code object}, 0 if they are equal.
+     *
+     * @see Comparable#compareTo(Object)
      */
     @Override
-    public int compareTo(final BigFraction object) {
-        int lhsSigNum = numerator.signum();
-        int rhsSigNum = object.numerator.signum();
+    public int compareTo(final BigFraction other) {
+        final int lhsSigNum = signum();
+        final int rhsSigNum = other.signum();
 
         if (lhsSigNum != rhsSigNum) {
             return (lhsSigNum > rhsSigNum) ? 1 : -1;
@@ -553,8 +553,8 @@ public class BigFraction
             return 0;
         }
 
-        BigInteger nOd = numerator.multiply(object.denominator);
-        BigInteger dOn = denominator.multiply(object.numerator);
+        final BigInteger nOd = numerator.multiply(other.denominator);
+        final BigInteger dOn = denominator.multiply(other.numerator);
         return nOd.compareTo(dOn);
     }
 
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index 24f3f46..c6a5eaf 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -158,14 +158,21 @@ public class BigFractionTest {
 
     @Test
     public void testCompareTo() {
-        BigFraction first = BigFraction.of(1, 2);
-        BigFraction second = BigFraction.of(1, 3);
-        BigFraction third = BigFraction.of(1, 2);
-
-        Assertions.assertEquals(0, first.compareTo(first));
-        Assertions.assertEquals(0, first.compareTo(third));
-        Assertions.assertEquals(1, first.compareTo(second));
-        Assertions.assertEquals(-1, second.compareTo(first));
+        BigFraction a = BigFraction.of(1, 2);
+        BigFraction b = BigFraction.of(1, 3);
+        BigFraction c = BigFraction.of(1, 2);
+        BigFraction d = BigFraction.of(-1, 2);
+        BigFraction e = BigFraction.of(1, -2);
+
+        Assertions.assertEquals(0, a.compareTo(a));
+        Assertions.assertEquals(0, a.compareTo(c));
+        Assertions.assertEquals(1, a.compareTo(b));
+        Assertions.assertEquals(-1, b.compareTo(a));
+        Assertions.assertEquals(-1, d.compareTo(a));
+        Assertions.assertEquals(1, a.compareTo(d));
+        Assertions.assertEquals(-1, e.compareTo(a));
+        Assertions.assertEquals(1, a.compareTo(e));
+        Assertions.assertEquals(0, d.compareTo(e));
 
         // these two values are different approximations of PI
         // the first  one is approximately PI - 3.07e-18
@@ -173,9 +180,8 @@ public class BigFractionTest {
         BigFraction pi1 = BigFraction.of(1068966896, 340262731);
         BigFraction pi2 = BigFraction.of( 411557987, 131002976);
         Assertions.assertEquals(-1, pi1.compareTo(pi2));
-        Assertions.assertEquals( 1, pi2.compareTo(pi1));
+        Assertions.assertEquals(1, pi2.compareTo(pi1));
         Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20);
-
     }
 
     @Test
@@ -517,6 +523,7 @@ public class BigFractionTest {
         Assertions.assertEquals(-Integer.MAX_VALUE, f.getDenominatorAsInt());
         Assertions.assertEquals(-1, f.getNumeratorAsInt());
 
+        Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 3).divide(BigInteger.valueOf(11)));
     }
 
     @Test


Mime
View raw message