In a book I am currently reading, there is this excerpt:
You can also use a floating-point
value as a loop counter. Here’s an
example of aforloop with this kind
of counter:double a(0.3), b(2.5); for(double x = 0.0; x <= 2.0; x += 0.25) cout << "\n\tx = " << x << "\ta*x + b = " << a*x + b;This code fragment calculates the
value ofa*x+bfor values ofx
from0.0to2.0, in steps of
0.25; however, you need to take care
when using a floating-point counter in
a loop. Many decimal values cannot be
represented exactly in binary
floating-point form, so discrepancies
can build up with cumulative values.
This means that you should not code a
for loop such that ending the loop
depends on a floating-point loop
counter reaching a precise value. For
example, the following poorly-designed
loop never ends:for(double x = 0.0 ; x != 1.0 ; x += 0.2) cout << x;The intention with this loop is to
output the value ofxas it varies
from0.0to1.0; however,0.2
has no exact representation as a
binary floating-point value, so the
value ofxis never exactly1.
Thus, the second loop control
expression is always false, and the
loop continues indefinitely.
Can someone please explain how the first code block runs while the second doesn’t?
The first one will eventually terminate, even if
xdoesn’t reach exactly 2.0… because it’ll end up being greater than 2.0, and thus break out.The second one would have to make
xhit exactly 1.0 in order to break.It’s unfortunate that the first example uses a step of 0.25, which is exactly representable in binary floating point – it would have been smarter to make both examples use 0.2 as the step size. (0.2 isn’t exactly representable in binary floating point.)