Floating Point Internals
How a computer stores a real number in 32 bits, using the IEEE 754 binary32 (single-precision) format. Toggle bits, or type a value, and watch the sign, exponent and fraction fields change. Try 0.1: the number stored is not exactly one tenth.
IEEE 754 Visualizer
32-BIT SINGLE PRECISION FLOAT
- ■ sign · 1 bit
- ■ exponent · 8 bits
- ■ fraction · 23 bits
Under the hood
Memory: 32 bits (4 bytes), laid out left to right:
- Bit 31: sign (0 = positive, 1 = negative)
- Bits 30–23: exponent field, 8 bits, stored with a bias of 127
- Bits 22–0: fraction field, 23 bits (the standard calls it the trailing significand field)
| Exponent field (e) | Fraction (f) | Kind | Value |
|---|---|---|---|
| 0 | 0 | Zero | (−1)ˢ × 0 |
| 0 | ≠ 0 | Subnormal | (−1)ˢ × 0.f × 2⁻¹²⁶ |
| 1 – 254 | any | Normal | (−1)ˢ × 1.f × 2⁽ᵉ⁻¹²⁷⁾ |
| 255 | 0 | Infinity | (−1)ˢ × ∞ |
| 255 | ≠ 0 | NaN | not a number |
The formula (−1)ˢ × 1.f × 2⁽ᵉ⁻¹²⁷⁾ describes only the normal numbers. The leading 1 is not stored; it is implied, which is how 23 stored bits give 24 bits of precision. When the exponent field is 0, the implied bit becomes 0 and the exponent is fixed at −126, so the numbers fade toward zero in even steps (gradual underflow) instead of jumping from the smallest normal number straight to 0. When the exponent field is 255, the value is an infinity or a NaN.
Most decimal fractions, 0.1 among them, have no finite binary expansion, so the nearest representable value is stored instead. For 0.1 that value is 0.100000001490116119384765625. The panel under the bit grid shows the exact stored value so you can see the gap.
A note on words: this field is often called the mantissa, and an earlier version of this page used that word. IEEE 754 uses significand for the whole 1.f quantity and trailing significand field for the stored 23 bits, because “mantissa” also names the fractional part of a logarithm, which is a different thing.