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

This commit is contained in:
Thomas Neidhart 2015-10-22 22:32:50 +02:00
parent 0dd621687d
commit 40f35da565
3 changed files with 19 additions and 1 deletions

View File

@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties> </properties>
<body> <body>
<release version="3.6" date="XXXX-XX-XX" description=""> <release version="3.6" date="XXXX-XX-XX" description="">
<action dev="tn" type="fix" issue="MATH-1283" due-to="Jean Noel Delavalade">
Fixed "Gamma#gamma(double)" for negative values smaller than -20.
</action>
<action dev="tn" type="fix" issue="MATH-1237" due-to="Ken Williams"> <action dev="tn" type="fix" issue="MATH-1237" due-to="Ken Williams">
Fixed javadoc of methods {floorDiv,floorMod} in class "FastMath". Fixed javadoc of methods {floorDiv,floorMod} in class "FastMath".
</action> </action>

View File

@ -695,7 +695,7 @@ public class Gamma {
} }
} else { } else {
final double y = absX + LANCZOS_G + 0.5; 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.pow(y, absX + 0.5) *
FastMath.exp(-y) * lanczos(absX); FastMath.exp(-y) * lanczos(absX);
if (x > 0.0) { if (x > 0.0) {

View File

@ -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, private void checkRelativeError(String msg, double expected, double actual,
double tolerance) { double tolerance) {