GCC和MSVC之间的数字10的差异 [英] Difference in digits10 between GCC and MSVC

查看:262
本文介绍了GCC和MSVC之间的数字10的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

#include <iostream>
#include <limits>

int main()
{
   std::cout << std::numeric_limits<unsigned long long>::digits10 << std::endl;
   return 0;
}




  • GCC 4.4返回19

  • MS VS 9.0返回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.

    推荐答案

    如果Visual C ++ 2008返回 18 for std :: numeric_limits< unsigned long long> :: digits10 ,这是一个错误(我没有Visual C ++ 2008安装以验证所描述的行为)。

    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).

    在Visual C ++中(至少对于32位和64位Windows), unsigned long long 64位无符号整数类型,并且能够表示0和18,446,744,073,709,551,615(2 64 - 1)之间的所有整数。

    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).

    digits10 的正确值为19,因为 unsigned long long 可表示9,999,999,999,999,999,999(19位数字),但不能表示99,999,999,999,999,999,999 (20位)。

    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.

    当使用Visual C ++ 2010编译时,您的程序打印预期的19个数字。

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

    这篇关于GCC和MSVC之间的数字10的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆