Search code examples
c++visual-c++gccdigitsnumeric-limits

Difference in digits10 between GCC and MSVC


I have the following code:

#include <iostream>
#include <limits>

int main()
{
   std::cout << std::numeric_limits<unsigned long long>::digits10 << std::endl;
   return 0;
}
  • GCC 4.4 returns 19
  • MS VS 9.0 returns 18

Can someone please explain Why is there a difference between the two? I would have expected such a constant would be the same regardless of the compiler.


Solution

  • If Visual C++ 2008 returns 18 for std::numeric_limits<unsigned long long>::digits10, it is a bug (I don't have Visual C++ 2008 installed to verify the described behavior).

    In Visual C++ (at least for 32-bit and 64-bit Windows), unsigned long long is a 64-bit unsigned integer type and is capable of representing all of the integers between zero and 18,446,744,073,709,551,615 (264 - 1).

    Therefore, the correct value for digits10 here is 19 because an unsigned long long can represent 9,999,999,999,999,999,999 (19 digits) but cannot represent 99,999,999,999,999,999,999 (20 digits). That is, it can represent every 19 digit number but not every 20 digit number.

    When compiled with Visual C++ 2010, your program prints the expected 19.