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)
Call
What 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
Parameter
Description
x
Required positional real number (int or float).
i
Required positional integer power of two (int only).
Return Value
Return Value
Description
float
x * 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))
Output
8.0
0.75
1536.0
-8.0
How It Works
Left shift by four.0.5 * 16 equals exact 8.0.
Negative exponent divides. Shifting 3.0 down by two powers of two yields 0.75.
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}")
Exact binary inversion. Splitting into normalized mantissa and integer exponent discards zero bits.
Direct tuple unpacking. Passing m, e into ldexp() shifts the mantissa back cleanly.
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")
Output
0.0
ldexp(1.0, 2000) raises OverflowError
exponent i must be an integer
How It Works
Silent underflow. Exponent -2000 drops far below subnormal minimum and rounds to 0.0.
Overflow raises OverflowError. Exponent 2000 pushes past 1023, raising OverflowError.
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).