Python Glossary Русский

all Python 3Numbers & mathfunction

math.frexp

representation

Decomposes a number into mantissa m and exponent e: x = m·2^e, with 0.5 ≤ |m| < 1.

Syntax

math.frexp(...)

The split is binary, not decimal: frexp(8) is (0.5, 4) because 8 = 0.5·2^4 — not (8.0, 0) as base-10 scientific notation would suggest. The decomposition is exact, so feeding the pair back through math.ldexp reproduces the original value bit for bit. The mantissa is a float and the exponent an int; zero gives (0.0, 0).

Examples

import math
print(math.frexp(8))   # → (0.5, 4)
print(math.frexp(0.5))   # → (0.5, 0)
print(math.frexp(-12))   # → (-0.75, 4)
print(math.frexp(0))   # → (0.0, 0)
m, e = math.frexp(10); print(m * 2 ** e)   # → 10.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy