Python Glossary Русский

Python 3.11+Numbers & mathfunction

math.exp2

roots/powers

Return 2 raised to the power x as a float (Python 3.11+).

Syntax

math.exp2(...)

For whole exponents stick with 2 ** n: it gives an exact int of any size, while exp2 always returns a float and raises OverflowError once the value no longer fits. exp2 earns its place with fractional exponents and numeric code that wants a single correctly rounded step instead of exp(x * log(2)).

Examples

import math
print(math.exp2(3))   # → 8.0
print(math.exp2(-1))   # → 0.5
print(round(math.exp2(0.5), 6))   # → 1.414214
print(math.exp2(8))   # → 256.0
print(2 ** 10, math.exp2(10))   # → 1024 1024.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy