c++ int型比较问题

荔枝树_12w 2014-03-30 12:14:14
直接上代码:
代码如下
cin >> a >> b >> c;
if(a < 0 && (b < (-2147483648-a)))
{
cout << b << (-2147483648-a) << " " << endl;;
cout << "flase";
}

其中保证 a,b,c均不会发生溢出。
在输入为-2147483648, -1, -2147483648时,if语句无法进入,改成
cin >> a >> b >> c;
if(a < 0 && (b > (-2147483648-a)))
{
cout << b << (-2147483648-a) << " " << endl;;
cout << "flase";
}

则能进入 而且输出为
-1 0
false

不知到为什么C++中将b < (-2147483648-a) 判断为负。
实在Ubuntu12.04.4下的code::block 10.05下编译的, 未在其他IDE上编译过。
求各位大神答疑解惑!
...全文
240 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
荔枝树_12w 2014-04-01
  • 打赏
  • 举报
回复
引用 13 楼 FancyMouse 的回复:
[quote=引用 9 楼 u013107295 的回复:] [quote=引用 7 楼 lm_whales 的回复:] 严格来说 -2147483648 其实已经溢出了,32Bits带符号整型常量,负数最小是 -2147483647, 最小可表示整数是-2147483648 ,表示为常量,要用 -2147483647 -1。 输入数据为-2147483648其实已经溢出。
改成
cin >> a >> b >> c;
if(a < 0 && (b < (-2147483647 - 1 -a)))
{
    cout << b << (-2147483648-a) << " " << endl;;
    cout << "flase";
}
就可以了,谢谢啦。 还有 int型的范围不是 -2147483648 ~ 2147483647 么, 意思是说常量和变量的范围不同么。 [/quote] -2147483648被理解成先有常数2147483648再对它取负。但是2147483648已经超出int的表示范围了。在C++03里是未定义行为,在C++11里编译器必须把它解释成long long。 解决方法7L已经说了。[/quote] 谢谢啦,很感谢!
lm_whales 2014-03-31
  • 打赏
  • 举报
回复
引用 13 楼 FancyMouse 的回复:
[quote=引用 9 楼 u013107295 的回复:] [quote=引用 7 楼 lm_whales 的回复:] 严格来说 -2147483648 其实已经溢出了,32Bits带符号整型常量,负数最小是 -2147483647, 最小可表示整数是-2147483648 ,表示为常量,要用 -2147483647 -1。 输入数据为-2147483648其实已经溢出。
改成
cin >> a >> b >> c;
if(a < 0 && (b < (-2147483647 - 1 -a)))
{
    cout << b << (-2147483648-a) << " " << endl;;
    cout << "flase";
}
就可以了,谢谢啦。 还有 int型的范围不是 -2147483648 ~ 2147483647 么, 意思是说常量和变量的范围不同么。 [/quote] -2147483648被理解成先有常数2147483648再对它取负。但是2147483648已经超出int的表示范围了。在C++03里是未定义行为,在C++11里编译器必须把它解释成long long。 解决方法7L已经说了。[/quote] ++ 谢谢,解说的真详细,我只是在编译器的头文件里,看到 最小整型被定义为 -2147483647-1 ,而不是 -2147483648 所以才认为 -2147483648已经溢出了。 标准我不是很清楚
FancyMouse 2014-03-31
  • 打赏
  • 举报
回复
引用 9 楼 u013107295 的回复:
[quote=引用 7 楼 lm_whales 的回复:] 严格来说 -2147483648 其实已经溢出了,32Bits带符号整型常量,负数最小是 -2147483647, 最小可表示整数是-2147483648 ,表示为常量,要用 -2147483647 -1。 输入数据为-2147483648其实已经溢出。
改成
cin >> a >> b >> c;
if(a < 0 && (b < (-2147483647 - 1 -a)))
{
    cout << b << (-2147483648-a) << " " << endl;;
    cout << "flase";
}
就可以了,谢谢啦。 还有 int型的范围不是 -2147483648 ~ 2147483647 么, 意思是说常量和变量的范围不同么。 [/quote] -2147483648被理解成先有常数2147483648再对它取负。但是2147483648已经超出int的表示范围了。在C++03里是未定义行为,在C++11里编译器必须把它解释成long long。 解决方法7L已经说了。
shiguojie19892 2014-03-31
  • 打赏
  • 举报
回复
引用 8 楼 unituniverse2 的回复:
编译器没问题 有符号数运算结果溢出后实际等于多少是未定义的 所以处理成什么结果都不能算编译器的责任。原本编译器该做的都做到了。 想结果靠的住的话在提交给第一个if之前就得预算好结果而不让发生溢出。 所以再加些判断语句吧
you are right
赵4老师 2014-03-31
  • 打赏
  • 举报
回复
C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
求大神们帮忙啊
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
引用 8 楼 unituniverse2 的回复:
编译器没问题 有符号数运算结果溢出后实际等于多少是未定义的 所以处理成什么结果都不能算编译器的责任。原本编译器该做的都做到了。 想结果靠的住的话在提交给第一个if之前就得预算好结果而不让发生溢出。 所以再加些判断语句吧
这个我也知道的,就是不知道-2147483648 也是溢出的。
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
引用 7 楼 lm_whales 的回复:
严格来说 -2147483648 其实已经溢出了,32Bits带符号整型常量,负数最小是 -2147483647, 最小可表示整数是-2147483648 ,表示为常量,要用 -2147483647 -1。 输入数据为-2147483648其实已经溢出。
改成
cin >> a >> b >> c;
if(a < 0 && (b < (-2147483647 - 1 -a)))
{
    cout << b << (-2147483648-a) << " " << endl;;
    cout << "flase";
}
就可以了,谢谢啦。 还有 int型的范围不是 -2147483648 ~ 2147483647 么, 意思是说常量和变量的范围不同么。
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
引用
你输出一下a、b、c看看实际上接收到的数据是多少。
单步调试时watch过变量 是正确值的
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
这样么。那这算是编译器漏洞?
PDD123 2014-03-30
  • 打赏
  • 举报
回复
你输出一下a、b、c看看实际上接收到的数据是多少。
逸萌 2014-03-30
  • 打赏
  • 举报
回复
应该是编译器问题,VC OK
荔枝树_12w 2014-03-30
  • 打赏
  • 举报
回复
求助啊!自己怎么想也想不通…
unituniverse2 2014-03-30
  • 打赏
  • 举报
回复
编译器没问题 有符号数运算结果溢出后实际等于多少是未定义的 所以处理成什么结果都不能算编译器的责任。原本编译器该做的都做到了。 想结果靠的住的话在提交给第一个if之前就得预算好结果而不让发生溢出。 所以再加些判断语句吧
lm_whales 2014-03-30
  • 打赏
  • 举报
回复
严格来说 -2147483648 其实已经溢出了,32Bits带符号整型常量,负数最小是 -2147483647, 最小可表示整数是-2147483648 ,表示为常量,要用 -2147483647 -1。 输入数据为-2147483648其实已经溢出。

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧