Barring overflow, in floating-point, x + x + x
is exactly the correctly rounded (i.e. nearest) floating-point number to the real 3*x
, x + x + x + x
is exactly 4*x
, and x + x + x + x + x
is again the correctly rounded floating-point approximation for 5*x
.
The first result, for x + x + x
, derives from the fact that x + x
is exact. x + x + x
is thus the result of only one rounding.
The second result is more difficult, one demonstration of it is discussed here (and Stephen Canon alludes to another proof by case analysis on the last 3 digits of x
). To summarize, either 3*x
is in the same binade as 2*x
or it is in the same binade as 4*x
, and in each case it is possible to deduce that the error on the third addition cancels the error on the second addition (the first addition being exact, as we already said).
The third result, “x + x + x + x + x
is correctly rounded”, derives from the second in the same way that the first derives from the exactness of x + x
.
The second result explains why 0.1 + 0.1 + 0.1 + 0.1
is exactly the floating-point number 0.4
: the rational numbers 1/10 and 4/10 get approximated the same way, with the same relative error, when converted to floating-point. These floating-point numbers have a ratio of exactly 4 between them. The first and third result show that 0.1 + 0.1 + 0.1
and 0.1 + 0.1 + 0.1 + 0.1 + 0.1
can be expected to have less error than might be inferred by naive error analysis, but, in themselves, they only relate the results to respectively 3 * 0.1
and 5 * 0.1
, which can be expected to be close but not necessarily identical to 0.3
and 0.5
.
If you keep adding 0.1
after the fourth addition, you will finally observe rounding errors that make “0.1
added to itself n times” diverge from n * 0.1
, and diverge even more from n/10. If you were to plot the values of “0.1 added to itself n times” as a function of n, you would observe lines of constant slope by binades (as soon as the result of the nth addition is destined to fall into a particular binade, the properties of the addition can be expected to be similar to previous additions that produced a result in the same binade). Within a same binade, the error will either grow or shrink. If you were to look at the sequence of the slopes from binade to binade, you would recognize the repeating digits of 0.1
in binary for a while. After that, absorption would start to take place and the curve would go flat.