mercredi 31 mai 2017

Why is std::abs(9484282305798401ull) = 9484282305798400?

It turns out that on C++17 (g++ 6.3.1),

#include <iostream>
#include <cmath>

int main()
{
    std::cout << (unsigned long long)std::abs(9484282305798401ull);
}

gives an incorrect output of 9484282305798400.

As I understood from cmath, std::abs first casts the argument into a double.

According to the C++ docs, double has 52 mantissa bits, which means that the maximum integer value that I must be strictly less than 2^52 = 4503599627370496 before any loss of precision.

Am I correct to say that since 9484282305798401 exceeds this limit, std::abs ends up discarding precision to give an incorrect answer?

Aucun commentaire:

Enregistrer un commentaire