You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/05 21:42:15 UTC

[commons-numbers] branch master updated (f91f812 -> a23596a)

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

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


    from f91f812  Increase coverage in RegularizedBetaTest.
     new 4e6e29a  Fraction: Check numerator before denominator in toString()
     new 489d90a  Increase coverage in FractionTest
     new 307024d  Increase coverage in BigFractionTest
     new 0824c49  Increase coverage in ContinuedFractionTest
     new a23596a  Formatting in ContinuedFraction.

The 5 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:
 .../numbers/fraction/ContinuedFraction.java        | 14 +++++-----
 .../commons/numbers/fraction/BigFractionTest.java  | 13 ++++++++++
 .../commons/numbers/fraction/CommonTestCases.java  |  2 ++
 .../numbers/fraction/ContinuedFractionTest.java    | 19 ++++++++++++++
 .../commons/numbers/fraction/FractionTest.java     | 30 ++++++++++++++++++++++
 5 files changed, 71 insertions(+), 7 deletions(-)


[commons-numbers] 04/05: Increase coverage in ContinuedFractionTest

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

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

commit 0824c497aa0e52f06111ec576d3046f9ca950621
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 22:15:02 2020 +0100

    Increase coverage in ContinuedFractionTest
---
 .../numbers/fraction/ContinuedFractionTest.java       | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
index 5cef76d..a6fe628 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
@@ -44,6 +44,25 @@ public class ContinuedFractionTest {
         Assertions.assertEquals(1.61803399, gr, eps);
     }
 
+    @Test
+    public void testMaxIterationsThrows() throws Exception {
+        ContinuedFraction cf = new ContinuedFraction() {
+            @Override
+            public double getA(int n, double x) {
+                return 1;
+            }
+
+            @Override
+            public double getB(int n, double x) {
+                return 1;
+            }
+        };
+
+        final double eps = 1e-8;
+        final int maxIterations = 3;
+        Assertions.assertThrows(FractionException.class, () -> cf.evaluate(0, eps, maxIterations));
+    }
+
     // NUMBERS-46
     @Test
     public void testOneIteration() {


[commons-numbers] 02/05: Increase coverage in FractionTest

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

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

commit 489d90a544970584e79ad69fe4f62e185773776d
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 15:24:18 2020 +0100

    Increase coverage in FractionTest
---
 .../commons/numbers/fraction/CommonTestCases.java  |  1 +
 .../commons/numbers/fraction/FractionTest.java     | 30 ++++++++++++++++++++++
 2 files changed, 31 insertions(+)

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 3e23798..1c5df47 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
@@ -213,6 +213,7 @@ final class CommonTestCases {
         testCases.add(new UnaryOperatorTestCase(1, Integer.MIN_VALUE, -1, Integer.MIN_VALUE));
 
         // XXX Failed by "BigFraction" (whose implementation differs from "Fraction").
+        // These are tested explicitly in FractionTest.
         // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, Integer.MIN_VALUE, -1, 1));
         // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, 1, Integer.MIN_VALUE, -1));
 
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 0dd7c5f..9d19663 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
@@ -218,6 +218,18 @@ public class FractionTest {
         }
     }
 
+    /**
+     * Test special cases of negation that differ from BigFraction.
+     */
+    @Test
+    public void testNegateMinValue() {
+        final Fraction one = Fraction.of(Integer.MIN_VALUE, Integer.MIN_VALUE);
+        assertFraction(-1, 1, one.negate());
+        // Special case where the negation of the numerator is not possible.
+        final Fraction minValue = Fraction.of(Integer.MIN_VALUE, 1);
+        assertFraction(Integer.MIN_VALUE, -1, minValue.negate());
+    }
+
     @Test
     public void testAdd() {
         for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.addFractionTestCases()) {
@@ -419,6 +431,23 @@ public class FractionTest {
         Fraction minusOne2 = Fraction.of(1, -1);
         Assertions.assertEquals(minusOne2, minusOne);
         Assertions.assertEquals(minusOne, minusOne2);
+
+        // Same numerator or denominator as 1/1
+        Fraction half = Fraction.of(1, 2);
+        Fraction two = Fraction.of(2, 1);
+        Assertions.assertNotEquals(one, half);
+        Assertions.assertNotEquals(one, two);
+
+        // Check worst case fractions which will have a component using MIN_VALUE.
+        // Note: abs(MIN_VALUE) is negative but this should not effect the equals result.
+        Fraction almostOne = Fraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
+        Fraction almostOne2 = Fraction.of(Integer.MIN_VALUE, -Integer.MAX_VALUE);
+        Assertions.assertEquals(almostOne, almostOne);
+        Assertions.assertNotEquals(almostOne, almostOne2);
+        Fraction almostZero = Fraction.of(-1, Integer.MIN_VALUE);
+        Fraction almostZero2 = Fraction.of(1, Integer.MIN_VALUE);
+        Assertions.assertEquals(almostZero, almostZero);
+        Assertions.assertNotEquals(almostZero, almostZero2);
     }
 
     @Test
@@ -433,6 +462,7 @@ public class FractionTest {
     @Test
     public void testToString() {
         Assertions.assertEquals("0", Fraction.of(0, 3).toString());
+        Assertions.assertEquals("0", Fraction.of(0, -3).toString());
         Assertions.assertEquals("3", Fraction.of(6, 2).toString());
         Assertions.assertEquals("2 / 3", Fraction.of(18, 27).toString());
         Assertions.assertEquals("-10 / 11", Fraction.of(-10, 11).toString());


[commons-numbers] 01/05: Fraction: Check numerator before denominator in toString()

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

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

commit 4e6e29a5c36fc40925973f2bfafbae3f5dbfa421
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 14:52:52 2020 +0100

    Fraction: Check numerator before denominator in toString()
    
    If constructed with a numerator of 0 the denominator is always reduced
    to 1 as the greatest common divisor.
    
    An alternative would be to drop the numerator == 0 branch as it is
    unreachable since denominator should always be 1.


[commons-numbers] 05/05: Formatting in ContinuedFraction.

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

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

commit a23596ae37ccba3657df18f01d02ca4ac5ef5dd3
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 22:24:16 2020 +0100

    Formatting in ContinuedFraction.
    
    Use https for javadoc link.
    
    Wrap the exception message and exception format argument to the same
    line.
---
 .../apache/commons/numbers/fraction/ContinuedFraction.java | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
index cf04ddb..cd98311 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
@@ -20,13 +20,13 @@ import org.apache.commons.numbers.core.Precision;
 
 /**
  * Provides a generic means to evaluate
- * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">continued fractions</a>.
+ * <a href="https://mathworld.wolfram.com/ContinuedFraction.html">continued fractions</a>.
  * Subclasses must provide the {@link #getA(int,double) a} and {@link #getB(int,double) b}
  * coefficients to evaluate the continued fraction.
  */
 public abstract class ContinuedFraction {
     /**
-     * Defines the <a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+     * Defines the <a href="https://mathworld.wolfram.com/ContinuedFraction.html">
      * {@code n}-th "a" coefficient</a> of the continued fraction.
      *
      * @param n Index of the coefficient to retrieve.
@@ -36,7 +36,7 @@ public abstract class ContinuedFraction {
     protected abstract double getA(int n, double x);
 
     /**
-     * Defines the <a href="http://mathworld.wolfram.com/ContinuedFraction.html">
+     * Defines the <a href="https://mathworld.wolfram.com/ContinuedFraction.html">
      * {@code n}-th "b" coefficient</a> of the continued fraction.
      *
      * @param n Index of the coefficient to retrieve.
@@ -116,12 +116,12 @@ public abstract class ContinuedFraction {
             hN = hPrev * deltaN;
 
             if (Double.isInfinite(hN)) {
-                throw new FractionException("Continued fraction convergents diverged to +/- infinity for value {0}",
-                                               x);
+                throw new FractionException(
+                    "Continued fraction convergents diverged to +/- infinity for value {0}", x);
             }
             if (Double.isNaN(hN)) {
-                throw new FractionException("Continued fraction diverged to NaN for value {0}",
-                                               x);
+                throw new FractionException(
+                    "Continued fraction diverged to NaN for value {0}", x);
             }
 
             if (Math.abs(deltaN - 1) < epsilon) {


[commons-numbers] 03/05: Increase coverage in BigFractionTest

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

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

commit 307024db19127e7b101a5071fe3e263724a3243f
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 21:32:31 2020 +0100

    Increase coverage in BigFractionTest
---
 .../apache/commons/numbers/fraction/BigFractionTest.java    | 13 +++++++++++++
 .../apache/commons/numbers/fraction/CommonTestCases.java    |  1 +
 2 files changed, 14 insertions(+)

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 a90ecd3..62057a2 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
@@ -385,6 +385,7 @@ public class BigFractionTest {
 
     @Test
     public void testConstructorDouble() {
+        assertFraction(0, 1, BigFraction.from(0.0));
         assertFraction(1, 2, BigFraction.from(0.5));
         assertFraction(6004799503160661L, 18014398509481984L, BigFraction.from(1.0 / 3.0));
         assertFraction(6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / 100.0));
@@ -477,6 +478,10 @@ public class BigFractionTest {
         Assertions.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
         Assertions.assertEquals(1, f.getDenominatorAsInt());
 
+        // Special case when numerator signum is zero
+        f = BigFraction.ZERO.add(BigInteger.TEN);
+        Assertions.assertEquals(10, f.getNumeratorAsInt());
+        Assertions.assertEquals(1, f.getDenominatorAsInt());
     }
 
     @Test
@@ -488,6 +493,7 @@ public class BigFractionTest {
         }
 
         Assertions.assertThrows(FractionException.class, () -> BigFraction.of(1, 2).divide(BigInteger.ZERO));
+        Assertions.assertThrows(FractionException.class, () -> BigFraction.of(1, 2).divide(BigFraction.ZERO));
 
         BigFraction f1;
         BigFraction f2;
@@ -595,6 +601,12 @@ public class BigFractionTest {
         BigFraction minusOne2 = BigFraction.of(1, -1);
         Assertions.assertEquals(minusOne2, minusOne);
         Assertions.assertEquals(minusOne, minusOne2);
+
+        // Same numerator or denominator as 1/1
+        BigFraction half = BigFraction.of(1, 2);
+        BigFraction two = BigFraction.of(2, 1);
+        Assertions.assertNotEquals(one, half);
+        Assertions.assertNotEquals(one, two);
     }
 
     @Test
@@ -648,6 +660,7 @@ public class BigFractionTest {
     @Test
     public void testToString() {
         Assertions.assertEquals("0", BigFraction.of(0, 3).toString());
+        Assertions.assertEquals("0", BigFraction.of(0, -3).toString());
         Assertions.assertEquals("3", BigFraction.of(6, 2).toString());
         Assertions.assertEquals("2 / 3", BigFraction.of(18, 27).toString());
         Assertions.assertEquals("-10 / 11", BigFraction.of(-10, 11).toString());
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 1c5df47..0938bcf 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
@@ -164,6 +164,7 @@ final class CommonTestCases {
         testCases.add(new DoubleToFractionTestCase(0.00000000000001, 0, 1));
         testCases.add(new DoubleToFractionTestCase(0.40000000000001, 2, 5));
         testCases.add(new DoubleToFractionTestCase(15.0000000000001, 15, 1));
+        testCases.add(new DoubleToFractionTestCase(0.0, 0, 1));
 
         return testCases;
     }