Inconsistent results casting double to int on x86

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On x86 the following code intended to calculate log2(4096) gives an
unexpected result of 11 when the value is cast directly to an int. If
the result is stored in a double and then cast to an int the expected
value (12) is given.


double r0;
double d_msb;
int i_msb;

r0=4096;

i_msb = (int)(log10(r0)/log10(2));
printf("%d\n",i_msb);

d_msb = (log10(r0)/log10(2));
i_msb = (int)d_msb;
printf("%d\n",i_msb);


# uname -srm
Linux 2.4.9-34smp i686
# g++ -o log log.c
# ./log
11
12


I have tried several versions of gcc on x86 and all give the same
behaviour. (3.0, 3.0.1,  3.0.4,  3.2,  3.2.2,  3.3.1)

On Solaris 8 this code gives the expected output (12 12). AMD64 with
-m64 gives the expected results but with -m32 the results differ (11
12).

Why do the two values differ? If it is the case that a
rounding/precision error is causing an off by one result, should it
not be consistent between the two forms of the code?


Thanks,

Andrew


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux