diff --git a/src/main/java/org/apache/commons/math3/special/Gamma.java b/src/main/java/org/apache/commons/math3/special/Gamma.java index 65bee5af0..be669f1e0 100644 --- a/src/main/java/org/apache/commons/math3/special/Gamma.java +++ b/src/main/java/org/apache/commons/math3/special/Gamma.java @@ -706,7 +706,9 @@ public class Gamma { */ public static double gamma(final double x) { - // TODO Check whether x is a negative integer + if ((x == FastMath.rint(x)) && (x <= 0.0)) { + return Double.NaN; + } final double ret; final double absX = FastMath.abs(x); diff --git a/src/test/java/org/apache/commons/math3/special/GammaTest.java b/src/test/java/org/apache/commons/math3/special/GammaTest.java index 205655d79..8c1a29289 100644 --- a/src/test/java/org/apache/commons/math3/special/GammaTest.java +++ b/src/test/java/org/apache/commons/math3/special/GammaTest.java @@ -954,6 +954,14 @@ public class GammaTest { } } + @Test + public void testGammaNegativeInteger() { + + for (int i = -100; i <= 0; i++) { + Assert.assertTrue(Integer.toString(i), Double.isNaN(Gamma.gamma(i))); + } + } + /** * Reference data for the {@link Gamma#logGammaSum(double, double)} * function. This data was generated with the following