Mail Archives: djgpp/1998/06/17/15:07:26
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 <stdio.h>
>
> 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 <stdio.h>
#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
- Raw text -