IEEE 754, explained
How computers represent numbers with fractions using a fixed 32 bits, and why that representation isn't always exact.
Whole numbers divide neatly into binary. Numbers with fractions don’t always. IEEE 754 is the standard almost every computer uses to store decimal numbers anyway, by borrowing the same trick as scientific notation: instead of storing the exact value, store a sign, a scale, and a set of significant digits.
The three parts
A single-precision (32-bit) float splits into:
- 1 sign bit — 0 for positive, 1 for negative
- 8 exponent bits — the scale, stored with a fixed offset (bias) of 127 added to the real exponent
- 23 mantissa bits — the significant digits after an assumed leading 1
Working through an example
Take 10.5. In binary, that’s 1010.1. Normalized into scientific-notation form, it becomes 1.0101 × 2³.
Sign: 0 (positive)
Exponent: 3 + 127 = 130 → 10000010
Mantissa: 0101 (padded to 23 bits) → 01010000000000000000000
Put together, those 32 bits are the IEEE 754 representation of 10.5 — which is also why it converts cleanly to the hex value 41280000.
Why it isn’t always exact
Some decimal fractions can’t be written exactly in binary, the same way 1/3 can’t be written exactly in decimal — you just get more repeating digits the further out you go. With only 23 mantissa bits to work with, the value sometimes gets rounded to the nearest representable one. That’s the real reason floating-point arithmetic occasionally produces results like 0.1 + 0.2 landing just barely off 0.3 — it’s not a bug, it’s a fixed number of bits trying to represent an infinite range of values.
Try the conversion yourself in the IEEE 754 converter.