5. Floating-Point Arithmetic
What really happens when you add two floats
When you write 1.5 + 0.125 in code, you might expect the CPU to just add two numbers. What actually happens is a multi-stage pipeline: the two values are dissected into sign, exponent, and mantissa; their exponents are brought into alignment; their mantissas are operated on; the result is normalized back into IEEE 754 form; and only then is rounding applied. Each stage can introduce or discard precision. Understanding this pipeline is the key to understanding why floating-point can surprise you.
The Addition Pipeline
Adding two IEEE 754 numbers requires four distinct stages. They must happen in this order because each stage's output is the next stage's input.
The four-stage floating-point addition pipeline
Multiplication and division follow a similar pipeline but with a simpler first step. We'll cover them separately.
Exponent Alignment
IEEE 754 stores numbers in the form 1.mantissa × 2^exponent. To add two numbers, they must share the same exponent. The CPU always shifts the smaller number's mantissa rightward until both exponents match.
Bits shifted off the right edge are lost
Shifting the mantissa right to align exponents drops low-order bits. If the exponent difference is large enough, all of the smaller number's significant bits fall off the end. This is called catastrophic cancellation's quieter cousin — absorption.
Worked example: 1.5 + 0.125
In IEEE 754 single precision:
- 1.5 = 1.1000000... × 20 (exponent field = 127, so biased exponent = 0)
- 0.125 = 1.0000000... × 2−3 (exponent field = 124, so biased exponent = −3)
The exponent difference is 3. We must right-shift 0.125's mantissa by 3 positions to bring it up to exponent 0:
Aligning 0.125 to match 1.5's exponent
1.5: 1.10000000000000000000000 × 2⁰
0.125: 1.00000000000000000000000 × 2⁻³
Shift 0.125 right by 3 (multiply exponent by 2³):
0.125 aligned: 0.00100000000000000000000 × 2⁰
↑↑↑
These three bits slid off the hidden-bit position
For 0.125 this is lossless — the three shifted bits were zero. But for a number like 0.1, many significant bits would be pushed off the end.
Mantissa Addition
Once the exponents match, the mantissas are added as ordinary binary integers (with the implicit leading 1 included).
Adding the aligned mantissas
1.10000000000000000000000
+ 0.00100000000000000000000
= 1.10100000000000000000000
Result: 1.10100000000000000000000 × 2⁰
For subtraction, the same alignment applies, then the mantissas are subtracted. This is where catastrophic cancellation can occur — if the two values are close, many leading bits cancel to zero, leaving a result with very few significant bits.
Normalization
The result of an addition or subtraction may not be in the canonical IEEE 754 form 1.xxx. Two cases arise:
- Overflow carry: addition produced a carry into bit 2 (e.g.
1.111 + 0.111 = 10.110). Shift right by 1, increment exponent by 1. - Leading-zero cancellation: subtraction produced a result like
0.0011xxx. Shift left until the leading 1 reappears, decrementing the exponent for each shift.
Normalization examples
Carry case:
1.1100 + 0.1100 = 10.1000 → shift right 1 → 1.01000 × 2¹
Cancellation case:
1.0010 − 1.0001 = 0.0001 → shift left 4 → 1.00000 × 2⁻⁴
Rounding
After normalization, the result may have more bits than the 23-bit mantissa field can hold. IEEE 754 defines four rounding modes:
| Mode | Also called | Description |
|---|---|---|
| Round to nearest, ties to even | Banker's rounding | Default. When exactly halfway, round to whichever value has a 0 in the least-significant bit. |
| Round toward zero | Truncation | Drop the extra bits. Always rounds toward zero. |
| Round toward +∞ | Ceiling | Round up for positive numbers, down for negative. |
| Round toward −∞ | Floor | Round down for positive numbers, up for negative. |
The default mode — round-to-nearest-even — is carefully designed to avoid systematic bias. If you always rounded 0.5 up, the rounding errors would accumulate in one direction. By rounding to the nearest even digit at the halfway point, errors tend to cancel out over many operations.
Guard, Round, and Sticky Bits
To make rounding decisions correctly, the hardware keeps three extra bits beyond the 23-bit mantissa during each operation. These are not stored in the final result — they only exist transiently inside the FPU.
The three rounding bits
- Guard bit (G): the first bit shifted off the mantissa during alignment or normalization
- Round bit (R): the second bit shifted off
- Sticky bit (S): the OR of all remaining shifted-off bits — 1 if any bit beyond R was non-zero
Together, GRS tell the rounding logic whether the discarded bits were exactly zero, less than half an ULP, exactly half an ULP, or more than half an ULP. Without them, round-to-nearest-even would be impossible to implement correctly.
Rounding decision table (round-to-nearest-even):
| G | R | S | Meaning | Action |
|---|---|---|---|---|
| 0 | — | — | Discarded part < half ULP | Truncate (round down in magnitude) |
| 1 | 0 | 0 | Exactly half ULP | Round to even (check LSB of mantissa) |
| 1 | 0 | 1 | Just above half ULP | Round up |
| 1 | 1 | — | More than half ULP | Round up |
Multiplication
Multiplying two floating-point numbers is simpler than adding them because no alignment step is needed:
-
Add the exponents
The product's exponent is the sum of the two input exponents (after removing the bias from one of them to avoid double-biasing). If the sum overflows, the result is ±Infinity.
-
XOR the sign bits
Positive × negative = negative. This is a single bit operation.
-
Multiply the mantissas
Multiply the two 24-bit values (23 stored bits + implicit leading 1) as integers. The result is up to 48 bits wide. This is the expensive step — the hardware contains a dedicated integer multiplier.
-
Normalize and round
The 48-bit product is right-shifted into 1.xxx form, the exponent is adjusted, and the GRS bits guide rounding exactly as in addition.
Multiplying 1.5 × 0.125
1.5: sign=0 exp=0 mantissa=1.10000...
0.125: sign=0 exp=−3 mantissa=1.00000...
Sign: 0 XOR 0 = 0 (positive)
Exponent: 0 + (−3) = −3
Mantissa: 1.100 × 1.000 = 1.100000 (no normalization shift needed)
Result: 1.10000... × 2⁻³ = 0.001875 × ... = 0.1875
Exact result: 1.5 × 0.125 = 0.1875 — representable exactly in IEEE 754.
Fused Multiply-Add (FMA)
A fused multiply-add computes a × b + c as a single hardware operation, with only one rounding step at the very end instead of two (one after the multiply, one after the add).
FMA vs. separate operations
Without FMA:
temp = round(a × b) ← rounding error ε₁ introduced here
result = round(temp + c) ← rounding error ε₂ introduced here
Total error: ε₁ + ε₂
With FMA:
result = round(a × b + c) ← one rounding step, full precision kept in temp
Total error: ε₁ only
FMA is available as a hardware instruction on all modern CPUs and GPUs. It matters most in:
- Dot products — used everywhere in linear algebra and machine learning
- Polynomial evaluation — Horner's method benefits directly from FMA
- Compensated summation — algorithms like Kahan summation can be accidentally broken if a compiler fuses or un-fuses operations without FMA awareness
In C/C++ you get FMA explicitly via fma(a, b, c) from <math.h>. Compilers may also emit FMA instructions automatically with -ffast-math or -ffp-contract=fast.
Key Takeaways
Key Takeaways
- Floating-point addition is a four-stage pipeline: align exponents, add mantissas, normalize, round
- Exponent alignment shifts the smaller number's mantissa right — bits shifted off are permanently lost
- The default rounding mode is round-to-nearest-even (banker's rounding) to avoid systematic drift
- Guard, round, and sticky bits extend the mantissa temporarily so rounding decisions are correct
- Multiplication is simpler: add exponents, XOR signs, multiply mantissas, normalize, round
- FMA computes a×b+c with one rounding step instead of two — more accurate and sometimes faster
Try It Yourself — Float Arithmetic Visualizer
Enter two decimal numbers and pick an operation. Step through the full IEEE 754 pipeline: bit representation, exponent alignment, mantissa operation, normalization, rounding, and final result with error analysis.