Closed MageFromAntares closed 4 years ago
This isn't a bug this is actually right behavior.
JSON and JavaScript in general requires numbers to be represented as at least double and this is what cppcms::json does - it keeps values as double.
INT64_MAX
can't be represented exactly in double values that has mantissa of 53 bits and thus can't represnet 64 bit integer correctly.
Now take a look on the sample:
#include <iterator>
#include <iostream>
int main()
{
double v=9223372036854775807.0;
std::cout << std::fixed;
std::cout << "Double " << v << std::endl;
std::cout << "Int64 MAX " << INT64_MAX << std::endl;
long long iv = static_cast<long long>(v);
std::cout << "Casted " << iv << std::endl;
std::cout << "Int=doub " << (v==iv) << std::endl;
}
Casting to long long will lead to negative number which is clearly wrong. this the behavior is correct
Hi, When storing large numbers in a JSON (for example INT64_MAX) a bad_cast error gets thrown instead of returning the value even when the target data type can store the value.
The same happens when using int64_t.
Thanks.