2 ms·
"Mathematically correct" doesn't seem like the right way to think about it, since the whole problem is that it isn't correct!
by iainmerrick 9y ago
"Mathematically correct" doesn't seem like the right way to think about it, since the whole problem is that it isn't correct!
- tom_mellior 9y agoYes, but it wouldn't be correct the other way round either. Wraparound on signed overflow is a well-defined operation on CPUs, but it is nonsensical. In almost no computation will it give a result that is meaningful in the context of what the program is trying to do. If you try to compute the average of two positive numbers as (a + b) / 2 but get a negative result, it would be of no use if this were "well-defined". So from the point of view of a C compiler that exploits undefined signed overflow, if your code could overflow, it was probably already buggy, and you should have fixed it. (One problem with this line of reasoning is that it kind of also applies to unsigned numbers, but there C goes the other way.)