Python math.exp2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.11+

Definition

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)
CallWhat 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

ParameterDescription
xRequired positional real exponent (int or float).

Return Value

Return ValueDescription
float2 ** 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")

How It Works

  1. Octave steps. Each unit increment in octave doubles the frequency via math.exp2(octave).
  2. Fractional step. An exponent of 0.5 computes the geometric midpoint (tritone) rounded to two decimal places.
  3. 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}")

How It Works

  1. Runtime capability check. Checking hasattr(math, "exp2") calls the 3.11+ function when available.
  2. Float-preserving fallback. Using 2.0 ** x guarantees a float return on Python 3.8 through 3.10 just like math.exp2().
  3. 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")

How It Works

  1. Exact binary multiples. Ten bits yield 1024.0 and minus three yields exact binary fraction 0.125.
  2. Underflow to zero. Passing negative infinity collapses cleanly to 0.0.
  3. 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.

Next: math.expm1()

Return exp(x) - 1 accurately for values near zero.

expm1() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful