Python Glossary Русский

all Python 3mathterm

math.log

logarithms

The natural logarithm. log(x, base) is the logarithm to the given base.

Syntax

math.log(x[, base])

Zero or a negative argument raises ValueError ("math domain error") instead of returning -inf or nan; use cmath.log if you want a complex result. The two-argument form is computed as log(x) / log(base), so it rounds twice: math.log(1000, 10) gives 2.9999999999999996, not 3.0. Prefer math.log10 and math.log2 for those bases, and never compare a logarithm with == .

Examples

import math
print(math.log(math.e))  # → 1.0
print(math.log(1))       # → 0.0
print(math.log(100, 10)) # → 2.0
print(math.log(8, 2))    # → 3.0
print(math.log(1000))    # → 6.907...
print(math.log(2))       # → 0.6931...

See also

Open in the full glossarydocs.python.orgReport an inaccuracy