Sums Are Not Exact
Your assumption that the sum of exact values is exact is wrong.
Floating-point arithmetic uses some number of digits that is fixed for the format (such as 24 binary digits for float
). The mathematical sum of two 24-digit numbers may have 25 digits and therefore requires rounding to represent within 24 digits (and an exponent).
Additionally, when two numbers with different exponents are added, the digits of one are offset relative to the other. The sum may have additional digits due to the offset, and again must be rounded.
When you add the numbers in different orders, the resulting roundings may be different.
Examples of Inexact Sums
These examples use three-digit binary significands.
In this example, the addition carries into a new column:
1.10 • 23
1.01 • 23
――――――――――
10.11 • 23 Exact sum, too many digits, must be rounded.
11.0 • 23 Sum rounded to three digits.
1.10 • 24 Rounded sum, exponent adjusted to normalize significand.
In this example, the numbers have different exponents, and adjusting for this shifts the digits into new columns:
1.11 • 23
1.01 • 25 Different exponent requires adjustment.
0.0111 • 25 Adjusted to match exponent.
1.01 • 25
――――――――――――
1.1011 • 25 Exact sum, too many digits, must be rounded.
1.11 • 25 Rounded sum.
Examples of Non-Associative Sums
Now we can look at adding three numbers in different ways and see that different sums are produced.
We will compare (1.10•20 + 1.10•20) + 1.00•24) to 1.10•20 + (1.10•20 + 1.00•24).
For the first express, we add the first and second operands, then the third:
Add first and second operands:
1.10 • 20 First operand.
1.10 • 20 Second operand.
――――――――――
11.00 • 20 Exact sum, too many digits, must be rounded.
11.0 • 20 Rounded sum, must be normalized.
1.10 • 21 Normalized, rounded sum.
Add previous result and third operand:
1.10 • 21 Previous result.
1.00 • 24 Third operand.
Exponents do not match, so adjust and then add:
0.00110 • 24 Previous result adjusted to match exponent.
1.00 • 24 Third operand.
――――――――――――
1.00110 • 24 Exact sum, too many digits, must be rounded.
1.01 • 24 Rounded sum.
For the second expression, we add the second and third operands, then the first:
Add second and third:
1.10 • 20 Second operand.
1.00 • 24 Third operand.
Exponents do not match, so adjust, then add:
0.000110 • 24 Second operand adjusted to match exponent.
1.00 • 24 Third operand.
――――――――――――――
1.000110 • 24 Exact sum, too many digits, must be rounded.
1.00 • 24 Rounded sum.
Add first operand and previous result:
1.10 • 20 First operand.
1.00 • 24 Previous result.
Exponents do not match, so adjust and then add:
0.000110 • 24 First operand adjusted to match exponent.
1.00 • 24 Previous result.
―――――――――――――
1.000110 • 24 Exact sum, too many digits, must be rounded.
1.00 • 24 Rounded sum.
The first expression yields 1.01•24, while the second expression yields 1.00•24. So the order in which operands are added affects the result.