What is overflow?

Overflow is a situation when an arithmetic operation results in a value that is too large to be stored in the specified datatype.

Let’s consider the following example:

int a = 2147483647;
a++;
printf (“value of a is %d\n”, a);

The range for an int datatype (assuming its size to be 4 bytes) is -2147483648 to 2147483647. Hence incrementing the value which is INT_MAX, results in a value which is too large to be stored in datatype int. The output of above program is:

value of a is -2147483648

What is underflow?

Underflow is a situation when an arithmetic operation results in a value that is too small to be stored in the specified datatype.

Let’s consider the following example:

float num = FLT_EPSILON;
float val1, val2;
val1 = num/10;
val2 = num/100;

In above example, variable num represents the smallest number a float datatype can have. Now dividing num by 10 or 100 (val1 and val2 respectively), are too small to be stored in datatype float. For all practical purpose, the value of val1 as well as val 2 is ZERO.

How to avoid overflow?

There can be many arithmetic operations that can lead to overflow. Addition and multiplication are the common operations leading to overflow, however increment, subtraction and division too can cause the results to overflow. How to detect and avoid overflow? The most common technique is to check for overflow using higher datatype. Let us see how to do that with an example:

int intSumOverflow(int a, int b)
{
    long int result;
    result = (long int)a + b;
    if ((result > INT_MAX) || (result < INT_MIN)){
        return -1;
    }
    else{
        return 0;
    }
}

The above code snippet shows one method to detect overflow for datatype int. We upgrade the variable to store the result of arithmetic operation to next larger size (long int in this case), perform the operation and check the results against MAX and MIN values for original datatype (int in this case). In case of overflow, we return -1. [For details about INT_MAX/INT_MIN – refer https://www.thebookofc.com/datatypes/finding-range-of-a-data-type/]

Following are some more points that a programmer can keep in mind to avoid overflow:

  1. It is advisable to choose datatypes based on your program’s requirement. e.g. for a program that deals with students marks or age, short int should suffice. If you are dealing with aeronautical figures, use long double. But matter of fact is that an overflow is not totally avoidable by choosing appropriate datatype.
  2. In case of complex mathematical operations, intermediate results can cause overflow. e.g. 

a = (num1*num2)/num3

If (num1*num2) may cause overflow, whereas the complete mathematical operation may still lead to a value in valid range. We can rewrite it to avoid overflow like this

a = (num1/num3)*num2;

How to avoid underflow?

Most common reason for underflow is with float datatype. Let us try to detect the underflow in the example that we gave earlier on this page

int fltUnderflow (float num1, float num2)
{
    if ((num1/num2)<FLT_EPSILON){
        return -1;
    }
    else{
        return 0;
    }
}

[For details on FLT_EPSILON, check https://www.thebookofc.com/floating-point/comparing-floats-using-epsilon/]

The above program checks if the operation of division causes the result to be lesser than the lowest possible precision that can be stored by float datatype, it detects an underflow and returns -1.

Following are the points that a programmer can keep in mind to avoid underflow:

  1. Use the appropriate datatype according to the precision you want in the program. Whereas you can store the percentage of a student in float, for aeronautical and scientific calculations, it is better to use long double.
  2. As with overflow, you can rewrite the arithmetic operations to avoid underflow in intermediate results.
  3. One common technique used in scientific calculations is to represent the number as ‘n’ times the original number, where value of ‘n’ is say  100. Hence your result will be 100 times the original result of arithmetic operation, but will avoid underflow if it can be too small a number.

NOTE: The above article is an indicator of how overflow and underflow can be handled, however it is not limited to the methods explained in this article. There can be many more ways to avoid overflow and underflow in user specific scenarios.