IEEE 754 Single-Precision Floating-Point Representation
How 32-bit floats encode sign, +127 biased exponent, and a 23-bit mantissa (with an implicit leading 1), and why 0.1 + 0.2 has rounding error.
1. Binary Fractions and Why 0.1 + 0.2 ≠ 0.3
In base 10, columns to the right of the decimal point represent negative powers of 10 (10⁻¹ = 1/10, 10⁻² = 1/100). A fraction like 1/3 = 0.3333… repeats infinitely in base 10 because 3 does not divide any power of 10.
In base 2, columns to the right of the binary point represent negative powers of 2:
Place value: 2⁻¹ 2⁻² 2⁻³ 2⁻⁴ 2⁻⁵
Fraction: 1/2 1/4 1/8 1/16 1/32
Decimal: 0.5 0.25 0.125 0.0625 0.03125
Any fraction whose denominator is a power of 2 terminates cleanly in binary. Click the 0.15625 (Exact) preset in the simulator above: 0.15625 = 5/32 = 1/8 + 1/32 = 0.00101₂, so its Rounding Error is 0.
By contrast, 0.1 = 1/10 has a factor of 5 in the denominator, so in binary it repeats forever: 0.0001100110011…₂. Click 0.1 (Rounded): because a 32-bit float only has 23 fraction bits, the infinite binary sequence must be rounded to the nearest representable value (0.10000000149…). When a program adds rounded 0.1 and rounded 0.2, those tiny rounding errors accumulate into 0.30000000000000004.
2. The IEEE 754 Single-Precision (float32) Layout
Just as decimal scientific notation writes numbers as ±d.ddd × 10ᵉ, the IEEE 754 standard writes binary floating-point numbers as:
Value = (−1)^S × (1.Fraction)₂ × 2^(E − 127)
A 32-bit single-precision float splits its 32 bits into three fields (color-coded in the simulator):
| Field | Bits | Role |
|---|---|---|
Sign (S) | 1 bit (bit 31) | 0 = positive (+), 1 = negative (−). |
Biased Exponent (E) | 8 bits (bits 30–23) | Unsigned integer from 1 to 254 for normal numbers. Subtract the bias of 127 to get the true exponent (−126 to +127). |
Fraction / Mantissa (M) | 23 bits (bits 22–0) | The fractional bits after the binary point in 1.M. |
Two key architectural details:
- The Hidden Leading
1(Implicit Bit): In base 2, every normalized non-zero number starts with1.something. Because the leading bit before the point is always1, IEEE 754 does not store it in memory — giving 24 bits of significand precision while only storing 23 bits. - Why Bias the Exponent by +127? Storing the exponent as an unsigned value
E = exponent + 127places the smallest exponent (−126 → 1) and largest exponent (+127 → 254) in strictly increasing unsigned order, allowing hardware to compare floating-point magnitudes using fast integer comparator circuits.
3. Worked Example: Encoding −6.5
Click the −6.5 preset button and follow the 4 steps used to encode it:
1. Sign: Negative → S = 1
2. Binary: 6 = 110₂ and 0.5 = 0.1₂ → 6.5 = 110.1₂
3. Normalize (shift point 2 places left): 110.1₂ = 1.101₂ × 2²
4. Fields:
• Biased Exponent E = 2 + 127 = 129 = 10000001₂
• Fraction M (drop the leading 1.) = 10100000000000000000000₂
• 32-bit Hex = 0xC0D00000
4. Special Exponent Values (E = 0 and E = 255)
The all-zeros exponent (E = 0) and all-ones exponent (E = 255) are reserved for special categories:
| Category | Exponent E | Fraction M | Meaning |
|---|---|---|---|
Signed Zero (±0.0) | 0 | 0 | Represents exact +0.0 or −0.0 (needed because normal numbers always have a leading 1.). |
| Subnormal (Denormal) | 0 | ≠ 0 | Leading bit becomes 0.M with fixed exponent 2⁻¹²⁶, providing gradual underflow near zero. |
Infinity (±∞) | 255 | 0 | Result of overflow or division by zero (1.0 / 0.0). |
| NaN (Not a Number) | 255 | ≠ 0 | Result of undefined operations (0.0 / 0.0, √−1). Note: NaN === NaN is always false. |
Exercises
1. Encode decimal 0.75 into IEEE 754 single-precision fields (Sign, Biased Exponent, Fraction, and Hex), then verify in the simulator.
Show solution
0.75 = 1/2 + 1/4 = 0.11₂ = 1.1₂ × 2⁻¹.• Sign
S = 0• Biased Exponent
E = −1 + 127 = 126 (01111110₂)• Fraction
M = 10000000000000000000000₂ (significand 1.5)• Hex:
0x3F400000.2. Type 16777217 (which is 2²⁴ + 1) into the Decimal Input box. Why does the Stored Value round back to 16777216?
Show solution
2²⁴ = 16,777,216, the 23rd fraction bit has weight 2²⁴ × 2⁻²³ = 2.0, so the gap between adjacent representable floats is 2. Odd integers above 2²⁴ cannot be represented in float32.Quiz
Why can 0.125 be represented with zero rounding error in IEEE 754 while 0.1 cannot?
Only fractions whose denominator is a power of 2 terminate in binary.
In 32-bit IEEE 754, if the 8-bit biased exponent field is 10000010₂ (decimal 130), what is the true unbiased exponent?
Unbiased exponent = E − 127 = 130 − 127 = +3.
How does a 32-bit normalized float achieve 24 bits of significand precision using only 23 fraction bits?
The hidden leading 1 in 1.M gives one extra bit of precision for free.
What category of IEEE 754 value has Exponent E = 255 and a non-zero Fraction M ≠ 0?
E = 255 with M = 0 is ±Infinity; E = 255 with M ≠ 0 is NaN.