This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/11892] New: double to int different if intermediate double used and no optimisation
- From: "simon dot marshall at misys dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 12 Aug 2003 10:02:22 -0000
- Subject: [Bug c/11892] New: double to int different if intermediate double used and no optimisation
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11892
Summary: double to int different if intermediate double used and
no optimisation
Product: gcc
Version: 3.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: simon dot marshall at misys dot com
CC: gcc-bugs at gcc dot gnu dot org
I hesitate to submit this, but I don't think this merits the usual "what do you
expect from doubles?" response. This is with:
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs
Configured with: ../configure --prefix=/usr/local --enable-
languages=c,c++,f77,objc,java,ada --enable-threads=posix --enable-shared --
enable-__cxa_atexit --with-system-zlib
Thread model: posix
gcc version 3.3.1
and the program:
#include <stdio.h>
int main ()
{
double d = 0.1;
double e = 5;
double f = e / d;
int i = (int) f;
int j = (int) (e / d);
printf ("f = %lf\n", f);
printf ("i = %d\n", i);
printf ("j = %d\n", j);
return 0;
}
Notice that i and j are the integer result of 5 / 0.1 but that the intermediate
double f is used to initialise i. I can just about except that 0.1 is
represented by 0.10000000000000001 according to gdb. What does surprise me is
that i and j can have different values:
$ gcc double.cpp && ./a.out
f = 50.000000
i = 50
j = 49
$ gcc -O double.cpp && ./a.out
f = 50.000000
i = 50
j = 50
$ gcc -O -ffloat-store double.cpp && ./a.out
f = 50.000000
i = 50
j = 49
As you can see, i != j unless I use raw optimisation. I can just about except
that the result of the arithmetic is not exactly 50, but surely i should always
be equal to j? In other words, I think i==j==50 or i==j==49 are correct
outcomes but i!=j is not.