Sub topic 14
Floating-point addition and multiplication
This chapter assumes you can already encode and decode IEEE 754 single precision values (sign, biased exponent, hidden bit), and that you know what guard, round and sticky bits do. Here we put those pieces together into the two arithmetic operations that GATE questions ask about most.
The working model
Hardware never adds or multiplies the raw bit patterns. It unpacks the fields, works on the significands (the hidden 1 plus the fraction) and the exponents separately, then repacks the result.
| Step | Addition | Multiplication |
|---|---|---|
| Sign | Depends on which operand is larger and whether you add or subtract | XOR of the two signs |
| Exponent | Align to the larger exponent | Add the biased exponents, then subtract one bias |
| Significand | Shift the smaller one right, then add or subtract | Multiply the two significands |
| Normalize | Result may need a right shift by 1 or a left shift by many | Product is in [1, 4), so at most a right shift by 1 |
| Round | Using guard, round and sticky bits | Same |
Addition is the harder operation, because alignment loses bits and subtraction can cancel many leading bits. Multiplication is simpler in principle, and its only trap is the bias.
Addition step by step
Alignment is the key idea. You can only add digits of equal weight, so the number with the smaller exponent has its significand shifted right by the difference of exponents. Each shift right adds 1 to its exponent until the two exponents match.
Worked example: 13 + 3
Write 13 = 1.101b × 2³ and 3 = 1.1b × 2¹.
- The exponent difference is 3 − 1 = 2, so shift 1.1 right two places: 0.011 × 2³.
- Add the significands: 1.101 + 0.011 = 10.000.
- The result 10.000 × 2³ is not normalized. Shift right by 1 and add 1 to the exponent: 1.0000 × 2⁴ = 16.
The result is correct: 13 + 3 = 16. The carry out of the leading position is the usual reason for a right shift of 1 after addition.
Subtraction and cancellation
When two nearly equal numbers are subtracted, leading bits cancel and the result needs a long left shift. Take 1.0001b × 2⁰ − 1.0000b × 2⁰ = 0.0001b × 2⁰. Normalizing shifts left 4 places and gives 1.0000b × 2⁻⁴.
The exponent drops by 4. The four bits shifted in on the right are zeros that carry no information. Any error that was in the low bits of the inputs is now in the leading bits of the result. This is called catastrophic cancellation, and the next chapter's pitfalls build on it.
Why guard, round and sticky bits matter here
When the smaller operand is shifted right, its low bits fall off the end of the register. Keep three extra bits:
- The guard bit is the first bit shifted out.
- The round bit is the second.
- The sticky bit is the OR of every bit shifted out after that.
With these three bits, a result rounded to nearest-even is the same as if the sum had been computed exactly and then rounded. If the exponents differ by more than about the precision of the format, the smaller operand can only influence the guard, round and sticky bits. In many cases the larger operand is returned unchanged.
Multiplication step by step
For two normal numbers, the result is built from three independent parts.
- Sign:
s = sa XOR sb. - Exponent:
e = ea + eb − bias. Both stored exponents already contain one bias each, so adding them counts the bias twice. Subtract it once. - Significand: multiply the two significands. Each is in [1, 2), so the product is in [1, 4). If it is 2 or more, shift right by 1 and add 1 to the exponent.
Worked example: 6.5 × 2.5 in single precision
Write 6.5 = 1.101b × 2² and 2.5 = 1.01b × 2¹. The biased exponents are 129 and 128.
- Exponent: 129 + 128 − 127 = 130, which is true exponent 3.
- Significand: 1.101 × 1.01 = 1.10100 + 0.01101 = 10.00001b, which is 2.03125.
- The product is at least 2, so normalize to 1.000001b and increase the exponent to 131 (true exponent 4).
- Result: 1.000001b × 2⁴ = 1.015625 × 16 = 16.25, which matches 6.5 × 2.5.
Simulating it in code
This function multiplies two normal single-precision numbers using only field arithmetic and round-to-nearest-even. It does not handle zero, subnormals, infinities, NaN or exponent overflow, so use it only for normal values whose product stays in range.
import struct
def f32(x):
return struct.unpack('>f', struct.pack('>f', x))[0]
def decode(x):
bits = struct.unpack('>I', struct.pack('>f', x))[0]
# sign, biased exponent, 24-bit significand with hidden 1
return bits >> 31, (bits >> 23) & 0xFF, (bits & 0x7FFFFF) | (1 << 23)
def fmul(a, b):
sa, ea, ma = decode(a)
sb, eb, mb = decode(b)
s = sa ^ sb
e = ea + eb - 127
p = ma * mb # 47 or 48 bits
shift = 24 if p >> 47 else 23 # product >= 2 needs one more shift
if shift == 24:
e += 1
keep = p >> shift
rem = p & ((1 << shift) - 1)
half = 1 << (shift - 1)
if rem > half or (rem == half and keep & 1):
keep += 1 # round to nearest, ties to even
if keep >> 24: # rounding carried out of the top
keep >>= 1
e += 1
bits = (s << 31) | (e << 23) | (keep & 0x7FFFFF)
return struct.unpack('>f', struct.pack('>I', bits))[0]
print(fmul(6.5, 2.5)) # 16.25
a, b = f32(0.1), f32(0.2)
print(fmul(a, b) == f32(a * b)) # TrueThe comparison works because a double holds the exact 48-bit product of two 24-bit significands, so f32(a * b) is a correctly rounded reference.
Non-associativity in action
Addition rounds after every operation. The order of operations therefore changes which bits are lost.
big = 2.0 ** 53 # spacing between doubles here is 2
print((big + 1.0) + 1.0 == big) # True
print(big + (1.0 + 1.0) == big + 2.0) # True
print((big + 1.0) + 1.0 == big + (1.0 + 1.0)) # FalseAdding 1.0 to 2⁵³ falls exactly halfway between two doubles. Ties-to-even returns 2⁵³, so the 1.0 is lost. Done twice in a row, both 1.0s are lost. Added together first, they make 2.0, which is representable and survives.
Common errors
- Subtracting the bias zero or two times in multiplication. You subtract it exactly once.
- Forgetting the hidden 1 when you multiply significands. Multiply 1.101, not 0.101.
- Shifting the larger operand instead of the smaller one. Always shift the smaller exponent's significand right.
- Normalizing before rounding and then forgetting that rounding can carry out and require one more right shift.
- Treating the stored exponent as the true exponent when you compare for alignment. The difference of biased exponents equals the difference of true exponents, so alignment is safe with either. Absolute values are not.
Exercises
Exercise
Add A = 1.010b × 2⁴ and B = 1.1b × 2² using alignment. Give the normalized result.
Show answer
Shift B right by 4 − 2 = 2 places: 0.011b × 2⁴. Add: 1.010 + 0.011 = 1.101. The result is 1.101b × 2⁴ = 1.625 × 16 = 26, already normalized (20 + 6 = 26).
Exercise
A format has a 6-bit exponent field with bias 31. Two numbers have stored exponents 40 and 20. What stored exponent does the product have before normalization of the significand product?
Show answer
40 + 20 − 31 = 29. The true exponents are 9 and −11, which sum to −2, and 29 − 31 = −2 matches. If the significand product is 2 or more, the stored exponent becomes 30.
Exercise
Why can (x + y) + z differ from x + (y + z) in floating point, even when no operation overflows?
Show answer
Each addition rounds its result to the available significand bits. A small value added to a large one can be rounded away completely, while the same small value added to another small value first may be large enough to survive. The two orders round at different points, so the final results can differ.