Chapter 3 Chapter 3
13.51 Limitations of Computer Arithmetic
3.5. Limitations of Computer Arithmetic
Given our input value
n,we have a couple of different
rangecommands that produce an appropriate list of factors for computing the factorial of
n.To generate them from smallest to largest (a Ia our second loop), we could use
range (2 ,n+1).
Notice how I used
n+1as the second parameter, since the range will go up to but not include this value. We need the +1 to make sure that
nitself is included as the last factor.
Another possibility is to generate the factors in the other direction (a Ia our first loop) using the three-parameter version of range and a negative step to cause the counting to go backwards:
range (n, 1, -1).This one produces a list starting with nand counting down (step -1) to, but not including
1.Here then is one possible version of the factorial program:
# factorial. py
# Program to compute the factorial of a number
# Illustrates for loop with an accumulator def main () :
n = int (input ("Please enter a whole number: ") ) fact = 1
for factor in range (n,1,-1) : fact = fact * factor
print ("The factorial of", n, "is", fact) main ()
Of course, there are numerous other ways this program could have been written.
I have already mentioned changing the order of factors. Another possibility is to
initialize
factto
nand then use factors starting at
n-1 (as long as
n> 0). You
might try out some of these variations and see which one you like best.
That's a pretty big number!
Although recent versions of Python have no difficulty with this calculation, older versions of Python (and modern versions of other languages such as C++
and Java) would not fare as well. For example, here's what happens in several runs of a similar program written using Java:
#run 1
Please enter a whole number: 6 The factorial is: 720
#run 2
Please enter a whole number: 12 The factorial is: 479001600
#run 3
Please enter a whole number: 13 The factorial is: 1932053504
This looks pretty good; we know that 6!
=720. A quick check also confirms that 12!
=479001600. Unfortunately, it turns out that 13!
=6227020800. It appears that the Java program has given us an incorrect answer!
What is going on here? So far, I have talked about numeric data types as rep
resentations of familiar numbers such as integers and decimals (fractions). It is important to keep in mind, however, that computer representations of numbers (the actual data types) do not always behave exactly like the numbers that they stand for.
Remember back in Chapter 1 you learned that the computer's CPU can per
form very basic operations such as adding or multiplying two numbers? It would be more precise to say that the CPU can perform basic operations on the com
puter's internal representation of numbers. The problem in this Java program is that it is representing whole numbers using the computer's underlying int data type and relying on the computer's multiplication operation for ints. Unfortu
nately, these machine ints are not exactly like mathematical integers. There are infinitely many integers, but only a finite range of ints. Inside the computer, ints are stored in a fixed-sized binary representation. To make sense of all this, we need to look at what's going on at the hardware level.
Computer memory is composed of electrical "switches," each of which can
be in one of two possible states, basically on or off. Each switch represents a
binary digit or bit of information. One bit can encode two possibilities, usually
3.5. Limitations of Computer Arithmetic
represented with the numerals 0 (for off) and 1 (for on). A sequence of bits can be used to represent more possibilities. With two bits, we can represent four things:
1 bit 2 1 bit 1 1
0 0
0 1
1 0
1 1
Three bits allow us to represent eight different values by adding a 0 or 1 to each of the four two-bit patterns:
1 bit 3 1 bit 2 1 bit 1 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
You can see the pattern here. Each extra bit doubles the number of distinct patterns. In general,
nbits can represent 2n different values.
The number of bits that a particular computer uses to represent an int de
pends on the design of the CPU. Typical PCs today use 32 or 64 bits. For a 32-bit CPU, that means there are 232 possible values. These values are centered at 0 to represent a range of positive and negative integers. Now 2 � 2
=231• So the range of integers that can be represented in a 32-bit int value is -231 to 231- 1.
The reason for the -1 on the high end is to account for the representation of 0 in the top half of the range.
Given this knowledge, let's try to make sense of what's happening in the Java factorial example. If the Java program is relying on a 32-bit int representation, what's the largest number it can store? Python can give us a quick answer:
>>> 2**31-1 2147483647
Notice that this value (about 2.1 billion) lies between 12! (about 480 million)
73
and 13! (about 6.2 billion). That means the Java program is fine for calculating factorials up to 12, but after that the representation "overflows" and the results are garbage. Now you know exactly why the simple Java program can't compute 13! Of course, that leaves us with another puzzle. Why does the modern Python program seem to work quite well computing with large integers?
At first, you might think that Python uses the float data type to get us around the size limitation of the ints. However, it turns out that floats do not really solve this problem. Here is an example run of a modified factorial program that uses floating-point numbers:
Please enter a whole number: 30
The factorial of 30 is 2.6525285981219103e+32
Although this program runs just fine, after switching to float, we no longer get an exact answer.
A very large (or very small) floating-point value is printed out using expo
nential, or scientific, notation. The
e+32at the end means that the result is equal to 2.6525285981219103
x1032• You can think of the
+32at the end as a marker that shows where the decimal point should be placed. In this case, it must move 32 places to the right to get the actual value. However, there are only 16 digits to the right of the decimal, so we have "lost" the last 16 digits.
Using a float allows us to represent a much larger range of values than a 32-bit int, but the amount of precision is still fixed. In fact, a computer stores floating-point numbers as a pair of fixed-length (binary) integers. One integer, called the mantissa, represents the string of digits in the value and the second, the exponent, keeps track of where the whole part ends and the fractional part begins (where the "binary point" goes). Remember I told you that floats are ap
proximations. Now you can see why. Since the underlying numbers are binary, only fractions that involve powers of 2 can be represented exactly; any other fraction produces an infinitely repeating mantissa. (Just like 1/3 produces an infinitely repeating decimal because 3 is not a power of 10.) When an infinitely long mantissa is truncated to a fixed length for storage, the result is a close ap
proximation. The number of bits used for the mantissa determines how precise the appoximations will be, but there is no getting around the fact that they will be approximations.
Fortunately, Python has a better solution for large, exact values. A Python
int is not a fixed size, but expands to accommodate whatever value it holds. The
only limit is the amount of memory the computer has available to it. When the
value is small, Python can just use the computer's underlying int representation
3.6. Chapter Summary