3 ms·
This is an misguided and dangerous article. Suppose that a, b and c are small values close to zero and so can be combined additively/subtractively in any combi
by kazinator 5y ago
This is an misguided and dangerous article.
Suppose that a, b and c are small values close to zero and so can be combined additively/subtractively in any combination without overflow. If they are signed, we can rely on algebra, like:
a - b > c // this expression
a - b - c > 0 // can become this expression
a - c > b // or this
This breaks for unsigned; you cannot just change the sign of c and move it to the other side of the inequality.
Even when you're not actually doing this algebra in the code, it's useful to be able to do it outside of the code. Sometimes in the code too.
For instance, suppose now that the values are not trivial. We have no assurance that a - b will not overflow, but we do have assurance from somewhere that a - c will not. The rewrite a - c > b is then helpful in avoiding overflow, and easily provable to be equivalent to the a - b > c expression that naturally comes from the problem specification.