RosettaCodeData/Task/Gamma-function/Zkl/gamma-function-2.zkl

29 lines
883 B
Plaintext

fcn lanczosGamma(z) { z = z.toFloat();
// Coefficients used by the GNU Scientific Library.
// http://en.wikipedia.org/wiki/Lanczos_approximation
const g = 7, PI = (0.0).pi;
exp := (0.0).e.pow;
var table = T(
0.99999_99999_99809_93,
676.52036_81218_851,
-1259.13921_67224_028,
771.32342_87776_5313,
-176.61502_91621_4059,
12.50734_32786_86905,
-0.13857_10952_65720_12,
9.98436_95780_19571_6e-6,
1.50563_27351_49311_6e-7);
// Reflection formula.
if (z < 0.5) {
return(PI / ((PI * z).sin() * lanczosGamma(1.0 - z)));
} else {
z -= 1;
x := table[0];
foreach i in ([1 .. g + 1]){
x += table[i] / (z + i); }
t := z + g + 0.5;
return((2.0 * PI).sqrt() * t.pow(z + 0.5) * exp(-t) * x);
}
}