go to previous page   go to home page   go to next page highlighting

Answer:

Value was 10.0 when the loop ended


The Perils of Floating Point

value starts out at 0.0, and has 0.1 added to it until it reaches exactly 9.9. This is < limit, so (ideally) the loop would execute one more time to make value 10.0 . The loop then would exit.

But the variable actually ends up as 10.09999999999998 because of floating point inaccuracy.

Single precision floating point is really not very accurate. But even double precision floating point has errors. If you are not cautious with floats, your program may go belly up.


QUESTION 14:

What action is usually performed at the bottom of a counting loop?


go to previous page   go to home page   go to next page