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 43c3ebe664f401aadcb3853177ec9de0a67aa138 Author: aherbert <[email protected]> AuthorDate: Wed May 4 18:06:33 2022 +0100 Sonar fix: Use constants in place of methods for constant values --- .../apache/commons/numbers/gamma/BoostBeta.java | 18 ++++----- .../apache/commons/numbers/gamma/BoostGamma.java | 44 +++++++++------------- .../commons/numbers/gamma/BoostGammaTest.java | 6 +-- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostBeta.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostBeta.java index d2c41f12..8ecc7637 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostBeta.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostBeta.java @@ -132,9 +132,9 @@ final class BoostBeta { final double b = p < q ? p : q; // Lanczos calculation: - final double agh = a + BoostGamma.Lanczos.gmh(); - final double bgh = b + BoostGamma.Lanczos.gmh(); - final double cgh = c + BoostGamma.Lanczos.gmh(); + final double agh = a + BoostGamma.Lanczos.GMH; + final double bgh = b + BoostGamma.Lanczos.GMH; + final double cgh = c + BoostGamma.Lanczos.GMH; double result = BoostGamma.Lanczos.lanczosSumExpGScaled(a) * (BoostGamma.Lanczos.lanczosSumExpGScaled(b) / BoostGamma.Lanczos.lanczosSumExpGScaled(c)); final double ambh = a - 0.5f - b; @@ -264,9 +264,9 @@ final class BoostBeta { final double c = a + b; // combine power terms with Lanczos approximation: - final double agh = a + BoostGamma.Lanczos.gmh(); - final double bgh = b + BoostGamma.Lanczos.gmh(); - final double cgh = c + BoostGamma.Lanczos.gmh(); + final double agh = a + BoostGamma.Lanczos.GMH; + final double bgh = b + BoostGamma.Lanczos.GMH; + final double cgh = c + BoostGamma.Lanczos.GMH; result = BoostGamma.Lanczos.lanczosSumExpGScaled(c) / (BoostGamma.Lanczos.lanczosSumExpGScaled(a) * BoostGamma.Lanczos.lanczosSumExpGScaled(b)); result *= prefix; @@ -839,9 +839,9 @@ final class BoostBeta { final double c = a + b; // incomplete beta power term, combined with the Lanczos approximation: - final double agh = a + BoostGamma.Lanczos.gmh(); - final double bgh = b + BoostGamma.Lanczos.gmh(); - final double cgh = c + BoostGamma.Lanczos.gmh(); + final double agh = a + BoostGamma.Lanczos.GMH; + final double bgh = b + BoostGamma.Lanczos.GMH; + final double cgh = c + BoostGamma.Lanczos.GMH; result = BoostGamma.Lanczos.lanczosSumExpGScaled(c) / (BoostGamma.Lanczos.lanczosSumExpGScaled(a) * BoostGamma.Lanczos.lanczosSumExpGScaled(b)); diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostGamma.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostGamma.java index 9356852d..bec83a50 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostGamma.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostGamma.java @@ -329,6 +329,18 @@ final class BoostGamma { // Generated with compiler: Microsoft Visual C++ version 8.0 on Win32 at Mar 23 2006 // + /** + * Lanczos constant G. + */ + static final double G = 6.024680040776729583740234375; + + /** + * Lanczos constant G - half. + * + * <p>Note: The form {@code (g - 0.5)} is used when computing the gamma function. + */ + static final double GMH = 5.524680040776729583740234375; + /** Common denominator used for the rational evaluation. */ private static final int[] DENOM = { 0, @@ -351,26 +363,6 @@ final class BoostGamma { // intentionally empty. } - /** - * Lanczos constant G. - * - * @return the Lanczos constant. - */ - static double g() { - return 6.024680040776729583740234375; - } - - /** - * Lanczos constant G - half. - * - * <p>Note: The form {@code (g - 0.5)} is used when computing the gamma function. - * - * @return the Lanczos constant - 0.5. - */ - static double gmh() { - return 5.524680040776729583740234375; - } - /** * Computes the Lanczos approximation. * @@ -675,7 +667,7 @@ final class BoostGamma { } double result = Lanczos.lanczosSum(z); - final double zgh = z + Lanczos.gmh(); + final double zgh = z + Lanczos.GMH; final double lzgh = Math.log(zgh); if (z * lzgh > LOG_MAX_VALUE) { // we're going to overflow unless this is done with care: @@ -806,7 +798,7 @@ final class BoostGamma { result = Math.log(tgamma(z)); } else { // regular evaluation: - final double zgh = z + Lanczos.gmh(); + final double zgh = z + Lanczos.GMH; result = Math.log(zgh) - 1; result *= z - 0.5f; // @@ -1778,7 +1770,7 @@ final class BoostGamma { // gives the greatest accuracy // - final double agh = a + Lanczos.gmh(); + final double agh = a + Lanczos.GMH; double prefix; final double factor = Math.sqrt(agh / Math.E) / Lanczos.lanczosSumExpGScaled(a); @@ -1787,13 +1779,13 @@ final class BoostGamma { // Lower threshold for large a from 150 to 128 and compute d on demand. // See NUMBERS-179. if (a > 128) { - final double d = ((z - a) - Lanczos.gmh()) / agh; + final double d = ((z - a) - Lanczos.GMH) / agh; if (Math.abs(d * d * a) <= 100) { // special case for large a and a ~ z. // When a and x are large, we end up with a very large exponent with a base near one: // this will not be computed accurately via the pow function, and taking logs simply // leads to cancellation errors. - prefix = a * SpecialMath.log1pmx(d) + z * -Lanczos.gmh() / agh; + prefix = a * SpecialMath.log1pmx(d) + z * -Lanczos.GMH / agh; prefix = Math.exp(prefix); return prefix * factor; } @@ -2170,7 +2162,7 @@ final class BoostGamma { } return 1 / (z * tgamma(z + delta)); } - final double zgh = z + Lanczos.gmh(); + final double zgh = z + Lanczos.GMH; double result; if (z + delta == z) { // Here: diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostGammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostGammaTest.java index 018a79d1..82fea674 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostGammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostGammaTest.java @@ -565,8 +565,8 @@ class BoostGammaTest { @Test void testLanczosGmh() { // These terms are equal. The value (g - 0.5) is provided as a convenience. - Assertions.assertEquals(Lanczos.g() - 0.5, Lanczos.gmh()); - Assertions.assertEquals(0.5 - Lanczos.g(), -Lanczos.gmh()); + Assertions.assertEquals(Lanczos.G - 0.5, Lanczos.GMH); + Assertions.assertEquals(0.5 - Lanczos.G, -Lanczos.GMH); } @ParameterizedTest @@ -1441,7 +1441,7 @@ class BoostGammaTest { result *= 1 / z - EULER; } else { result *= Lanczos.lanczosSum(z); - final double zgh = z + Lanczos.g() - 0.5; + final double zgh = z + Lanczos.G - 0.5; final double lzgh = Math.log(zgh); if (z * lzgh > LOG_MAX_VALUE) { // we're going to overflow unless this is done with care:
