Mail Archives: djgpp/2000/06/27/15:15:30
Erik Berglund <erik2 DOT berglund AT telia DOT com> wrote:
> Hi, I tried the following program, which gives
> j=9999. I think the right result should be 10000,
> because a=0x40c3880000000000, which is exactly 10000.
>
> #include <stdio.h>
> #include <math.h>
> int main() {
> double a;
> int j,*p;
>
> a = pow(100,2);
Although 'a' is a double, it is held for the time being in
the FPU, which is at 80-bit precision. It's value is
0x400C9C3FFFFFFFFFFFFE, which is just less than 10000.
> j = a;
This therefore sets j to 9999.
> p = (int*)&a;
> printf("%08x%08x\n",*(p+1),*p);
> printf("%i\n",j);
> return(0);
> }
>
> If I insert a printf("\n") before j=a;, the result
> becomes 10000 instead, and I have no idea why.
This is because gcc couldn't leave the double in the FPU
during the function call, so it stores it in memory,
converting to 64-bit in the process (because it's a double).
This happens to make it 0x40c3880000000000, which (as you
pointed out) is exactly 10000.
Don't you just love floating point arithmetic?
S.
- Raw text -