How Does IEEE 754 Encode Floating-Point Numbers?
The IEEE 754 standard defines how computers represent and manipulate floating-point numbers in binary. It divides a fixed-width bit sequence into three distinct fields: the sign bit, the exponent, and the mantissa, which is also known as the significand. By combining these three components with a specific bias and an implicit leading bit, classical computers can represent a vast range of real numbers, from tiny fractions to massive values, as well as special states like infinity and Not a Number.
Bit Layout and Precision Formats
IEEE 754 specifies several binary formats, with single-precision (32-bit) and double-precision (64-bit) being the most common in classical computing.
In single-precision (binary32), the 32 bits are allocated as follows:
- Sign bit: 1 bit (bit 31)
- Exponent: 8 bits (bits 30 to 23)
- Mantissa: 23 bits (bits 22 to 0)
In double-precision (binary64), the 64 bits are allocated as follows:
- Sign bit: 1 bit (bit 63)
- Exponent: 11 bits (bits 62 to 52)
- Mantissa: 52 bits (bits 51 to 0)
The Sign Bit
The sign bit \(s\) is the most significant bit and determines whether the number is positive or negative. A value of \(0\) indicates a positive number, while a value of \(1\) indicates a negative number. Mathematically, this is expressed as \((-1)^s\). Because the sign is stored separately from the magnitude, IEEE 754 supports both \(+0\) and \(-0\).
The Biased Exponent
The exponent field \(E\) represents the power of \(2\) by which the mantissa is scaled. Rather than using two's complement to store negative exponents, IEEE 754 uses a biased representation. A fixed bias \(B\) is added to the actual exponent \(e\) so that the stored value \(E = e + B\) is always an unsigned integer.
For a field of \(k\) bits, the bias is calculated as:
\[B = 2^{k-1} - 1\]
- For single-precision (\(k = 8\)), the bias is \(127\).
- For double-precision (\(k = 11\)), the bias is \(1023\).
Using a bias allows floating-point numbers to be compared lexicographically as unsigned integers, simplifying hardware comparison circuits.
The Mantissa and the Implicit Leading Bit
The mantissa field \(M\) stores the fractional part of the number's binary scientific notation. For normalized numbers, binary scientific notation always has a single non-zero digit—which in binary must be \(1\)—before the binary point (e.g., \(1.xxxx_2 \times 2^e\)).
Because this leading \(1\) is always present in normalized numbers, IEEE 754 omits it from storage to gain an extra bit of precision. The 23-bit mantissa in single-precision actually represents 24 bits of precision, and the 52-bit mantissa in double-precision represents 53 bits.
The value of a normalized floating-point number is computed as:
\[V = (-1)^s \times (1 + M) \times 2^{E - B}\]
Here, \(M\) is the fractional value of the mantissa bits:
\[M = \sum_{i=1}^{p} b_{-i} 2^{-i}\]
where \(p\) is the number of mantissa bits (\(23\) or \(52\)) and \(b_{-i}\) is the \(i\)-th bit of the mantissa from left to right.
Special Values and Subnormal Numbers
IEEE 754 reserves the minimum and maximum exponent values (\(E = 0\) and \(E = 2^k - 1\)) to encode special cases:
- Zero: When \(E = 0\) and \(M = 0\), the value is \(+0\) or \(-0\) depending on the sign bit.
- Subnormal Numbers: When \(E = 0\) and \(M \neq 0\), the implicit leading bit becomes \(0\) instead of \(1\), and the exponent is fixed at \(1 - B\). The value is \((-1)^s \times M \times 2^{1 - B}\). This allows gradual underflow near zero.
- Infinity: When all exponent bits are \(1\) (\(E = 2^k - 1\)) and \(M = 0\), the value represents \(+\infty\) or \(-\infty\).
- NaN (Not a Number): When all exponent bits are \(1\) and \(M \neq 0\), the value represents NaN, which is used for undefined results such as \(0/0\) or \(\sqrt{-1}\).