IEEE-754 32-Bit Float Internals: Why 0.1 + 0.2 != 0.3 and How Hardware Encodes Floats
DEV Community

IEEE-754 32-Bit Float Internals: Why 0.1 + 0.2 != 0.3 and How Hardware Encodes Floats

Almost every programmer encounters the infamous 0.1 + 0.2 == 0.30000000000000004 quirk early in their journey. This is not a compiler error or language flaw - it is a direct consequence of how silicon arithmetic logic units (ALUs) encode real numbers under the IEEE-754 standard.

The 32-Bit Layout

An IEEE-754 Single Precision floating-point word is partitioned into three distinct bit fields:

  • Sign Bit (Bit 31): 0 for positive numbers, 1 for negative numbers.
  • Biased Exponent (Bits 30-23): 8 bits encoded with a fixed bias of 127 ( 2⁸-1 - 1 ).
  • Mantissa / Significand (Bits 22-0): 23 bits representing fractional magnitude.

extValue = (-1)^S imes (1.M) imes 2^E - 127

Why Use an Excess-127 Biased Exponent?

Why not use standard Two's Complement for the exponent? By adding 127 to the real exponent, the stored values range from 1 to 254 (for normalized values).

Because the exponent is strictly non-negative, the CPU hardware can compare the magnitudes of two floating-point numbers using high-speed integer comparison logic without decoding signs.

The Implicit Leading 1

In scientific notation in binary, every non-zero normalized number begins with 1. (e.g. 1.011₂ imes 2³ ).

Since the leading bit is always 1, hardware designers deliberately omitted it from physical storage. The ALU decodes the 23-bit mantissa as 1.M , providing 24 bits of effective precision for the price of 23 bits.

Special Hardware Patterns

  • Zero: E = 0, M = 0 . Note that IEEE-754 supports +0 and -0 .
  • Subnormal Numbers: $E = 0, M eq 0$. The implicit leading bit becomes 0.M , allowing gradual underflow near zero.
  • Infinity ( ±∞ ): E = 255, M = 0 . Generated by overflow or non-zero division by zero.
  • NaN (Not a Number): $E = 255, M eq 0 . Generated by undefined operations ( 0/0, \sqrt{-1}$).

Explore our complete Computer Organization & Architecture track on Binary Dose .

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.