Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

error

What are overflow and underflow?

Educative Answers Team

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.

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.

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.

RELATED TAGS

error
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring