Skip to content

gamma

gamma(x): number

Defined in: packages/math-analysis/src/gamma.ts:39

The gamma function Γ(x) — the factorial, extended to every real number.

Γ(n) = (n − 1)! at each positive integer, and it joins those points with a smooth curve, so a formula that “should” contain a factorial can be handed 2.5 as readily as 3. That is why it turns up in the normalizing constant of almost every continuous probability distribution.

Reach for logGamma the moment the result might not fit: Γ passes the top of binary64 at about x = 171.6. Reach for beta rather than writing gamma(a) * gamma(b) / gamma(a + b) yourself, which overflows long before the ratio it computes would.

Relative error is within about six ulps for ordinary arguments and stays under 5e-13 at the extremes of the representable range.

O(1) time, O(1) memory — a fixed nine-term series, plus at most one reflection step for arguments below 0.5.

number

Any real other than a non-positive integer.

number

Γ(x); ±Infinity where the true value exceeds binary64, and NaN at the non-positive integers, where Γ has a pole whose one-sided limits disagree in sign.

gamma(5); // 23.999999999999986 — 4!, to within four ulps
gamma(0.5); // 1.7724538509055152 — √π
gamma(-1); // NaN — a pole
gamma(172); // Infinity — past the binary64 range
```;
## See
- [logGamma](logGamma.md) for arguments whose Γ overflows.
- [The gamma and beta functions](https://anthochamp.pages.grenadine.io/node-essentials/topics/math/functions/gamma/)