0

I am trying to convert float value to long long int, using std::llround. But there seems to have some precision or "overflow" issue. I am not sure. The code looks like this:

#include <iostream>

int main()
{
    float value = 123456.0f;

    value *= 1.0E+6f;

    std::cout << value << std::endl; // 1.23456e+11

    std::cout << "after rounding: " << std::llround(value) << std::endl; // 123455995904

    return;
}

The float value is 1.23456e+11, but using std::cout, the converted integer looks like 123455995904. I would expect it to be something like '123456000000'. How can I achieve that?

If I do the rounding directly on double value :

std::cout << "rounding double: " << std::llround(123456000000.0) << std::endl;

Then the long integer becomes expected: 123456000000.

(I am using Visual Studio 2019 in Windows 10.) Many thanks!

David
  • 1,505
  • 2
  • 10
  • 7
  • 2
    The full name of `float` datatype is "single-precision floating point" and `double` is "double-precision floating point". You should not be surprised that the precision is less for `float` than `double`... – Ben Voigt Nov 21 '22 at 23:19
  • @BenVoigt Thanks for your comment. Do you mean the "some" precision is lost during the rounding process/computation, when using float? – David Nov 21 '22 at 23:25
  • 1
    This value is unrepresentable in `float`. You can see it e.g. with `std::cout << std::fixed << value` and for instance check the output for `value = (value - 0.01f) * 1.0E+6f` it will be the same as for `value * 1.0E+6f`. – dewaffled Nov 21 '22 at 23:26
  • 3
    @David: The multiplication `123456.0f * 1.0E+6f` computes and stores the result to single precision (it stores the nearest value, representable in a `float`, to the true result). That nearest representable value is 123455995904 – Ben Voigt Nov 21 '22 at 23:29
  • 1
    For the record: float can accurately represent integers up to about 2^24. Your value closer to 2^37 – Homer512 Nov 21 '22 at 23:32
  • @Homer512 Thanks a lot for the link to the other answer. It does explain a lot. – David Nov 21 '22 at 23:35

0 Answers0