(-2147483648> 0) 在 C++ 中返回真?

(-2147483648gt; 0) returns true in C++?((-2147483648 0) 在 C++ 中返回真?)

本文介绍了(-2147483648> 0) 在 C++ 中返回真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-2147483648是32位整数类型的最小整数,但是在if(...)语句中好像会溢出:

-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";

这将在我的测试中打印 true.但是,如果我们将 -2147483648 强制转换为整数,结果会有所不同:

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";

这将打印false.

我很困惑.谁能解释一下?

I'm confused. Can anyone give an explanation on this?

2012 年 2 月 5 日更新:

Update 02-05-2012:

感谢您的评论,在我的编译器中,int 的大小为 4 个字节.我正在使用 VC 进行一些简单的测试.我已更改问题中的说明.

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.

这篇文章中有很多非常好的回复,AndreyT 非常详细地解释了编译器的行为方式在这样的输入上,以及这个最小整数是如何实现的.qPCR4vir 另一方面给出了一些相关的好奇心"以及如何表示整数.太感人了!

That's a lot of very good replys in this post, AndreyT gave a very detailed explanation on how the compiler will behave on such input, and how this minimum integer was implemented. qPCR4vir on the other hand gave some related "curiosities" and how integers are represented. So impressive!

推荐答案

-2147483648 不是数字".C++ 语言不支持负字面值.

-2147483648 is not a "number". C++ language does not support negative literal values.

-2147483648 实际上是一个表达式:一个正字面值 2147483648 前面带有一元 - 运算符.值 2147483648 显然对于您平台上 int 范围的积极方面来说太大了.如果类型 long int 在您的平台上具有更大的范围,编译器将必须自动假定 2147483648 具有 long int 类型.(在 C++11 中,编译器还必须考虑 long long int 类型.)这将使编译器在更大类型的域中评估 -2147483648正如人们所期望的那样,结果将是负面的.

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

但是,显然在您的情况下,long int 的范围与 int 的范围相同,并且通常没有比 范围更大的整数类型int 在您的平台上.这正式意味着正常量 2147483648 溢出所有可用的有符号整数类型,这反过来意味着您的程序的行为是未定义的.(语言规范在这种情况下选择未定义的行为,而不是需要诊断消息,这有点奇怪,但事实就是这样.)

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

在实践中,考虑到行为是未定义的,2147483648 可能会被解释为一些依赖于实现的负值,在应用一元 - 后恰好变成正值到它.或者,某些实现可能决定尝试使用无符号类型来表示值(例如,在 C89/90 中,编译器需要使用 unsigned long int,但在 C99 或 C++ 中则不需要).实现可以做任何事情,因为无论如何行为都是未定义的.

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.

作为旁注,这就是为什么像 INT_MIN 这样的常量通常被定义为

As a side note, this is the reason why constants like INT_MIN are typically defined as

#define INT_MIN (-2147483647 - 1)

而不是看似更直接的

#define INT_MIN -2147483648

后者不会按预期工作.

这篇关于(-2147483648> 0) 在 C++ 中返回真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:(-2147483648> 0) 在 C++ 中返回真?

基础教程推荐