0X80000000 - 10 < 0 and 10 - 0X80000000 < 0 gcc version 3.3.4 gets a right result of comparing between 10 and 0X80000000, but gcc version 4.1.0 fails. the test code followed: #include <stdio.h> int main() { int b = 0X80000000; if ((10 - b) < 0) { printf("(10 - b) = %d < 0 is True\n", 10 - b); } else { printf("(10 - b) = %d < 0 is False\n", 10 - b); } if ((b - 10) < 0) { printf("(b - 10) = %d < 0 is True\n", b - 10); } else { printf("(b - 10) = %d < 0 is False\n", b - 10); } return 0; } I had tested the code and got the result: bingo@sttest:~/tmp> ../bin/g++ t.cpp bingo@sttest:~/tmp> a.out (10 - b) = -2147483638 < 0 is True (b - 10) = 2147483638 < 0 is True
(In reply to comment #0) > int b = 0X80000000; > if ((b - 10) < 0) b - 10 is 0xff(...)7ffffff6 and this is an integer overflow. [ cite: c++ standard / $5.5 ] if during the evaluation of an expression the result isn't mathematically defined nor in the range of representable values for its type the behaviuor is undefined, unless such an expression is a constant expression, in which case the program is ill-formed. (...) [ /cite ] so, use -fwrapv (man g++) to get not quite correct behaviour.
This is signed int overflow being undefined, in fact I should I know because I was the one who write the patch to optimize this.
Ok, you are right. it would be nice if g++ 4.1.0 acts as what g++ 3.3.4 does.
(In reply to comment #3) > Ok, you are right. it would be nice if g++ 4.1.0 acts as what g++ 3.3.4 does. Use -fwrapv if you want defined wrapping.
*** Bug 30475 has been marked as a duplicate of this bug. ***