Python math.frexp() Function

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

Definition

math.frexp(x) decomposes x into a tuple (m, e) where m is a float and e is an int such that x == m * (2 ** e).

For every finite nonzero number, 0.5 <= abs(m) < 1.0. Together with its inverse math.ldexp(), it gives clean access to IEEE 754 binary exponents.

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

Syntax

Import math and unpack the (mantissa, exponent) pair.

python
import math

m, e = math.frexp(x)
CallWhat it does
math.frexp(8.0)Returns (0.5, 4) (0.5 * 2**4 == 8.0).
math.frexp(1.0)Returns (0.5, 1).
math.frexp(0.0)Returns (0.0, 0).
math.frexp(float("inf"))Returns (inf, 0).

Notice the normalized mantissa lies in [0.5, 1.0) rather than [1.0, 2.0). Recombine m and e exactly with math.ldexp(m, e).

Function signatures
python
math.frexp(x, /)
# returns: (m: float, e: int) with x == m * 2**e

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
tupleA 2-tuple (m, e) of normalized float mantissa and integer base-2 exponent.

Python Examples

Example 1 — Decompose Numbers Into Mantissa and Binary Exponent

Unpack math.frexp() and verify exact reconstruction with math.ldexp().

python
import math

for val in [0.5, 1.0, 6.0, 8.0, -0.375]:
    m, e = math.frexp(val)
    print(f"{val:>6} -> m={m:>6}, e={e:>2}, ok={math.ldexp(m, e) == val}")

How It Works

  1. Normalized range [0.5, 1.0). Every nonzero number maps its mantissa magnitude into [0.5, 1.0).
  2. Exact binary fractions. Six is 0.75 * 2**3 and -0.375 is -0.75 * 2**-1.
  3. Lossless round trip. math.ldexp() restores every single value bit-for-bit.

Example 2 — Check Exponents of Normal and Subnormal Boundary Floats

Inspect the smallest normal and smallest positive subnormal binary64 floats.

python
import math

for val in [2.0 ** -1022, 2.0 ** -1074, 1024.0]:
    m, e = math.frexp(val)
    print(f"m={m}, e={e}")

How It Works

  1. Smallest normal float. 2**-1022 normalizes to 0.5 * 2**-1021.
  2. Smallest subnormal float. Even subnormal 2**-1074 still normalizes cleanly to m = 0.5 with exponent -1073.
  3. Large power of two. 1024.0 equals 0.5 * 2**11.

Example 3 — Verify Zero, Negative Zero, and Infinity Outputs

Confirm frexp() preserves -0.0, inf, and nan with exponent 0.

python
import math

print(math.frexp(0.0))
print(math.frexp(-0.0))
print(math.frexp(float("inf")))

How It Works

  1. Zero returns zero exponent. Both 0.0 and -0.0 keep their exact sign alongside exponent 0.
  2. Infinity unchanged. Passing infinity returns (inf, 0) without error.
  3. Consistent tuple signature. Second element is always an int.

Frequently Asked Questions

A tuple (m, e) where m is a float with 0.5 <= abs(m) < 1.0 (for nonzero finite x) and e is an int such that x == m * 2**e.
C's frexp specification defines the mantissa range as [0.5, 1.0), not [1.0, 2.0).
math.ldexp(m, e) multiplies m by 2**e and restores x exactly.
It returns (0.0, 0) or (inf, 0) cleanly without raising.
Yes, if an integer is too large to convert to a 64-bit float (~1.8e308).

Next: math.fsum()

Return an accurate floating-point sum of values in an iterable.

fsum() →

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