matlablanguage-designinteger-overflowinteger-arithmetic

Why did Matlab choose saturation arithmetic for integers?


Matlab implements saturation arithmetic instead of wrapping on overflow for integers. While understanding the difference between the two, but not necessarily finer nuances, I wonder what was the motivation and reasoning of Matlab designers behind this choice? Especially given that most other language designers seem to choose differently. At least for mainstream general purpose languages.

Assuming we can't have a "direct reading", e.g., one of the people who was involved with Matlab design frequents StackOverflow or has published an account somewhere (I couldn't find one or any mention of its existence), I think second best would be understanding the benefits of saturation arithmetic beyond obvious properties that Wikipedia article briefly mentions. I.e. why would any language designer choose it?


Solution

  • We cannot really know how the decision process went down, but we can make educated guesses.

    Saturation arithmetic has many advantages over modular arithmetic, as described in the Wikipedia article you link:

    However, although more difficult to implement, saturation arithmetic has numerous practical advantages. The result is as numerically close to the true answer as possible; [...] it is considerably less surprising [...].

    Saturation arithmetic also enables overflow of additions and multiplications to be detected consistently without an overflow bit or excessive computation, by simple comparison with the maximum or minimum value (provided the datum is not permitted to take on these values).

    Additionally, saturation arithmetic enables efficient algorithms for many problems, particularly in digital signal processing. [...]

    Processors implement modular arithmetic because it's much simpler and cheaper to implement. So any software that wants to implement saturation arithmetic needs to do additional work. Languages like C provide access to the underlying hardware facilities, and facilities required by the standard are usually the ones that were common across most hardware at the time the standard was written. For example, C guarantees modular arithmetic, but only for unsigned integers, not for signed ones, where overflow is undefined behavior (the compiler can choose what happens in that case).

    MATLAB was originally written to do linear algebra. In that context, it makes sense to have all numeric data be doubles. But MATLAB was being used in many different areas of engineering, and it turned out MATLAB was really great to deal with large amounts of data. When I started writing DIPimage, a large MATLAB toolbox for image analysis (this was 1999, and MATLAB 5.3), MATLAB was able to create, convert, load and save arrays for specific integer types, but no arithmetic could be done with them. It looks like it was MATLAB 7 that introduced integer arithmetic (in 2004).

    In the introductory newsletter linked above, all the examples for integer arithmetic are for image processing and signal processing. It is likely that most customers requesting integer arithmetic were working in these fields. And in these fields, saturation arithmetic makes a lot of sense, and is worth the extra cost. For example, in DIPlib (a C++ library for image analysis) we also chose to use saturation arithmetic.

    I know of only a few applications where modular arithmetic is truly useful. As programmers, we're used to using it, and rely on it in certain cases, but don't really need it -- if it weren't the default in so many languages, we probably would be happier programmers.