Skip to main content

Floating point addition and subtraction

Short answer

To add/subtract, align exponents by shifting the smaller mantissa right. Then add/subtract mantissas, normalize (shift left on leading zero, shift right on overflow). Example: 1.101×2³ + 0.011×2³ = 10.000×2³ → 1.000×2⁴ = 16.

Explain the floating point addition and subtraction algorithm with flowchart and example.

A floating-point number is X = m × 2^e (mantissa m, exponent e; the exponent is biased in IEEE 754). To add or subtract two such numbers, their exponents must first be made equal.

Algorithm (steps)

  1. Check for zeros: if either operand is 0, the result is the other operand (for subtraction, with its sign changed if it is the subtrahend).
  2. Align the mantissas: compare the exponents. Shift the mantissa of the number with the smaller exponent right by the difference, increasing its exponent with each shift, until the exponents are equal.
  3. Add or subtract the mantissas (for subtraction, change the sign of the second operand and add) using the signed-mantissa rules. The result gets the common exponent.
  4. Check for a zero result and for mantissa overflow: if the mantissa overflows (carry out), shift it right by one and increase the exponent by 1.
  5. Normalise the result: if the mantissa has leading zeros, shift it left and decrease the exponent for each shift, until the first digit is non-zero (1.xxx in binary).
  6. Check for exponent overflow or underflow, round the mantissa if needed, and give the result.
        Start
          |
   Is X or Y zero? --yes--> result = other operand
          | no
   Align mantissas (shift smaller right)
          |
   Add / subtract mantissas
          |
   Result zero? --yes--> result = 0
          | no
   Mantissa overflow? --yes--> shift right, e = e + 1
          | no
   Normalise (shift left, e = e - 1)
          |
   Exponent overflow/underflow? --yes--> report error
          | no
         Stop

Example 1 (addition): 13 + 3

X = 1.101 × 2³ (13), Y = 1.100 × 2¹ (3).

  • Exponent difference = 3 - 1 = 2; shift Y right 2 places: 0.011 × 2³.
  • Add: 1.101 + 0.011 = 10.000 (carry out).
  • Overflow: shift right 1 and increment the exponent: 1.000 × 2⁴.
  • Result = 1.000 × 2⁴ = 16 ✓

Example 2 (subtraction): 13 - 3

  • Align as above: Y = 0.011 × 2³.
  • Subtract: 1.101 - 0.011 = 1.010.
  • Already normalised: 1.010 × 2³ = 1.25 × 8 = 10 ✓

(If the result has leading zeros, for example 1.101 - 1.100 = 0.001, shift it left 3 times and decrease the exponent by 3.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Organization and Architecture chapter 5 questions →