< previous page page_532 next page >

Page 532
Thus, we could use the Factorial function we wrote in Chapter 8 and write this formula in an assignment statement:
hands = Factorial(52) / (Factorial(5) * Factorial(47));
The only problem is that 52! is a very large number (approximately 8.0658 * 1067). And 47! is also rather big (approximately 2.5862 * 1059). Both of these numbers are well beyond the capacity of most systems to represent exactly as integers (52! requires 68 digits of precision). Even though they can be represented on many machines as floating point numbers, some of the precision is still lost. By rearranging the calculations, however, we can achieve an exact result on any system with 9 or more digits of precision. How? Consider that most of the multiplications in computing 52! are cancelled when it is divided by 47!
0532-01.gif
So, we really only have to compute
hands = 52 * 51 * 50 * 49 * 48 / Factorial(5);
which means the numerator is 311,875,200 and the denominator is 120. On a system with 9 digits of precision, we thus have an exact answer of 2,598,960 poker hands.
Cancellation Error
Another type of error that can happen with floating point numbers is called cancellation error, a form of representational error that occurs when numbers of widely differing magnitudes are added or subtracted. Let's look at an example:
(1 +0.00001234-1)=0.00001234
The laws of arithmetic say this equation should be true. But is it true if the computer does the arithmetic?
0532-02.gif
To four digits, the sum is 1000 * 10-3. Now the computer subtracts 1:
0532-03.gif
The result is 0, not .00001234.

 
< previous page page_532 next page >