c++ - 无法正确比较两个数字

标签 c++

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 longlong long 不是 c++03 中的标准类型,但编译器很可能会使用上面定义的机制来处理这些类型。然后,提到的转换规则将涵盖此特定转换。

关于c++ - 无法正确比较两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694015/

相关文章:

c++ - Data.Aeson 在 ghc 中使用 ForeignFunctionInterface

c++ - 为什么我在传递 vl 或 vl1 时得到与输出相同的值 (0x7fff5fbff808)?地址应该不同

c++ - 将数据从较小的 vector 复制到较大的 vector

c++ - 从 python 解释器调用方法

C++类使用双冒号

c++ - CUDA Thrust - 如何使用多个不同大小的设备 vector 编写函数?

c++ - 使用 unix 终端运行 c++ 程序

c++ - 有没有一种聪明的方法来实现完全静态的观察者模式(在编译时)

c++ - 将十六进制字符串转换为无符号整数问题 C++

c++ - 有没有C++/win32库函数把文件路径转换成文件://URL?