math.exp2(x) returns 2 raised to the power x as a float, using dedicated base-2 exponential routines for accuracy.
It is the direct inverse of math.log2(). While integer powers like 1 << n give exact arbitrary-precision ints, math.exp2(x) also accepts fractional and negative exponents.
math.exp2() is a function in Python's standard math module: import it with import math and call math.exp2(...), or import the name directly with from math import exp2. Added in Python 3.11.
Syntax
Import math and pass the power of two (Python 3.11+).
python
import math
value = math.exp2(x)
Call
What it does
math.exp2(3)
Returns 8.0.
math.exp2(-2)
Returns 0.25.
math.exp2(0.5)
Returns approximately 1.4142....
math.exp2(1024)
Raises OverflowError: exceeds float range.
Added in Python 3.11; use hasattr(math, "exp2") to fall back to 2.0 ** x when supporting Python 3.10 or older.
Function signatures
python
math.exp2(x, /)
# added in Python 3.11; returns float 2 ** x
Parameters
Parameter
Description
x
Required positional real exponent (int or float).
Return Value
Return Value
Description
float
2 ** x. Returns inf for inf, 0.0 for -inf, and nan for nan.
Python Examples
Example 1 — Scale Musical Octave Frequencies From A4
Double or halve the 440 Hz reference pitch across octaves using math.exp2() (Python 3.11+).
python
import math
base_hz = 440.0
for octave in [-1, 0, 1, 2]:
freq = base_hz * math.exp2(octave)
print(f"octave {octave:+d}: {freq:.1f} Hz")
print(f"half-octave: {base_hz * math.exp2(0.5):.2f} Hz")
Octave steps. Each unit increment in octave doubles the frequency via math.exp2(octave).
Fractional step. An exponent of 0.5 computes the geometric midpoint (tritone) rounded to two decimal places.
Reference unchanged. At octave = 0, math.exp2(0) equals 1.0, returning 440.0 Hz.
Example 2 — Compute Half-Life Decay Safely Across Versions
Guard math.exp2() with hasattr and fall back to 2.0 ** x on older Python releases.
python
import math
def pow2_float(x):
if hasattr(math, "exp2"):
return math.exp2(x)
return 2.0 ** x
for half_lives in [0, 1, 2, 3, -2]:
fraction = pow2_float(-half_lives)
print(f"after {half_lives:>2} half-lives: {fraction:.4f}")
Output
after 0 half-lives: 1.0000
after 1 half-lives: 0.5000
after 2 half-lives: 0.2500
after 3 half-lives: 0.1250
after -2 half-lives: 4.0000
How It Works
Runtime capability check. Checking hasattr(math, "exp2") calls the 3.11+ function when available.
Float-preserving fallback. Using 2.0 ** x guarantees a float return on Python 3.8 through 3.10 just like math.exp2().
Negative exponent sign. Passing -2 flips the negation to 2.0 ** 2, yielding 4.0000.
Example 3 — Test Exact Binary Powers and Float Overflow
Inspect how math.exp2 handles negative infinity and raises OverflowError at 1024.
python
import math
exp2_fn = getattr(math, "exp2", lambda x: 2.0 ** x)
print(exp2_fn(10), exp2_fn(-3))
print(exp2_fn(float("-inf")))
try:
math.pow(2.0, 1024) if not hasattr(math, "exp2") else math.exp2(1024)
except OverflowError:
print("2**1024 overflows 64-bit float")
Output
1024.0 0.125
0.0
2**1024 overflows 64-bit float
How It Works
Exact binary multiples. Ten bits yield 1024.0 and minus three yields exact binary fraction 0.125.
Underflow to zero. Passing negative infinity collapses cleanly to 0.0.
Exponent cap at 1024. Since the maximum IEEE 754 binary64 exponent is 1023, 1024 triggers OverflowError.
Frequently Asked Questions
math.exp2() was added in Python 3.11. Older Python versions require 2.0 ** x or math.pow(2.0, x).
Any finite exponent of 1024.0 or greater exceeds the maximum binary64 exponent (1023) and raises OverflowError.
1 << n computes an exact integer of arbitrary size for non-negative integer n. math.exp2(x) returns a float and supports negative and fractional exponents.
C library exp2 implementations are tuned for sub-ULP accuracy on fractional exponents, whereas 2 ** x uses general power code.
math.log2(y) reverses math.exp2(x) for positive finite values.