5*1.2 = 5 or 5*1.2 = 6 ?

Momchil Velikov velco@fadata.bg
Tue Jul 16 04:28:00 GMT 2002


>>>>> "Jocelyn" == Jocelyn Fournier <joc@presence-pc.com> writes:
Jocelyn> #include <stdio.h>

Jocelyn> main()
Jocelyn> {
Jocelyn>      int x = 5;
Jocelyn>      x *= 1.2;
Jocelyn>      printf("5 x 1.2 = %d\n", x);
Jocelyn> }

Jocelyn> I've compiled it with gcc 3.1

Jocelyn> First try :

Jocelyn> gcc -o code1 code1.c

Jocelyn> [root@forum] /home/joce> ./code1
Jocelyn> 5 x 1.2 = 5

Jocelyn> Second try :

Jocelyn> gcc -O -o code1 code1.c
Jocelyn> [root@forum] /home/joce> ./code1
Jocelyn> 5 x 1.2 = 6

Jocelyn> Why does the compiler return a different result if the -O is used ?

Differend rounding performed by the program (with no optimizations)
and the compiler (with optimizations).

~velco



More information about the Gcc-help mailing list