[ILUG] unexpected output from a c program...

Mark Fallon mark.fallon at oracle.com
Wed Feb 28 20:37:21 GMT 2001


kevin lyda wrote:
> 
> why does this...
> 
>     int main(void)
>     {
>         int a = 60, b = 6, c = 10;
> 
>         printf("%d = %d\n", (int) (((60/6)*0.3) + (10*0.7)),
>                             (int) ((( a/b)*0.3) + ( c*0.7)));
>         exit(1);
>     }
> 
> ...yield...
> 
>     10 = 9
> 
> ...on an intel linux box, while it yields...
> 
>     10 = 10
> 
> ...on a sparc solaris box?  both compiled with gcc.  why do i suspect
> to see the letters i (once) and e (thrice) in the answer?
> 

On my Intel based Linux box it comes out with 10 = 10,
when you compile with -05

This is all down to the Intel floating point unit. In
the first part the compiler can do all the calculation,
in the second the executable does it and the extra precision that
is held internally causes the answer to be different. The -O5 option
to the compiler (not always recommended) sees that a and b are
not modified so it does the calculation in both cases.

Regards,

Mark

-- 
_______________________________________________________________

Mark Fallon			E-mail : Mark.Fallon at oracle.com
Senior Software Engineer	Phone  : +353-1-8033207
Global Product Engineering	Fax    : +353-1-8033221
_______________________________________________________________




More information about the ILUG mailing list