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 2020/04/06 13:32:20 UTC
[commons-numbers] 02/02: Fixed RegularizedGamma.P to confine to the
interval [0, 1]
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 <ah...@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