-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 ramrunner wrote: > float a; > int main() > { > for(;a<3;a+=0.1) > printf("%f\n",a); > return 1; > } > output : > 0.000000 > 0.100000 > .... > 2.700000 > 2.799999 > 2.899999 > 2.999999 > why does the add loses a decimal point?
float is so tricky... Never use it for precision. For example : float a,b; if(a==b) {} may fail. if( abs(a-b) < 0.00001 ) {} may work. You may want to look ceil(3) and floor(3) as well. Using printf("%.2f\n"a) works for me. Error depends on the precision. I would rather use something like that : int main(void) { int a=0; for(a=0; a<30; a++) printf("%f\n",a/10.); /* Do not forget the '.' */ /*or printf("%d.%d\n", a/10, a%10); depending on the context. */ return 1; } cheers, theo Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFD1Cq0SH6NzHMSyhIRAswgAJ9L6DvPZR03WF9VCQ3KAd+YsGlCVwCfcP9U ++cEzwTwb7Cxi3P7SdyWPIw= =bjmn -----END PGP SIGNATURE-----