Describe the bug
lgammaNumber incorrectly returns NaN for all n < 0.
To Reproduce
- Call
mathjs.log(mathjs.gamma(-1.5)) and get 0.8600470153764809.
- Call
mathjs.lgamma(-1.5) and get NaN.
- See that the returned values are not equal.
Problematic code
|
export function lgammaNumber (n) { |
|
if (n < 0) return NaN |
Suggested solution
Change the code
to
Math.log(Math.PI / Math.sin(Math.PI * n)) - lgammaNumber(1 - n)
It correctly returns NaN for $n \in (-2k-1, -2k)$ and positive values for $n \in (-2k, -2k+1)$ for $k \in ℕ$.
You can check it for -1.5 too: Math.log(Math.PI / Math.sin(Math.PI * -1.5)) - mathjs.lgamma(1 - -1.5) returns 0.8600470153764859, which is close to 0.8600470153764809.
You can also just remove this if statement, because the one below already handles the n < 0 case properly:
|
if (n < 0.5) { |
|
// Use Euler's reflection formula: |
|
// gamma(z) = PI / (sin(PI * z) * gamma(1 - z)) |
|
return Math.log(Math.PI / Math.sin(Math.PI * n)) - lgammaNumber(1 - n) |
|
} |
Describe the bug
lgammaNumberincorrectly returnsNaNfor alln < 0.To Reproduce
mathjs.log(mathjs.gamma(-1.5))and get0.8600470153764809.mathjs.lgamma(-1.5)and getNaN.Problematic code
mathjs/src/plain/number/probability.js
Lines 91 to 92 in 4bbe862
Suggested solution
Change the code
to
It correctly returns$n \in (-2k-1, -2k)$ and positive values for $n \in (-2k, -2k+1)$ for $k \in ℕ$ .
NaNforYou can check it for
-1.5too:Math.log(Math.PI / Math.sin(Math.PI * -1.5)) - mathjs.lgamma(1 - -1.5)returns0.8600470153764859, which is close to0.8600470153764809.You can also just remove this if statement, because the one below already handles the
n < 0case properly:mathjs/src/plain/number/probability.js
Lines 96 to 100 in 4bbe862