问题描述
-2147483648是具有32位的整数类型的最小整数,但似乎它会在 if(...)句中溢出:
if(-2147483648> 0)
std :: cout< 真正;
else
std :: cout<< 假;
这将在我的测试中打印 true 但是,如果我们将-2147483648转换为整数,结果将不同:
if(int(-2147483648)> )
std :: cout<< 真正;
else
std :: cout<< 假;
这将打印 false 。
我很困惑。谁能给出这个解释?
更新02-05-2012:
$感谢您的意见,在我的编译器,int的大小是4字节。我使用VC进行一些简单的测试。我已更改了我的问题中的说明。
这篇文章中有很多很好的回复,给出了关于编译器如何对这种输入进行行为的详细解释,以及如何实现此最小整数。另一方面,提供了一些相关的好奇心以及如何表示整数。令人印象深刻!
-2147483648 不是数字。 C ++语言不支持负的文字值。
-2147483648 实际上是一个表达式: c $ c> 2147483648 在其前面有一元的 - 运算符。值 2147483648 对于您的平台上的 int 范围的正面来说显然太大了。如果类型 long int 在你的平台上有更大的范围,编译器将不得不自动假设 2147483648 有 long int 类型。 (在C ++ 11中,编译器还必须考虑 long long int 类型。)这将使编译器评估 -2147483648 在较大类型的域中,结果将是负的,正如人们所期望的。
但是,显然在您的情况下, long int 的范围与 int ,通常在您的平台上没有大于 int 的整数类型。这正式意味着正常数 2147483648 溢出所有可用的有符号整数类型,这反过来意味着您的程序的行为是未定义的。 (这是有点奇怪,语言规范选择在这种情况下未定义的行为,而不是要求诊断消息,但这是它的方式。)
在实践,考虑到行为是未定义的, 2147483648 可能会被解释为一些实现相关的负值,在一元的 - 应用于它。或者,一些实现可能决定尝试使用无符号类型来表示值(例如,在C89 / 90编译器中需要使用 unsigned long int ,但不能在C99或C ++)。
另外,这也是为什么像 INT_MIN
这样的常量的原因, code>通常定义为#define INT_MIN(-2147483647 - 1)
而不是看起来更简单
#define INT_MIN -2147483648
后者无法正常工作。
-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the if(...) sentence:
if (-2147483648 > 0) std::cout << "true"; else std::cout << "false";This will print true in my testing. However, if we cast -2147483648 to integer, the result will be different:
if (int(-2147483648) > 0) std::cout << "true"; else std::cout << "false";This will print false.
I'm confused. Can anyone give an explanation on this?
Update 02-05-2012:
Thanks for your comments, in my compiler, the size of int is 4 bytes. I'm using VC for some simple testing. I've changed the description in my question.
That's a lot of very good replys in this post, AndreyT gave a very detailed explanation on how the compiler will behaviour on such input, and how this minimum integer was implemented. qPCR4vir on the other hand gave some related "curiosities" and how integer are represented. So impressive!
解决方案-2147483648 is not a "number". C++ language does not support negative literal values.
-2147483648 is actually an expression: a positive literal value 2147483648 with unary - operator in front of it. Value 2147483648 is apparently too large for the positive side of int range on your platform. If type long int had greater range on your platform, the compiler would have to automatically assume that 2147483648 has long int type. (In C++11 the compiler would also have to consider long long int type.) This would make the compiler to evaluate -2147483648 in the domain of larger type and the result would be negative, as one would expect.
However, apparently in your case the range of long int is the same as range of int, and in general there's no integer type with greater range than int on your platform. This formally means that positive constant 2147483648 overflows all available signed integer types, which in turn means that the behavior of your program is undefined. (It is a bit strange that the language specification opts for undefined behavior in such cases, instead of requiring a diagnostic message, but that's the way it is.)
In practice, taking into account that the behavior is undefined, 2147483648 might get interpreted as some implementation-dependent negative value which happens to turn positive after having unary - applied to it. Alternatively, some implementations might decide to attempt using unsigned types to represent the value (for example, in C89/90 compilers were required to use unsigned long int, but not in C99 or C++). Implementations are allowed to do anything, since the behavior is undefined anyway.
As a side note, this is the reason why constants like INT_MIN are typically defined as
#define INT_MIN (-2147483647 - 1)instead of the seemingly more straightforward
#define INT_MIN -2147483648The latter would not work as intended.
这篇关于(-2147483648> 0)在C ++中返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!