You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/10/22 22:27:13 UTC

[math] [MATH-1283] Fixed Gamma#gamma function for values smaller than -20. Thanks to Jean Noel Delavalade

Repository: commons-math
Updated Branches:
  refs/heads/master 260a4392a -> 9e0c5ad4b


[MATH-1283] Fixed Gamma#gamma function for values smaller than -20. Thanks to Jean Noel Delavalade


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/9e0c5ad4
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/9e0c5ad4
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/9e0c5ad4

Branch: refs/heads/master
Commit: 9e0c5ad4b3148f42f3940faf7d67b305e34bef0d
Parents: 260a439
Author: Thomas Neidhart <th...@gmail.com>
Authored: Thu Oct 22 22:26:47 2015 +0200
Committer: Thomas Neidhart <th...@gmail.com>
Committed: Thu Oct 22 22:26:47 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                              |  3 +++
 .../java/org/apache/commons/math4/special/Gamma.java |  2 +-
 .../org/apache/commons/math4/special/GammaTest.java  | 15 +++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/9e0c5ad4/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1fb00e1..e18ba9f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="tn" type="fix" issue="MATH-1283" due-to="Jean Noel Delavalade"> <!-- backported to 3.6 -->
+        Fixed "Gamma#gamma(double)" for negative values smaller than -20.
+      </action>
       <action dev="tn" type="fix" issue="MATH-1237" due-to="Ken Williams"> <!-- backported to 3.6 -->
         Fixed javadoc of methods {floorDiv,floorMod} in class "FastMath".
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/9e0c5ad4/src/main/java/org/apache/commons/math4/special/Gamma.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/special/Gamma.java b/src/main/java/org/apache/commons/math4/special/Gamma.java
index aa0e90c..f390f7c 100644
--- a/src/main/java/org/apache/commons/math4/special/Gamma.java
+++ b/src/main/java/org/apache/commons/math4/special/Gamma.java
@@ -695,7 +695,7 @@ public class Gamma {
             }
         } else {
             final double y = absX + LANCZOS_G + 0.5;
-            final double gammaAbs = SQRT_TWO_PI / x *
+            final double gammaAbs = SQRT_TWO_PI / absX *
                                     FastMath.pow(y, absX + 0.5) *
                                     FastMath.exp(-y) * lanczos(absX);
             if (x > 0.0) {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/9e0c5ad4/src/test/java/org/apache/commons/math4/special/GammaTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/special/GammaTest.java b/src/test/java/org/apache/commons/math4/special/GammaTest.java
index 35da9fe..51982a3 100644
--- a/src/test/java/org/apache/commons/math4/special/GammaTest.java
+++ b/src/test/java/org/apache/commons/math4/special/GammaTest.java
@@ -974,6 +974,21 @@ public class GammaTest {
         }
     }
 
+    @Test
+    public void testGammaNegativeDouble() {
+        // check that the gamma function properly switches sign
+        // see: https://en.wikipedia.org/wiki/Gamma_function
+
+        double previousGamma = Gamma.gamma(-18.5);
+        for (double x = -19.5; x > -25; x -= 1.0) {
+            double gamma = Gamma.gamma(x);
+            Assert.assertEquals(  (int) FastMath.signum(previousGamma),
+                                - (int) FastMath.signum(gamma));
+
+            previousGamma = gamma;
+        }
+    }
+
     private void checkRelativeError(String msg, double expected, double actual,
                                     double tolerance) {