Python math.ldexp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.ldexp(x, i) computes x * (2 ** i) and returns a float, effectively shifting the binary exponent of x by integer i.

It is the exact inverse of math.frexp(): for any finite float, math.ldexp(*math.frexp(x)) == x.

math.ldexp() is a function in Python's standard math module: import it with import math and call math.ldexp(...), or import the name directly with from math import ldexp.

Syntax

Import math and pass float/int x and integer exponent i.

python
import math

scaled = math.ldexp(x, i)
CallWhat it does
math.ldexp(0.5, 4)Returns 8.0.
math.ldexp(3.0, -2)Returns 0.75.
math.ldexp(1.0, -2000)Returns 0.0 (underflows smoothly).
math.ldexp(1.0, 2000)Raises OverflowError: result exceeds float range.

Notice i must be an int; passing a float exponent like 2.5 raises TypeError.

Function signatures
python
math.ldexp(x, i, /)
# returns: float x * (2 ** i); i must be an int

Parameters

ParameterDescription
xRequired positional real number (int or float).
iRequired positional integer power of two (int only).

Return Value

Return ValueDescription
floatx * 2**i, preserving -0.0, inf, and nan.

Python Examples

Example 1 — Scale Measurements by Binary Shifts Without Rounding

Multiply and divide numbers by exact powers of two using math.ldexp().

python
import math

print(math.ldexp(0.5, 4))
print(math.ldexp(3.0, -2))
print(math.ldexp(1.5, 10))
print(math.ldexp(-1.0, 3))

How It Works

  1. Left shift by four. 0.5 * 16 equals exact 8.0.
  2. Negative exponent divides. Shifting 3.0 down by two powers of two yields 0.75.
  3. Kilobyte scaling. Multiplying 1.5 by 2**10 gives exact 1536.0.

Example 2 — Reconstruct Floats Exactly From frexp() Components

Decompose three numbers with math.frexp() and rebuild them identically with math.ldexp().

python
import math

for val in [0.125, 12.5, -42.25]:
    m, e = math.frexp(val)
    rebuilt = math.ldexp(m, e)
    print(f"{val:>6} -> ({m}, {e}) -> same={rebuilt == val}")

How It Works

  1. Exact binary inversion. Splitting into normalized mantissa and integer exponent discards zero bits.
  2. Direct tuple unpacking. Passing m, e into ldexp() shifts the mantissa back cleanly.
  3. Bit-for-bit equality. == confirms exact identity without needing tolerances.

Example 3 — Observe Underflow to Zero, OverflowError, and TypeError

See extreme negative shifts vanish to 0.0 while positive overflow and float exponents raise.

python
import math

print(math.ldexp(1.0, -2000))

try:
    math.ldexp(1.0, 2000)
except OverflowError:
    print("ldexp(1.0, 2000) raises OverflowError")

try:
    math.ldexp(1.0, 2.5)
except TypeError:
    print("exponent i must be an integer")

How It Works

  1. Silent underflow. Exponent -2000 drops far below subnormal minimum and rounds to 0.0.
  2. Overflow raises OverflowError. Exponent 2000 pushes past 1023, raising OverflowError.
  3. Strict integer exponent. Passing float 2.5 as i raises TypeError.

Frequently Asked Questions

It multiplies x by 2 raised to integer power i (x * 2**i) directly by adjusting the binary exponent, returning a float.
When i is negative and huge (like -2000), 2 ** (-2000) underflows to 0.0 before multiplying, whereas ldexp(1e300, -1000) adjusts exponents directly without losing precision.
TypeError, because the second argument i must be an integer.
Whenever x is finite and nonzero and x * 2**i exceeds the largest binary64 float (~1.8e308).
No. Zero times any power of two stays 0.0 (and -0.0 stays -0.0).

Next: math.lgamma()

Return natural log of the absolute value of Gamma(x).

lgamma() →

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.

12 people found this page helpful