I
I
isvirin2013-08-23 14:43:36
Programming
isvirin, 2013-08-23 14:43:36

Loss of precision in type conversion?

Here is such a function:

void fp()
{
    int64_t tmpn=(((1<<30)-1)/3*2)*1000LL;
    double tmpd=tmpn/1000.0;
    int64_t tmpm=(int64_t)(tmpd*1000.0);
    printf("%lld %.3f %lld\n", tmpn, tmpd, tmpm);
    fflush(stdout);
}

I call it at the beginning and at the end of the main() function. The exhaust is different. At the beginning:
715827882000 715827882.000 715827882000
At the end:
715827882000 715827904.000 715827904512
If you look closely at the number 715827904.000, you can see that 24 bits are left in the mantissa, the rest are zeros. Those. as if the integer was converted not to double, but to float.
It appears only on Windows, in 32-bit mingw. In 64-bit Linux everything is ok.
And now two questions raised by Chernyshevsky:
Who is to blame?
What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
greechnik, 2013-08-23
@isvirin

It looks like someone somewhere inside the functions called from main called the _controlfp function (or the machine instruction fldcw) and didn't get the state back. As a solution, you can call _controlfp(_PC_64, _MCW_PC).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question