Mail Archives: djgpp/1998/06/18/02:46:11
On Wed, 17 Jun 1998, Roberto Sassi wrote:
> double r=0.1;
> double inc=0.05;
>
> for(;r<=0.25;r+=inc)
> printf("%6.5f\n",r);
>
You should reiterate Lesson no.1: this is the same case. The loop
tests r and 0.25 for EQUALITY. In a previous message I describe the
way to do that: you need to allow for a slack of (in this case)
0.25*DBL_EPSILON, like this:
for ( ; r <= 0.25 * (1 + DBL_EPSILON); r += inc)
> The floating point optimization error, I think, makes the loop one
> step shorter.
No, optimizations rearrange the floating-point operations and use FP
registers of the CPU, so the results are slightly different. Your
program uses code whose behavior is undefined, so you get different
results for different compilers and optimization levels.
- Raw text -