Sub topic 14
Floating-Point Addition and Multiplication
Assume you know binary place value, normalized binary scientific notation, IEEE 754 fields, biased exponents, and rounding. This chapter applies those ideas to floating-point arithmetic.
Floating-point addition
Before adding two floating-point numbers, their exponents must match. The number with the smaller exponent has its significand shifted right. This alignment can discard low-order bits before the addition even begins.
For example:
1.101 × 2^3
+ 1.010 × 2^1Rewrite the second operand with exponent 3:
1.101 × 2^3
+ 0.01010 × 2^3
= 1.11110 × 2^3The result is already normalized. If addition produces 10.xxxx, shift the significand right once and increase the exponent by one. If cancellation produces 0.00...1, shift left and decrease the exponent until the result is normalized.
A real format has a limited number of fraction bits. Therefore, the aligned significand may need rounding. Guard, round, and sticky bits help the hardware decide whether to round the retained significand upward.
| Stage | Operation | Possible issue |
|---|---|---|
| Alignment | Match the exponents | Small operand may lose low bits |
| Addition | Add or subtract significands | Carry or cancellation |
| Normalization | Restore the form `1.x × 2^e` | Exponent may change |
| Rounding | Fit the significand width | Exact value may be lost |
| Packing | Store sign, exponent, and fraction | Overflow or underflow |
Floating-point multiplication
For normalized numbers,
(significand₁ × 2^e₁) × (significand₂ × 2^e₂)
= (significand₁ × significand₂) × 2^(e₁ + e₂)The sign is negative when exactly one operand is negative. The significands are multiplied, and the exponents are added. In an IEEE 754 format, the stored biased exponents must be handled carefully:
Eresult = E₁ + E₂ - biasThe product of two normalized significands lies between 1 and almost 4. If it is at least 2, shift it right and increase the exponent. Rounding then reduces the product to the available fraction width.
Multiplication can overflow when the adjusted exponent is too large. It can underflow when the result is too small for a normalized representation. IEEE 754 may represent such a result as a subnormal number, zero, or infinity, depending on the case.
Rounding and non-associativity
Floating-point operations round after intermediate results. Consequently, grouping can change the answer:
x = 1e16
left_grouped = (x + (-x)) + 1.0
right_grouped = x + ((-x) + 1.0)
print(left_grouped) # 1.0
print(right_grouped) # 0.0The expression (-x) + 1.0 rounds to -x, because 1.0 is too small to affect a number near 10^16. Thus, floating-point addition is not generally associative.
Exercise
Why must exponents be aligned before floating-point addition?
Show answer
Binary place values must represent the same powers of two. Shifting the smaller-exponent significand makes the two values comparable, although the shift can lose precision.