Message-Id: Comments: Authenticated sender is From: "Salvador Eduardo Tropea (SET)" Organization: INTI To: sassi AT biomed DOT polimi DOT it (Roberto Sassi), djgpp AT delorie DOT com Date: Wed, 17 Jun 1998 16:08:57 +0000 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: Another silly question about the double 0.1 ... In-reply-to: <3587fb67.19298603@news.polimi.it> Precedence: bulk sassi AT biomed DOT polimi DOT it (Roberto Sassi) wrote: > thank you very much to everyone who had spent time to answer to my > previous silly question about the 0.1 double. > Here I have another question... > > Let's begin with the code ... > > #include > > int main(void) { > > double r=0.1; > double inc=0.05; > > for(;r<=0.25;r+=inc) > printf("%6.5f\n",r); > > } > > I compile the source code, building cicla.exe (in italian "ciclo" > means "loop") The same is in spanish. > D:\Roberto\djgpp\codice>gcc -o cicla.exe cicla.c > > D:\Roberto\djgpp\codice>cicla > 0.10000 > 0.15000 > 0.20000 > 0.25000 > > Now let's see what happen if I use the -O flag, when I compile with my > djgpp compiler .. > > D:\Roberto\djgpp\codice>gcc -O -o cicla.exe cicla.c > > D:\Roberto\djgpp\codice>cicla > 0.10000 > 0.15000 > 0.20000 > > D:\Roberto\djgpp\codice> > > The floating point optimization error, I think, makes the loop one > step shorter. Use an integer for the loop variable. Or add a little bit to the problematic constant, for example: #include #define sup_err 0.000000001 int main(void) { double r=0.1; double inc=0.05; for(;r<=0.25+sup_err;r+=inc) printf("%6.5f\n",r); } This problem is very strange because it doesn't dissapear when you use -ffloat-store. The problem is that 0.1 can't be encoded in a double, the closer value is: 0.10000000000000001 SET ------------------------------------ 0 -------------------------------- Visit my home page: http://set-soft.home.ml.org/ or http://www.geocities.com/SiliconValley/Vista/6552/ Salvador Eduardo Tropea (SET). (Electronics Engineer) Alternative e-mail: set-soft AT usa DOT net set AT computer DOT org ICQ: 2951574 Address: Curapaligue 2124, Caseros, 3 de Febrero Buenos Aires, (1678), ARGENTINA TE: +(541) 759 0013