commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aherb...@apache.org
Subject [commons-numbers] 02/02: Fixed RegularizedGamma.P to confine to the interval [0, 1]
Date Mon, 06 Apr 2020 13:32:20 GMT
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 2ba8f9e122b63789ba17b36cb231a5e084d6d0cb
Author: aherbert <aherbert@apache.org>
AuthorDate: Mon Apr 6 14:32:08 2020 +0100

    Fixed RegularizedGamma.P to confine to the interval [0, 1]
---
 .../main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java  | 4 +++-
 .../java/org/apache/commons/numbers/gamma/RegularizedGammaTest.java   | 2 --
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
index bfefa50..448c8ea 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/RegularizedGamma.java
@@ -119,7 +119,9 @@ public final class RegularizedGamma {
                 } else if (Double.isInfinite(sum)) {
                     return 1;
                 } else {
-                    return Math.exp(-x + (a * Math.log(x)) - LogGamma.value(a)) * sum;
+                    // Ensure result is in the range [0, 1]
+                    final double result = Math.exp(-x + (a * Math.log(x)) - LogGamma.value(a))
* sum;
+                    return result > 1.0 ? 1.0 : result;
                 }
             }
         }
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedGammaTest.java
b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedGammaTest.java
index 5a38723..ca8e895 100644
--- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedGammaTest.java
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedGammaTest.java
@@ -17,7 +17,6 @@
 package org.apache.commons.numbers.gamma;
 
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -59,7 +58,6 @@ public class RegularizedGammaTest {
         testRegularizedGamma(0.632120558828558, 1.0, 1.0);
     }
 
-    @Disabled
     @Test
     public void testRegularizedGammaPWithACloseToZero() {
         // Creates a case where the regularized gamma P series is evaluated and the


Mime
View raw message