AQA 7517, section 4.5.4.9
A computer stores numbers in a fixed number of bits, so there is a limit to the largest and smallest values it can hold. When a calculation goes outside these limits, an overflow or underflow occurs.
Overflow
Overflow happens when the result of a calculation is too large to be stored in the number of bits available.
Integer overflow
In unsigned 8-bit binary the largest value is 255. Adding 1 to it:
The ninth bit is lost, and the stored result is . The answer is wrong.
In two's complement, adding two large positive numbers can give a result that wraps round to a negative number. In 8 bits, , which is .
# Python integers don't overflow, but this shows the idea for 8 bits
value = (127 + 1) & 0xFF # keep only 8 bits
print(value) # 128, which is 10000000
signed = value - 256 if value > 127 else value
print(signed) # -128 in two's complement
Floating point overflow
In floating point, overflow happens when the result needs an exponent larger than the biggest one available. With a 4-bit two's complement exponent (maximum 7), a value that needs an exponent of 8 can't be stored. The program may give an error, or return a special "infinity" value.
Underflow
Underflow happens when a result is too small (too close to zero) to be stored, because it is smaller than the smallest non-zero value the format can represent.
Floating point underflow
When the exponent would need to be more negative than the smallest possible one (for a 4-bit exponent, below ), the number can't be represented. The result is usually stored as zero, so precision is lost.
Example: with a 4-bit exponent, the smallest normalised positive number is mantissa (0.5) with exponent (). That value is . Dividing it by 4 would give about , which is smaller than the smallest normalised number. If results must be normalised, underflow occurs and the result becomes 0.
Fixed point underflow
In fixed point, a value smaller than the smallest step (for example with 4 fractional bits) can't be stored, and it is rounded to zero.
Comparing them
| Overflow | Underflow | |
|---|---|---|
| The result is | Too large | Too small (too close to zero) |
| Integers | The result needs more bits than are available | Doesn't occur with integers |
| Floating point | The exponent is too large and positive | The exponent is too large and negative |
| Typical result | Wrong value, wrapped number, error or infinity | Value becomes 0 |
| Effect | The answer is badly wrong | Precision is lost; may cause errors later, such as dividing by zero |
Why does it matter?
An unnoticed overflow or underflow can give a completely wrong answer without any warning. A famous example is the 1996 Ariane 5 rocket failure, caused by a number too large for its storage. Programs should check for these cases or use data types that are big enough.
Worked example
Task: a program uses 4-bit unsigned integers. What happens in ?
and .
The true answer is 19, which needs 5 bits. The maximum for 4 bits is 15, so only the lowest 4 bits are kept: . Overflow has occurred, and the stored result is 3 instead of 19.
Exam tip: describe the circumstances. Overflow is when the result is too large for the available bits (or exponent), and underflow is when the result is too close to zero to be represented, so it is stored as zero. Say what the effect is.
Check yourself
What is overflow?
When the result of a calculation is too large to be stored in the number of bits available.
What is underflow?
When the result of a calculation is too close to zero to be represented, so it is smaller than the smallest value that can be stored, and it is usually stored as zero.
What happens in 8-bit unsigned binary when you add 1 to 255?
The result needs 9 bits, so the extra bit is lost and the stored value is 0 (overflow).