commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [3/5] commons-numbers git commit: NUMBER-46: Meaning of "maxIterations" argument.
Date Mon, 03 Jul 2017 14:24:37 GMT
NUMBER-46: Meaning of "maxIterations" argument.


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

Branch: refs/heads/master
Commit: bc83d14fe2b7b4e86f6b01d37ce6b92bbf468916
Parents: 47e19ab
Author: Gilles Sadowski <gilles@harfang.homelinux.org>
Authored: Mon Jul 3 16:08:19 2017 +0200
Committer: Gilles Sadowski <gilles@harfang.homelinux.org>
Committed: Mon Jul 3 16:08:19 2017 +0200

----------------------------------------------------------------------
 .../numbers/fraction/ContinuedFraction.java     | 28 ++++++-------
 .../numbers/fraction/ContinuedFractionTest.java | 44 +++++++++++++++++++-
 2 files changed, 54 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bc83d14f/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/ContinuedFraction.java
----------------------------------------------------------------------
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 47bd9b9..5c5648d 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
@@ -80,7 +80,7 @@ public abstract class ContinuedFraction {
     }
 
     /**
-     * Evaluates the continued fraction at the value x.
+     * Evaluates the continued fraction.
      * <p>
      * The implementation of this method is based on the modified Lentz algorithm as described
      * on page 18 ff. in:
@@ -99,12 +99,13 @@ public abstract class ContinuedFraction {
      * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction
@ MathWorld</a>.
      * </p>
      *
-     * @param x the evaluation point.
-     * @param epsilon maximum error allowed.
-     * @param maxIterations maximum number of convergents
-     * @return the value of the continued fraction evaluated at x.
+     * @param x Point at which to evaluate the continued fraction.
+     * @param epsilon Maximum error allowed.
+     * @param maxIterations Maximum number of iterations.
+     * @return the value of the continued fraction evaluated at {@code x}.
      * @throws ArithmeticException if the algorithm fails to converge.
-     * @throws ArithmeticException if maximal number of iterations is reached
+     * @throws ArithmeticException if the maximal number of iterations is reached
+     * before the expected convergence is achieved.
      */
     public double evaluate(double x, double epsilon, int maxIterations) {
         final double small = 1e-50;
@@ -120,7 +121,7 @@ public abstract class ContinuedFraction {
         double cPrev = hPrev;
         double hN = hPrev;
 
-        while (n < maxIterations) {
+        while (n <= maxIterations) {
             final double a = getA(n, x);
             final double b = getB(n, x);
 
@@ -146,21 +147,16 @@ public abstract class ContinuedFraction {
                                                x);
             }
 
-            if (Math.abs(deltaN - 1.0) < epsilon) {
-                break;
+            if (Math.abs(deltaN - 1) < epsilon) {
+                return hN;
             }
 
             dPrev = dN;
             cPrev = cN;
             hPrev = hN;
-            n++;
-        }
-
-        if (n >= maxIterations) {
-            throw new FractionException("maximal count ({0}) exceeded", maxIterations);
+            ++n;
         }
 
-        return hN;
+        throw new FractionException("maximal count ({0}) exceeded", maxIterations);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/bc83d14f/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/ContinuedFractionTest.java
----------------------------------------------------------------------
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 8f450aa..8543f0a 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
@@ -28,8 +28,47 @@ public class ContinuedFractionTest {
     @Test
     public void testGoldenRatio() 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;
+        double gr = cf.evaluate(0, eps);
+        Assert.assertEquals(1.61803399, gr, eps);
+    }
+
+    // NUMBERS-46
+    @Test
+    public void testOneIteration() {
+        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 = 10;
+        double gr = cf.evaluate(0, eps, 1);
+        Assert.assertEquals(1.61, gr, eps);
+    }
+
+    // NUMBERS-46
+    @Test
+    public void testTwoIterations() {
+        ContinuedFraction cf = new ContinuedFraction() {
+            @Override
             public double getA(int n, double x) {
                 return 1;
             }
@@ -40,7 +79,8 @@ public class ContinuedFractionTest {
             }
         };
 
-        double gr = cf.evaluate(0.0, 1e-8);
-        Assert.assertEquals(1.61803399, gr, 1e-8);
+        final double eps = 0.5;
+        double gr = cf.evaluate(0, eps, 2);
+        Assert.assertEquals(1.5, gr, 0d);
     }
 }


Mime
View raw message