Python Glossary Русский

Python 3.2+Numbers & mathfunction

math.expm1

roots/powers

exp(x) − 1, accurate for small x.

Syntax

math.expm1(...)

The problem it removes: for tiny x, exp(x) sits so close to 1 that subtracting 1 cancels the leading digits and leaves only a couple of significant ones — expm1 computes the difference directly and keeps full precision. Its counterpart math.log1p(x) computes log(1+x) and inverts it; for large x there is nothing to gain, and it overflows exactly like exp.

Examples

import math
print(math.expm1(0))   # → 0.0
print(round(math.expm1(1), 6))   # → 1.718282
print(round(math.expm1(0.005), 6))   # → 0.005013
print(math.exp(1e-16) - 1)   # → 0.0
print(math.expm1(1e-16))   # → 1e-16

See also

Open in the full glossarydocs.python.orgReport an inaccuracy