Bits, floats, text and encodings

O(1)·O(1) space
Fit

Drag to pan; pinch or Ctrl+scroll to zoom; arrow keys pan once the picture has focus.

001111111100100110011001100110011001100110011001100110011001101000111111101110011001100110011001100110011001100110011001100110100.2 (exp -3)0.1 (exp -4)

Decomposed: 0.2 has sign 0, biased exponent 1020, mantissa 1001100110011001100110011001100110011001100110011010; 0.1 has sign 0, biased exponent 1019, mantissa 1001100110011001100110011001100110011001100110011010.

Decomposed: 0.2 has sign 0, biased exponent 1020, mantissa 1001100110011001100110011001100110011001100110011010; 0.1 has sign 0, biased exponent 1019, mantissa 1001100110011001100110011001100110011001100110011010.
  1. 1decompose both operands into sign, exponent and mantissa
  2. 2compare the exponents to find which operand has the larger one
  3. 3shift the smaller exponent’s mantissa right to align it, keeping the bits that fall off
  4. 4add the two aligned significands
  5. 5renormalise if the sum needs one more bit than the significand holds
  6. 6round to the nearest representable mantissa, ties to even
  7. 7reassemble the result from its sign, exponent and mantissa
Additions performed
1

Complexity

BestO(1)
AverageO(1)
WorstO(1)
Extra spaceO(1)
bits touched at n=1 predicts 1; this run took 1.

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

Worth noticing

Cost

Best
O(1)
Average
O(1)
Worst
O(1)
Extra space
O(1)

Worth comparing

All bits algorithms