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:14 UTC

[commons-numbers] 11/19: Use constants for magic numbers.

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 b7420d0d45e80f9cea4b13614bc1746da6234ac9
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Nov 8 22:41:00 2019 +0000

    Use constants for magic numbers.
    
    Allow some gamma classes to use magic numbers in PMD rules.
---
 .../java/org/apache/commons/numbers/gamma/Erf.java |  5 ++-
 .../org/apache/commons/numbers/gamma/Erfc.java     |  5 ++-
 .../org/apache/commons/numbers/gamma/Gamma.java    |  5 ++-
 .../org/apache/commons/numbers/gamma/LogBeta.java  | 37 +++++++++++++---------
 src/main/resources/pmd/pmd-ruleset.xml             |  5 +++
 5 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
index 4434b58..6f700e2 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
@@ -20,6 +20,9 @@ package org.apache.commons.numbers.gamma;
  * <a href="http://mathworld.wolfram.com/Erf.html">Error function</a>.
  */
 public final class Erf {
+    /** The threshold value for returning the extreme value. */
+    private static final double EXTREME_VALUE_BOUND = 40;
+
     /** Private constructor. */
     private Erf() {
         // intenitonal empty.
@@ -46,7 +49,7 @@ public final class Erf {
      * @see RegularizedGamma.P#value(double, double, double, int)
      */
     public static double value(double x) {
-        if (Math.abs(x) > 40) {
+        if (Math.abs(x) > EXTREME_VALUE_BOUND) {
             return x > 0 ? 1 : -1;
         }
         final double ret = RegularizedGamma.P.value(0.5, x * x, 1e-15, 10000);
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
index 07a7cbc..44a6879 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
@@ -20,6 +20,9 @@ package org.apache.commons.numbers.gamma;
  * <a href="http://mathworld.wolfram.com/Erfc.html">Complementary error function</a>.
  */
 public final class Erfc {
+    /** The threshold value for returning the extreme value. */
+    private static final double EXTREME_VALUE_BOUND = 40;
+
     /** Private constructor. */
     private Erfc() {
         // intentionally empty.
@@ -46,7 +49,7 @@ public final class Erfc {
      * @see RegularizedGamma.Q#value(double, double, double, int)
      */
     public static double value(double x) {
-        if (Math.abs(x) > 40) {
+        if (Math.abs(x) > EXTREME_VALUE_BOUND) {
             return x > 0 ? 0 : 2;
         }
         final double ret = RegularizedGamma.Q.value(0.5, x * x, 1e-15, 10000);
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java
index 2698a73..c43e2a7 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Gamma.java
@@ -31,6 +31,9 @@ package org.apache.commons.numbers.gamma;
  * </p>
  */
 public final class Gamma {
+    /** The threshold value for choosing the Lanczos approximation. */
+    private static final double LANCZOS_THRESHOLD = 20;
+
     /** &radic;(2&pi;). */
     private static final double SQRT_TWO_PI = 2.506628274631000502;
 
@@ -55,7 +58,7 @@ public final class Gamma {
         }
 
         final double absX = Math.abs(x);
-        if (absX <= 20) {
+        if (absX <= LANCZOS_THRESHOLD) {
             if (x >= 1) {
                 /*
                  * From the recurrence relation
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java
index 04ea5b8..12e4863 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/LogBeta.java
@@ -23,6 +23,13 @@ package org.apache.commons.numbers.gamma;
  * </p>
  */
 public final class LogBeta {
+    /** The threshold value of 10 where the series expansion of the Δ function applies. */
+    private static final double TEN = 10;
+    /** The threshold value of 2 for algorithm switch. */
+    private static final double TWO = 2;
+    /** The threshold value of 1000 for algorithm switch. */
+    private static final double THOUSAND = 1000;
+
     /** The constant value of ½log 2π. */
     private static final double HALF_LOG_TWO_PI = 0.9189385332046727;
 
@@ -85,8 +92,8 @@ public final class LogBeta {
             a > b) {
             throw new GammaException(GammaException.OUT_OF_RANGE, a, 0, b);
         }
-        if (b < 10) {
-            throw new GammaException(GammaException.OUT_OF_RANGE, b, 10, Double.POSITIVE_INFINITY);
+        if (b < TEN) {
+            throw new GammaException(GammaException.OUT_OF_RANGE, b, TEN, Double.POSITIVE_INFINITY);
         }
 
         final double h = a / b;
@@ -126,11 +133,11 @@ public final class LogBeta {
     private static double sumDeltaMinusDeltaSum(final double p,
                                                 final double q) {
 
-        if (p < 10) {
-            throw new GammaException(GammaException.OUT_OF_RANGE, p, 10, Double.POSITIVE_INFINITY);
+        if (p < TEN) {
+            throw new GammaException(GammaException.OUT_OF_RANGE, p, TEN, Double.POSITIVE_INFINITY);
         }
-        if (q < 10) {
-            throw new GammaException(GammaException.OUT_OF_RANGE, q, 10, Double.POSITIVE_INFINITY);
+        if (q < TEN) {
+            throw new GammaException(GammaException.OUT_OF_RANGE, q, TEN, Double.POSITIVE_INFINITY);
         }
 
         final double a = Math.min(p, q);
@@ -165,7 +172,7 @@ public final class LogBeta {
 
         final double a = Math.min(p, q);
         final double b = Math.max(p, q);
-        if (a >= 10) {
+        if (a >= TEN) {
             final double w = sumDeltaMinusDeltaSum(a, b);
             final double h = a / b;
             final double c = h / (1 + h);
@@ -176,8 +183,8 @@ public final class LogBeta {
             } else {
                 return (((-0.5 * Math.log(b) + HALF_LOG_TWO_PI) + w) - v) - u;
             }
-        } else if (a > 2) {
-            if (b > 1000) {
+        } else if (a > TWO) {
+            if (b > THOUSAND) {
                 final int n = (int) Math.floor(a - 1);
                 double prod = 1;
                 double ared = a;
@@ -196,7 +203,7 @@ public final class LogBeta {
                     final double h = ared / b;
                     prod1 *= h / (1 + h);
                 }
-                if (b < 10) {
+                if (b < TEN) {
                     double prod2 = 1;
                     double bred = b;
                     while (bred > 2) {
@@ -215,8 +222,8 @@ public final class LogBeta {
                 }
             }
         } else if (a >= 1) {
-            if (b > 2) {
-                if (b < 10) {
+            if (b > TWO) {
+                if (b < TEN) {
                     double prod = 1;
                     double bred = b;
                     while (bred > 2) {
@@ -237,7 +244,7 @@ public final class LogBeta {
                        LogGammaSum.value(a, b);
             }
         } else {
-            if (b >= 10) {
+            if (b >= TEN) {
                 return LogGamma.value(a) +
                        logGammaMinusLogGammaSum(a, b);
             } else {
@@ -265,8 +272,8 @@ public final class LogBeta {
         if (a < 0) {
             throw new GammaException(GammaException.OUT_OF_RANGE, a, 0, Double.POSITIVE_INFINITY);
         }
-        if (b < 10) {
-            throw new GammaException(GammaException.OUT_OF_RANGE, b, 10, Double.POSITIVE_INFINITY);
+        if (b < TEN) {
+            throw new GammaException(GammaException.OUT_OF_RANGE, b, TEN, Double.POSITIVE_INFINITY);
         }
 
         /*
diff --git a/src/main/resources/pmd/pmd-ruleset.xml b/src/main/resources/pmd/pmd-ruleset.xml
index e3b6b26..55ef27b 100644
--- a/src/main/resources/pmd/pmd-ruleset.xml
+++ b/src/main/resources/pmd/pmd-ruleset.xml
@@ -94,6 +94,11 @@
   <rule ref="category/java/errorprone.xml/AvoidLiteralsInIfCondition">
     <properties>
       <property name="ignoreMagicNumbers" value="-1,0,1" />
+      <property name="violationSuppressXPath"
+        value="//ClassOrInterfaceDeclaration[@Image='LogGamma'
+               or @Image='LogGammaSum' 
+               or @Image='InverseErf' 
+               or @Image='InvGamma1pm1']"/>
     </properties>
   </rule>