Reference · Math

math.gamma

signature
math.gamma(num) -> float

What 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))
output
24.0 1.0 true1.772453850905516 0.886226925452758 1133278.3889487854

Edge cases

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")))
output
-3.5449077018110318 2.363271801207355 truetrueinf NaN

Errors

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.

this one fails
print(math.gamma(0))
output
line 1: `math.gamma`: math domain error (0)
this one fails
print(math.gamma(-2))
output
line 1: `math.gamma`: math domain error (-2)
this one fails
print(math.gamma(172))
output
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))
output
24.0[1.0, 1.0, 2.0, 6.0, 24.0]

See also

math.lgamma, math.factorial, math.erf, math.exp

Thread
8Useful? Like it, or add to its thread below.

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