unsigned long long value = 0;
bool result = value >= std::numeric_limits<signed int>::min();
这应该给出 true 但给出 false?为什么以及如何解决它?
最佳答案
int 转换为无符号 int 以进行比较。您可以将 unsigned long long
转换为 long long
。
5/9:
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
...
Then, if either operand is unsigned long the other shall be converted to unsigned long.
...
Otherwise, if either operand is unsigned, the other shall be converted to unsigned.
unsigned long long
和 long long
不是 c++03 中的标准类型,但编译器很可能会使用上面定义的机制来处理这些类型。然后,提到的转换规则将涵盖此特定转换。
关于c++ - 无法正确比较两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694015/