Understanding How Computers Store Floating-Point Numbers
IEEE 754 represents a floating-point value as:
V = (-1)^s × M × 2^E
s: sign bit; 0 means positive and 1 means negative
M: significand represented as a binary fraction
E: exponent that scales the significand by a power of two
Floating-point values are finite approximations of real numbers. Many decimal fractions cannot be represented exactly in binary, which explains rounding errors, overflow, underflow, and why financial systems often use decimal or fixed-point arithmetic.
Bit layout
Single precision
+---------+----------------+------------------------+
| s | E | M |
+---------+----------------+------------------------+
1 8 23
Double precision
+---------+----------------+------------------------+
| s | E | M |
+---------+----------------+------------------------+
1 11 52
Convert a decimal number to binary
Consider 4.25 in single precision. Convert the integer and fractional parts separately.
For the integer part, repeatedly divide by two and read the remainders in reverse:
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
4 = 100₂
For the fraction, repeatedly multiply by two and record each integer part:
0.25 × 2 = 0.5 -> 0
0.5 × 2 = 1.0 -> 1
0.25 = 0.01₂
Therefore:
4.25 = 100.01₂
= 1.0001₂ × 2²
For a normalized single-precision value:
s = 0
E = 2 + 127 = 129 = 10000001₂
M = 00010000000000000000000
The complete representation is:
0 10000001 00010000000000000000000
Convert the bits back to decimal
Exponent bits: 10000001₂ = 129
Stored fraction: 0001... = 2^-4 = 0.0625
The implicit leading 1 makes the significand 1.0625, so:
(-1)^0 × 1.0625 × 2^(129 - 127) = 4.25
Special exponent patterns represent zero, subnormal values, infinity, and NaN. Normalized numbers use an implicit leading one; subnormal numbers do not.