Floating-Point Representation in Computers
Binary Representation
In computers, floating-point numbers are represented using binary scientific notation, consisting of sign bit, exponent part, and mantissa part. The sign bit indicates the sign of the number, the exponent part determines the magnitude, and the mantissa part records the precision. This structure allows floating-point numbers to represent very large or very small values while balancing precision and range.
- Sign Bit
A single bit used to indicate the sign of the number. A 0 indicates a positive number; a 1 indicates a negative number. - Exponent Part
The number of bits depends on the floating-point format (e.g., 32-bit single precision or 64-bit double precision). For single precision, the exponent part is 8 bits; for double precision, it is 11 bits. The actual exponent value is adjusted via bias (e.g., single precision has a bias of 127, double precision of 1023) to avoid negative exponents. - Mantissa Part
The number of bits also depends on the format. Single precision has a mantissa part of 23 bits, and double precision has 52 bits. The actual mantissa is formed by combining the implicit 1 (1.000...0) with the mantissa part. For example, a mantissa part of 01000000 in single precision corresponds to an actual mantissa of 1.01000000.
Precision and Range
The precision of floating-point numbers is determined by the number of bits in the mantissa, while the range is determined by the number of bits in the exponent.
- Single Precision (32-bit)
- Precision: Approximately 7 decimal digits of effective precision.
- Range: From approximately $1.2 \times 10^{-38}$ to $3.4 \times 10^{38}$.
- Double Precision (64-bit)
- Precision: Approximately 15 decimal digits of effective precision.
- Range: From approximately $2.3 \times 10^{-308}$ to $1.7 \times 10^{308}$.
Special Values
Floating-point numbers (IEEE 754 standard) define various special values to handle boundary cases:
- Infinity
Exponent part is all 1s, mantissa part is all 0s. The sign bit determines the sign of infinity. For example, in single precision, the binary representation is1 1111111 00000000000000000000000. - Subnormal Numbers
Exponent part is all 0s, mantissa part is not all zeros. These numbers have extremely low precision and are used to represent values near zero. - NaN (Not a Number)
Exponent part is all 1s, mantissa part is not all zeros. Used to represent undefined or unrepresentable values (e.g., $0/0$).
Practical Considerations
- Precision Loss: When the mantissa cannot precisely represent a decimal number, the computer rounds it. For example, binary floating-point numbers cannot precisely represent 0.1, leading to calculation errors.
- Overflow and Underflow: When a number exceeds the maximum range, it triggers overflow (displaying infinity); when it is smaller than the minimum range, it triggers underflow (displaying zero).
- Format Conversion: In programming, care must be taken when converting between different floating-point types (e.g.,
floatanddoublein C).
Summary
The representation of floating-point numbers is the core mechanism for processing real numbers in computers. Using binary scientific notation, it balances the trade-off between numerical range and precision. Understanding its structure and rules helps optimize algorithm performance and avoid numerical calculation pitfalls.