I have written the following for loop to demonstrate ceil() function of <cmath>.
for (double ceilDemo = 2.0; ceilDemo <= 3.0; ceilDemo += 0.1) cout << setw(10) << ceilDemo << setw(20) << ceil( ceilDemo ) << endl;
The output is as expected except for the last iteration. I mean in the first iteration, when the value of ceilDemo variable is 2.0, ceil() returns 2. for all next iterations upto 2.9 it gives 3 which perfect. Then two strange things happen.
- The loop terminates one iteration before I am expecting it to. I mean for the code I have given above, I get output only upto the value 2.9 of the variable ceilDemo.
- If I modify the loop termination condition to get one extra iteration then regardless of the fact that ceilDemo contains only 3.0, ceil() now returns 4.
Is it a problem due to using a variable of type double to control the loop or something else? Please reply!!!
Thanks in advance!