Problem

Overflow. What would happen if you went beyond the range for a particular type? For example, the largest integer that can be stored in int8 is 127, and the smallest integer is -128, so what would happen if we type cast a larger integer to the type int8? Smaller integer? Use the MATLAB built-in functions intmin and intmax or Python’s numpy package command iinfo to find the largest and smallest integers that can be stored in int16 and int32.

Solution

The resulting behavior is language-dependent.

MATLAB

In MATLAB, the overflow and underflow as of today freeze to the largest or smallest possible values that could be represented by that variable type. Here are the results in MATLAB:

>> int8(200)
ans =
  int8
   127
>> int8(-150)
ans =
  int8
   -128
>> intmax('int16')
ans =
  int16
   32767
>> intmin('int16')
ans =
  int16
   -32768
>> intmax('int32')
ans =
  int32
   2147483647
>> intmin('int32')
ans =
  int32
   -2147483648
Python

In python, it leads to rollover or perhaps random value assignments to the variable.

Comments