Implicit Overflow Considered Harmful, and how to fix it
While the article discusses possible solutions to an important problem and it may have some good ideas, I strongly dislike the choice of words of the author, because such inappropriate terminology is one of the main causes why there are problems with handling integers.<p>When using a bit string with N bits, e.g. a 64-bit bit string, it is possible to implement 3 distinct integer types.<p>Two types are subtypes of the integer numbers in the mathematical sense, i.e. interval types a.k.a. range types, which can be either signed, e.g. with values in the interval [-2^63, 2^63-1], or unsigned, e.g. with values in the interval [0,2^64-1].<p>The third type are modular numbers, e.g. numbers modulo 2^64.<p>Unfortunately I am not aware of any programming language that either allows the use of all 3 kinds of types or at least uses appropriate names for the available types.<p>For example, the types named &quot;unsigned&quot; in C and C++ are not unsigned numbers, but modular numbers. They should never be used for a variable that is intended to belong to an interval of integer numbers, unless there is certainty that during any computations with that value it is impossible for reductions by the modulus to appear.<p>The wrapping behavior of modular numbers is not an &quot;implicit overflow&quot;, it is their normal behavior according to their definition. There is no overflow of modular numbers.<p>On the other hand, for the interval numbers (e.g. C&#x2F;C++ &quot;int&quot; or &quot;long&quot;) a decent programming language should define what happens on integer overflow.<p>The fact that many modern programming languages have chosen to specify that the behavior on integer overflow is undefined is extremely annoying and this is the root cause of all bugs caused by overflows.<p>There are 2 acceptable behaviors, the same as for floating point numbers, either integer saturation, where the interval limits are considered infinities, which are the result of overflows and then they are preserved by all subsequent operations until they are tested later, or an integer overflow exception signaled immediately.<p>Most ancient computers and programming languages always generated exceptions on integer overflow.<p>Ignoring integer overflow is a modern method of cost reduction which moves the burden from CPU designers &amp; manufacturers to the programmers who must now either be able to predict correctly whether integer overflows will ever happen or compile with options for overflow checking.<p>Due to the poor support for integer overflow checking in most modern CPUs, this option causes a much greater performance penalty than really necessary.<p>Fortunately the existence of the IEEE 754 standard has prevented such cost reduction measures for floating-point operations, but for integer operations there is no standard to force the CPU makers to do the right thing.