This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: What the hell is going on? Just a simple division...
- To: help-gcc at gnu dot org
- Subject: Re: What the hell is going on? Just a simple division...
- From: David Wragg <dpw at doc dot ic dot ac dot uk>
- Date: 22 Dec 1999 01:06:36 +0000
- Newsgroups: gnu.gcc.help
- Organization: (Posted via) U-NET Internet Ltd.
- References: <83oqfc$2q4$1@dinkel.civ.utwente.nl>
- Xref: wodc7nx0 gnu.gcc.help:2287
"Paul E.C. Melis" <melis@cs.utwente.nl> writes:
> Using gcc-2.95.2 and linux 2.0.35 (libc5) the
> following program gives as output
> 0
> 1
> instead of the obviously correct output
> 1
> 1
> Is this a compiler bug?
No. Just a pitfall of floating point on x86.
> printf ("%d\n", (int) ( log(2.0) / log(2.0) ));
Look at the generated assembly. One "log(2.0)" gets evaluated to a
double held in an 80-bit FP register. This is then transfered to a
64-bit double in memory, causing it to be rounded. The other
"log(2.0)" is then evaluated to a double held in an 80-bit bit FP
register. The division is then performed. Since only one value was
rounded, the result will not be 1.0, and so on conversion to int may
be truncated to 0.
David Wragg