Take the 2-minute tour ×

For the below program I am getting precision loss of 1 which I am unable to understand. Need help.

void main()
    typedef std::numeric_limits< double > dbl;

    double x = -53686781.0;
    float xFloat = (float) x;

    cout << "x :: " << x << "\n";
    cout << "xFloat :: " << xFloat << "\n";

x :: -53686781
xFloat :: -53686780
share|improve this question
floats store less information than doubles; what you're seeing is just a manifestation of that. Is there anything specific you don't understand? –  Philip Kendall Mar 20 '13 at 11:29
Moreover, floats have about 7 decimal digits of precision, so it's no surprise that the last digit in the string representation of xfloat is 0. –  High Performance Mark Mar 20 '13 at 11:32
@HighPerformanceMark That's some very weird reasoning there. We're dealing with binary numbers, the decimal 0 at the end is pure coincidence as the higher bits that are set for a number which is divisible by 10. If OP had picked double x = -53686777.0;, xFloat would have been -53686776 (and that doesn't end in 0). –  us2012 Mar 20 '13 at 12:11
add comment

2 Answers

For normal floats I believe p=23, which gives 2^23 of digit precision (about 7 digits as already mentioned. Double has p=52, which gives 2^52 of digit precision (about 15 digits).

The wiki page is actually pretty good.

share|improve this answer
That's not very precise at all. The binary representation of a float has 23 bits for the mantissa, but a leading 1 is implicit, so you actually get a precision of 24 bits, leading to the result that Alexey Frunze has illustrated in his answer. –  us2012 Mar 20 '13 at 12:10
add comment

53686781 looks like this in binary: 11001100110011000111111101. That's 26 bits.

Your float can only store up to 24 bits in its mantissa portion, so, you end up with 110011001100110001111111 stored in it. The last two binary digits, 01, get truncated.

And 11001100110011000111111100 is 53686780.

As simple as that.

share|improve this answer
The last two bits in the complete significand (not mantissa) are rounded, not truncated. In this case, where they are 01, the result is the same, but it is different in almost half the cases. –  Eric Postpischil Mar 20 '13 at 13:02
@EricPostpischil Yeah, they should be rounded and the effect is the same in this case. –  Alexey Frunze Mar 20 '13 at 13:03
add comment

Your Answer


