Reference · Math
math.gamma
math.gamma(num) -> floatWhat it does
The gamma function, as Python's math.gamma: the factorial extended to any number, as
a float, with math.gamma(n) equal to (n - 1)! for a positive int n.
math.gamma(5) is 24.0, math.gamma(0.5) is the square root of pi. It reaches past
where math.factorial overflows an int, up to
math.gamma(171), which is 170!.
Examples
print(math.gamma(5), math.gamma(1), math.gamma(6) == math.factorial(5))print(math.gamma(0.5), math.gamma(1.5), math.gamma(10.5))24.0 1.0 true1.772453850905516 0.886226925452758 1133278.3889487854Edge cases
- Negative non-integers have a value:
math.gamma(-0.5)is about-3.545, andmath.gamma(-1.5)is positive again. - The answer is a float with a float's precision:
math.gamma(0.5) * math.gamma(0.5)is pi to the last digit or so, not exactly. - Values near zero grow without bound:
math.gamma(1e-300)is about1e300. - Plus infinity gives infinity, not an error; NaN stays NaN.
print(math.gamma(-0.5), math.gamma(-1.5), math.gamma(171) > 1e306)print(math.isclose(math.gamma(0.5) * math.gamma(0.5), math.pi))print(math.gamma(float("inf")), math.gamma(float("nan")))-3.5449077018110318 2.363271801207355 truetrueinf NaNErrors
Zero and the negative ints are the poles: math.gamma(0) and math.gamma(-2) are a
runtime error, math domain error, not an error value and not infinity, as Python's
ValueError. So is minus infinity. A result too large for a float, math.gamma(172)
and up, is a runtime error, math range error; take math.lgamma
instead. An argument that is not a number is a runtime error as well.
print(math.gamma(0))line 1: `math.gamma`: math domain error (0)print(math.gamma(-2))line 1: `math.gamma`: math domain error (-2)print(math.gamma(172))line 1: `math.gamma`: math range error (172)Chaining
In a pipeline the number comes first:
print(5 | math.gamma)print([1, 2, 3, 4, 5] | map(math.gamma))24.0[1.0, 1.0, 2.0, 6.0, 24.0]See also
math.lgamma, math.factorial, math.erf, math.exp
Thread 0 posts
On math.gamma(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo