Consequences of using bits: overflow and rounding errors (College Board AP® Computer Science Principles): Study Guide
Integer representation limits
Why are integers limited in computing?
In many programming languages, integers are represented by a fixed number of bits, which limits the range of integer values and the mathematical operations that can be performed on them
This limitation can cause an overflow error (when a calculation produces a value outside the representable range)
Not all languages work this way
Some provide an abstraction where the size of representable integers is limited only by the computer's memory, this is the case for the language defined in the AP exam reference sheet
For AP pseudocode questions, you should not assume integers will overflow at a fixed bit limit
In programming languages that use fixed-bit integers, the number of bits allocated determines the range of representable values:
Number of bits | Number of values (2^n) | Range |
|---|---|---|
4 bits | 16 | 0 to 15 |
8 bits | 256 | 0 to 255 |
16 bits | 65,536 | 0 to 65,535 |
32 bits | 4,294,967,296 | 0 to 4,294,967,295 |
Implications of integer limits
Programs that perform calculations with large numbers must account for the possibility of overflow
Choosing the wrong number of bits for a variable can lead to incorrect results without any visible error message (for example, a counter that resets to 0 after reaching its maximum value)
Understanding integer limitations is essential for writing programs that behave correctly across all expected inputs
Real number representation limits
Why are real numbers difficult to represent precisely?
Computers also use a fixed number of bits to store real numbers (numbers with decimal points)
Unlike integers, real numbers can have infinite decimal places, but a fixed number of bits can only store a finite level of precision
This means most real numbers are stored as approximations, and small rounding errors can accumulate over many calculations
Implications of real number limits
Programs that perform calculations with decimal values may produce results that are slightly off due to accumulated rounding errors
Financial and scientific calculations are particularly vulnerable; small rounding errors repeated across many operations can lead to significantly incorrect results
Developers should be aware that testing a real number for exact equality (for example, checking if a result is exactly 0.1) may fail even when the calculation appears correct
Limitation | Integers | Real numbers |
|---|---|---|
Precision | Exact within the representable range | Approximate due to finite decimal precision |
Common error | Overflow (value exceeds maximum) | Rounding errors that accumulate over calculations |
Examiner Tips and Tricks
The AP exam frequently tests whether you understand that using a fixed number of bits creates limitations for both integers and real numbers. For integers, the key issue is range (overflow). For real numbers, the key issue is precision (rounding). If a question describes a calculation that produces a slightly wrong decimal result, the answer is almost always related to real number precision limits.
For the AP Create Performance Task, if your program performs calculations with large numbers or decimal values, be aware that overflow and rounding errors can cause unexpected behavior — on exam day you may be asked to identify or explain errors in a program, and number representation limits are a common source
Worked Example
A program uses 8 bits to store non-negative integers. A student writes a line of code that attempts to store the value 300.
Which of the following best describes what happens?
(A) The value 300 is stored correctly
(B) The program rounds 300 to the nearest representable value
(C) An overflow error occurs because 300 is outside the range of values that 8 bits can represent
(D) The program automatically allocates more bits to store the value
[1]
Answer:
(C) An overflow error occurs because 300 is outside the range of values that 8 bits can represent [1 mark]
With 8 bits, the representable range is 0 to 255 (2^8 values). The value 300 is outside this range, so the computer cannot represent it correctly — this is the definition of an overflow error
Unlock more, it's free!
Was this revision note helpful?