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
有人可以解释一下为什么两者之间有区别吗?我本以为无论编译器如何,这样的常量都是相同的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 Visual C++ 2008 对于
std::numeric_limits::digits10
返回18
,则这是一个错误(我没有安装 Visual C++ 2008 来验证所描述的行为)。在 Visual C++(至少对于 32 位和 64 位 Windows)中,
unsigned long long
是 64 位无符号整数类型,能够表示 0 到 18,446,744,073,709,551,615 之间的所有整数 (2 64 - 1)。因此,这里
digits10
的正确值为 19,因为unsigned long long
可以表示 9,999,999,999,999,999,999(19 位数字),但不能表示 99,999,999,999,999,999,999(20 位数字)。也就是说,它可以表示每19位数字,但不能表示每20位数字。使用 Visual C++ 2010 编译时,您的程序将打印预期的 19。
If Visual C++ 2008 returns
18
forstd::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 anunsigned 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.
numeric_limits::digits10 指定可以在不损失精度的情况下表示的小数点左侧的小数位数。所以,我想不同的编译器会有所不同,具体取决于它们的实现细节。
numeric_limits::digits10 specifies the number of decimal digits to the left of the decimal point that can be represented without a loss of precision. So, I guess it will differ from compiler to compiler depending on their implementation detail.
一般来说,声明
不正确,因为 C 中类型的大小不固定。该标准仅规定了最低限制,编译器可以自由使用更广泛的类型。例如,32 或 64 位计算机上的一些奇怪的编译器可能具有
CHAR_BIT = 9
并且unsigned long long
不再是 64 位,或者它可能使用不同的1 的补码或其他一些数字编码。简而言之,不同编译器的结果可能会有所不同。但是,如果
unsigned long long
是 64 位类型,那么它肯定是一个错误。我刚刚检查了一下,发现该错误已在 VS 2008 中修复。返回了正确的值 19In general, the statement
is not correct because the size of a type in C isn't fixed. The standard only mandates a minimum limit and compilers are free to use wider types. For example some weird compilers on a 32 or 64-bit computer may have
CHAR_BIT = 9
andunsigned long long
wouldn't be 64 bit any more, or it may use different 1's complement or some other number encodings. In short, the result may vary between compilers.However in case that
unsigned long long
is a 64-bit type then it's definitely a bug. I've just checked and saw that the bug has been fixed in VS 2008. The correct value of 19 is returned