On my platform this prints 9223372036854775808.
double x = 1e19;
std::cout << static_cast<unsigned __int64>(x) << '\n';
I tried Boost.NumericConversion, but got the same result.
Splitting x into 2 equal part, then adding together converted halves give the correct result. But I need a generic solution to use in a template code.
Thank you in advance.
EDIT:
This problem shows up on Visual Studio 2008, but not MinGW. Casting 4.0e9 into unsigned long works fine.
Seems like it works well with gcc, but it is problematic in Visual Studio. See Microsoft’s answer regarding this issue:
So you can only convert the numbers in the signed 64-bit integer range: −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (-2^63~2^63-1).