What are overflow and underflow?

Overflow and underflow are both errors resulting from a shortage of space. On the most basic level, they manifest in data types like integers and floating points.

svg viewer

Unlike the physical world, the number stored in a computer exists in a discrete number of digits. When we make a calculation that results in an extra digit, we cannot simply append that to our result, so we get an overflow or underflow error.

Overflow errors come up when working with integers and floating points, and underflow errors are generally just associated with floating points.

Overflow

Overflow indicates that we have done a calculation that resulted in a number larger than the largest number we can represent. Let’s look at an example involving unsigned integers.

Lets assume we have an integer stored in 11 byte. The greatest number we can store in one byte is 255255, so let’s take that. This is 1111111111111111. Now, suppose we add 22 to it to get 0000001000000010. The result is 257257, which is 100000001100000001. The result has 99 bits, whereas the integers we are working with consist of only 88.

What does a computer then do in this scenario? A computer will discard the most-significant bit (MSB) and keep the rest.

This is essentially equal to rr %\% 2n2^n.

Here, rr is the result, nn is the number of bits available, and %\% is the modulo operator.

Code example

In the following C++ code, we demonstrate an overflow error by adding 1 in the maximum possible value for a one-byte unsigned char (255). Since the result 256 is larger than the maximum representable value for an unsigned char, it causes an overflow error.

#include <iostream>
using namespace std;
int main() {
// Define a variable to store the value (1 byte)
unsigned char value = 255; // Maximum value for 1 byte
// Add 2 to the value
value = value + 1;
// Print the result
cout << "The result after adding 2: " << static_cast<int>(value) << endl;
return 0;
}

In the output, instead of resulting in the expected value (256), overflow causes the value to wrap around to the lowest value, which is 0, when adding 1.

Underflow

Underflow is a bit trickier to understand because it has to do with precision in floating points. Again, due to the discrete nature of storage in computers, we cannot store an arbitrarily small number either. The floating-point convention comes up with techniques to represent fractional numbers. When we use these in calculations that result in a smaller number than our least value, we again exceed our designated space. Without going into details of floating-point representation, we can see how this problem would manifest by considering a decimal example.

Example: Suppose we are given designated boxes to write decimal numbers in. We have one box on the left of the decimal point and three boxes on the right. So, we can easily represent 0.0040.004. Now, we want to perform a calculation, 0.004×0.0040.004 \times 0.004. The answer to this is 0.0000160.000016, but we simply do not have this many places available to us. So we discard the least-significant bits and store 0.0000.000, which is quite obviously an erroneous answer.

Code example

In the following C++ code, we demonstrate an underflow error by subtracting 1 from the minimum possible value for a one-byte unsigned char (0). Since the result -1 is smaller than the minimum representable value for an unsigned char, it causes an underflow error.

#include <iostream>
using namespace std;
int main() {
// Define a variable to store the value (1 byte)
unsigned char value = 0; // Minimum value for 1 byte
// Subtract 2 from the value (underflow)
value = value - 1;
// Print the result
cout << "The result after subtracting 2: " << static_cast<int>(value) << endl;
return 0;
}

In the output, underflow results in the value wrapping around to the maximum value representable by an unsigned char (255) when subtracting 1 rather than producing a negative number.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved