This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/16403] New: Floating-point assignment to int is inconsistent
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 7 Jul 2004 08:27:04 -0000
- Subject: [Bug c/16403] New: Floating-point assignment to int is inconsistent
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The program shown below assigns different values to i and j.
The difference is that the result of '(int)5 * (double)1.2' is
stored in an intermediate double-precision variable 'g' before it is
assigned to j. This causes different rounding to take place. So the
same double-precision calculation is rounded, or truncated, in
two different ways when assigned to an int.
Environment:
System: Linux goya 2.4.18-3custom #1 SMP Wed Oct 16 18:51:01 IST 2002 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.4.1/configure --prefix=/usr/local/gnu/gcc/3.4.1-fixed --enable-languages=c,c++,f77 --with-gcc-version-trigger=/vobs/tools/xde/gcc-3.4.1/gcc/version.c --no-create --no-recursion
How-To-Repeat:
Compile the following program with no options ('gcc f.c') and
run a.out. The output will be 'i = 5, j = 6'.
/***/
extern int printf (char*, ...);
int main (void)
{
int j, i = 5;
double g = i * 1.2;
j = g;
i *= 1.2;
printf("i = %d, j = %d\n",i,j);
return 0;
}
/***/
--
Summary: Floating-point assignment to int is inconsistent
Product: gcc
Version: 3.4.1
Status: UNCONFIRMED
Severity: critical
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: yuvalk at mainsoft dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16403