gcc strangeness
Bruce R. Montague
brucem at mail.cruzio.com
Sun Jul 11 11:37:29 PDT 2004
Hi, re:
>one of my friends has raisen very strange issue regarding gcc rounding:
> printf("%f %.3f %d\n", a*100, a*100, (int)(a*100));
>
> 9.999999 10.000 9
Hasty unresearched guess:
If you print with a large fp fmt (say 22.18)
you will get a better idea of the value:
9.999999403953552246 10.000 9
The "%.3f" says to round upward to inf after 3 decimal
places, so "9.9999" is rounded to "10.000".
The "%f" defaults to round up after 6 decimal places,
so "9.9999994" is rounded to ""9.999999".
Everything is working.
There are a lot of subtleties in floating-point
printf(). Printing binary values out and reading them
back accurately can be a non-trivial exercise.
- bruce
More information about the freebsd-hackers
mailing list