|
|
|
|
|
by serpent_skis
987 days ago
|
|
> This is not true if a, b and c are unsigned True, but this is not valid if they are signed, either. Take a = INT_MIN
b = 1
c = 2
Then a < b + c
is true. But a - b < c
invokes undefined behavior.Edit: missed > Say that a, b and c are small integers (we don't worry about addition overflow) Ah, well that makes this example vacuously true, however I'm not sure what the utility in that restriction is. We've only moved the goalposts from "bend[ing] the rules of arithmetic around zero" to bending the rules of arithmetic outside of "small integers". |
|
We have moved the goalposts much farther apart.
If we are using a 32 bit integer type, all we need is that a, b and c fit into 31 bits. Then there is no way that b + c or a - b overflow. For a single addition or subtraction, we just need one bit of headroom.
I.e. the values do not actually have to be that small.
There are all kinds of situations in which programs work with small integers, where the calculations could bork if an unsigned creeps in.
A cliff near zero is qualitatively different from clipping at two extremes. An electronic device that clips everything below zero volts will distort even the faintest waveform. One that clips near the power rails has clean headroom.