Python Glossary Русский

Python 3.2+Numbers & mathfunction

math.lgamma

special functions

The natural log of the absolute value of the gamma function.

Syntax

math.lgamma(...)

This is the tool for the range where gamma itself overflows: work with log-factorials and log-binomials by adding and subtracting them, and apply exp only at the very end. Note it is the log of the absolute value, so the sign of gamma (negative on parts of the negative axis) is lost, and zero and negative integers raise ValueError.

Examples

import math
print(math.lgamma(1))   # → 0.0
print(round(math.lgamma(5), 4))   # → 3.1781
print(round(math.exp(math.lgamma(6))))   # → 120
print(round(math.lgamma(1000), 2))   # → 5905.22
print(math.lgamma(0))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy