Example 1 — Decompose Numbers Into Mantissa and Binary Exponent
Unpack math.frexp() and verify exact reconstruction with math.ldexp().
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
- Normalized range [0.5, 1.0). Every nonzero number maps its mantissa magnitude into
[0.5, 1.0). - Exact binary fractions. Six is
0.75 * 2**3and-0.375is-0.75 * 2**-1. - Lossless round trip.
math.ldexp()restores every single value bit-for-bit.

