Hacker News new | ask | show | jobs
by kazinator 3701 days ago
> And if the behavior of overflow is to trap, then it should trap regardless of the order you add the numbers in.

How come? Suppose A + B doesn't trap, and if T0 = A + B, then T0 + C doesn't trap. Why should (A + B) + C trap?

2 comments

Say you have a register whose range is -9..+9. (5-1)+5 doesn't trap but (5+5)-1 does.
I understand how to make an example of this; the question is why should (5-1)+5 trap, just because (5+5)-1 does.
Ah, good point! I was thinking of unsigned overflow, but signed overflow is a different matter, as pointed out elsewhere in this thread. I retract my statement about trapping overflow.