4 ms·
Note that f + 1 > f can also fail for integers in many languages (e.g. it does not hold for signed integers in C or C++, because the behavior is undefined when
by stephencanon 7y ago
Note that f + 1 > f can also fail for integers in many languages (e.g. it does not hold for signed integers in C or C++, because the behavior is undefined when you add 1 to INT_MAX, and unsigned integers always wrap around). This particular gotcha is not unique to floating-point.
- nayuki 7y agoYes, signed overflow can make f+1 > f be false. However in C/C++, because signed overflow is undefined behavior, the compiler is perfectly allowed to simplify the expression f+1 > f to always be true.
- stephencanon 7y agoThe compiler can make that simplification, but there's no guarantee that every compiler does, so you cannot assume that it will do so; it will sometimes happen to be true in a specific compiler, but it is never true in the standardized language model (which is what programs are written against).
- yoz-y 7y agoWhich would then have the same effect as the title's article, a bug with -O0 but working with -O3
- BubRoss 7y agoIt _could_ have that effect, but it would depend on signed integers wrapping.
- kbp 7y agoUnsigned integers are still specified to wrap.