Why Integers Are Not Enough
All arithmetic covered earlier in this series assumed whole numbers stored as fixed-width binary integers. Many real-world computations, however, require fractional values and numbers spanning an enormous range of magnitudes — from the mass of an electron to the distance between galaxies — which a plain integer representation cannot handle efficiently.
The Core Idea: Scientific Notation in Binary
Floating Point representation borrows the idea behind decimal scientific notation, where a number is expressed as a value multiplied by a power of a base. In binary floating point, a number is broken into three components packed into a fixed-width word:
Sign: a single bit indicating whether the number is positive or negative.Exponent: determines the overall magnitude, effectively how far the binary point is shifted.Fraction(also called theMantissa): holds the significant digits of the number.
Conceptually, a floating-point value is interpreted as:
Value = (−1)^Sign × (1.Fraction) × 2^(Exponent − Bias)The IEEE 754 Standard
Nearly all modern hardware, including RISC-V, follows the IEEE 754 standard for floating-point representation, which precisely defines the bit widths and behavior of these fields so that floating-point results are consistent across different processors and manufacturers.
Single Precisionuses 32 bits total: 1 sign bit, 8 exponent bits, and 23 fraction bits.Double Precisionuses 64 bits total: 1 sign bit, 11 exponent bits, and 52 fraction bits, providing a much wider range and greater precision at the cost of double the storage.
The standard also uses a Bias value subtracted from the stored exponent field, which allows both very large and very small magnitudes to be represented without needing a separate sign bit for the exponent itself.
Precision Limits and Rounding
Because only a fixed number of fraction bits are available, most real numbers cannot be represented exactly and must be rounded to the nearest representable value. This has a direct practical consequence: floating-point arithmetic does not always behave like exact mathematics.
Example of a common rounding artifact:
0.1 + 0.2 does not always equal exactly 0.3
in floating-point representation, due to rounding
during binary encoding.This is not a hardware defect but an inherent consequence of representing an infinite range of real numbers using a finite number of bits.
Special Values Defined by the Standard
IEEE 754 reserves specific bit patterns for values that do not correspond to ordinary numbers, including Positive and Negative Infinity, used to represent overflow, and NaN (Not a Number), used to represent the result of undefined operations such as zero divided by zero.
Why Understanding This Matters
Any programmer working with monetary calculations, scientific simulations, or graphics needs to understand that floating-point values carry inherent rounding error, and that comparing two floating-point numbers for exact equality is often unreliable — a direct consequence of the finite-bit representation described in this article.