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 2019/11/09 00:03:13 UTC

[commons-numbers] 10/19: Change !Double.isInfinite(w) to w < Infinity and w == Infinity.

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 b2917ede83fce0aff88615da77a977fbc56153cc
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Nov 8 21:56:56 2019 +0000

    Change !Double.isInfinite(w) to w < Infinity and w == Infinity.
    
    Adds explicit edge case for NaN.
    
    This change removes the potential two boolean tests in Double.isInfinite
    to two if statements. The number of test is the same but the case for
    NaN is handled explicitly.
    
    Change suggested from PMD analysis.
---
 .../main/java/org/apache/commons/numbers/gamma/InverseErf.java | 10 +++++++---
 .../java/org/apache/commons/numbers/gamma/InverseErfTest.java  |  1 +
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
index 95c9966..802262a 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
@@ -40,7 +40,7 @@ public final class InverseErf {
      */
     public static double value(final double x) {
         // Beware that the logarithm argument must be
-        // commputed as (1 - x) * (1 + x),
+        // computed as (1 - x) * (1 + x),
         // it must NOT be simplified as 1 - x * x as this
         // would induce rounding errors near the boundaries +/-1
         double w = -Math.log((1 - x) * (1 + x));
@@ -92,7 +92,7 @@ public final class InverseErf {
             p =     0.005370914553590063617 + p * w;
             p =       1.0052589676941592334 + p * w;
             p =       3.0838856104922207635 + p * w;
-        } else if (!Double.isInfinite(w)) {
+        } else if (w < Double.POSITIVE_INFINITY) {
             w = Math.sqrt(w) - 5;
             p =  -2.7109920616438573243e-11;
             p =  -2.5556418169965252055e-10 + p * w;
@@ -111,7 +111,7 @@ public final class InverseErf {
             p =  -0.00013871931833623122026 + p * w;
             p =       1.0103004648645343977 + p * w;
             p =       4.8499064014085844221 + p * w;
-        } else {
+        } else if (w == Double.POSITIVE_INFINITY) {
             // this branch does not appears in the original code, it
             // was added because the previous branch does not handle
             // x = +/-1 correctly. In this case, w is positive infinity
@@ -121,6 +121,10 @@ public final class InverseErf {
             // So the branch above incorrectly returns negative infinity
             // instead of the correct positive infinity.
             p = Double.POSITIVE_INFINITY;
+        } else {
+            // this branch does not appears in the original code, it
+            // occurs when the input is NaN or not in the range [-1, 1].
+            return Double.NaN;
         }
 
         return p * x;
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
index e988972..af2295d 100644
--- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
@@ -27,6 +27,7 @@ public class InverseErfTest {
     public void testErfInvNaN() {
         Assertions.assertTrue(Double.isNaN(InverseErf.value(-1.001)));
         Assertions.assertTrue(Double.isNaN(InverseErf.value(+1.001)));
+        Assertions.assertTrue(Double.isNaN(InverseErf.value(Double.NaN)));
     }
 
     @Test