Quote:

" writes:

>Hi,

>I have the following code:

>--------------

>#include <stdio.h>

>#include <math.h>

>int a;

>double x,y,c;

>void main (void) {

main returns int in C, always.

Quote:

> x = log(2.0);

> y = log(64.0);

> a = (int)(y / x);

> printf ("%f %i\n",y/x,a);

>}

>---------------

>What I get is

>6.0000 5

>on my Intel-based machine. The same Program compiled and started on a sun or

>sgi will give me

>6.0000 6

>My question is:

>Does printf round the result of y/x ? Or why do I get such results ?

This has nothing to do with printf. When you convert a floating point value

to an integer type the fractional part of the result is discarded.

Floating point arithmetic doesn't in general guarantee exact results so

y/x might evaluate to something like 5.9999999999.. which when converted

to int will end up as 5. Change The %f to something like %.20f to see

a much better approximation of what y/x evaluated to. Be aware however that

intermediate results can be held at a higher precision than values

stored in variables so you may get more consistent results if you write:

c = y / x;

and use c instead of y / x in subsequent expressions.

--

-----------------------------------------

-----------------------------------------