Date: Thu, 18 Jun 1998 09:45:44 +0300 (IDT) From: Eli Zaretskii To: Roberto Sassi cc: djgpp AT delorie DOT com Subject: Re: Another silly question about the double 0.1 ... In-Reply-To: <3587fb67.19298603@news.polimi.it> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk 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.