IEEE 754 addition
Floating-point addition decomposes both operands into a sign, an exponent and a mantissa, then shifts the mantissa of whichever operand has the smaller exponent to the right until both exponents agree. That shift is where precision is lost: bits that fall off the low end of the shifted mantissa are gone before the two mantissas are even added. The two aligned mantissas are then added exactly, renormalised if the sum overflowed the available width, and rounded (to nearest, ties to even) back down to the mantissa’s fixed width.
When it is the right choice
This is not a choice of algorithm — it is what every CPU’s floating-point unit does for every floating-point addition, in hardware. The page exists to make the mechanism visible, not to offer an alternative to it.
How it works
- Decompose both operands into sign, exponent and mantissa.
- Shift the mantissa of the operand with the smaller exponent right until the exponents agree, keeping the bits that fall off the end.
- Add the two aligned mantissas exactly.
- If the sum overflowed the mantissa’s width, shift it right by one bit and increase the exponent (renormalise).
- Round the result to the mantissa’s fixed width, to nearest, with ties rounding to an even last bit.
Worth noticing
- The bits that fall off the end during alignment are shown directly, not summarised as "some precision was lost" — count them and you can predict exactly how far the result will drift.
- When the exponents already agree, or the shift amount is small enough that no significant bit falls off, addition is exact. Precision loss is not automatic — it depends on how far apart the two exponents are.
- When the shift amount exceeds the mantissa’s own width, every bit of the smaller operand is lost: the sum equals the larger operand exactly, as if the smaller one had never been added at all.
Cost
- Best
- O(1)
- Average
- O(1)
- Worst
- O(1)
- Extra space
- O(1)