6.6 Testing Code
153
These are two equations for two unknowns C and r. We can easily eliminate C by
dividing, e.g., (6.26) by (6.27). Doing so, and proceeding to solve for r, followed by
also introducing a subscript i − 1 for r, gives
r i−1 = −
ln(E i /E i−1 )
ln(n i /n i−1 )
.
(6.28)
The subscript is introduced, since the estimated value for r will vary with i.
Hopefully, r i−1 approaches the correct convergence rate as the number of intervals
increases and i → q.
6.6.3 Finite Precision of Floating-Point Numbers
The test procedures above lead to comparison of numbers for checking that
calculations were correct. Such comparison is more complicated than what a
newcomer might think. Suppose we have a calculation a + b and want to check
that the result is what we expect.
Adding Integers We start with 1 + 2:
In [1]: a = 1; b = 2; expected = 3
In [2]: a + b == expected
Out[2]: True
Adding Real Numbers Then we proceed with 0.1 + 0.2:
In [3]: a = 0.1; b = 0.2; expected = 0.3
In [4]: a + b == expected
Out[4]: False
Approximate Representation of Real Numbers on a Computer So why is 0.1 +
0.2 = 0.3? The reason is that, generally, real numbers cannot be represented exactly
on a computer. They must instead be approximated by a floating-point number 5
that can only store a finite amount of information, usually about 17 digits of a real
number. Let us print 0.1, 0.2, 0.1 + 0.2, and 0.3 with 17 decimals:
In [5]: print(’{:.17f}\n{:.17f}\n{:.17f}\n{:.17f}’\
.format(0.1, 0.2, 0.1 + 0.2, 0.3))
0.10000000000000001
0.20000000000000001
0.30000000000000004
0.29999999999999999
We see that all of the numbers have an inaccurate digit in the 17th decimal place.
Because 0.1 + 0.2 evaluates to 0.30000000000000004 and 0.3 is represented as
0.29999999999999999, these two numbers are not equal. In general, real numbers
in Python have (at most) 16 correct decimals.
5 https://en.wikipedia.org/wiki/Floating_point.
153
These are two equations for two unknowns C and r. We can easily eliminate C by
dividing, e.g., (6.26) by (6.27). Doing so, and proceeding to solve for r, followed by
also introducing a subscript i − 1 for r, gives
r i−1 = −
ln(E i /E i−1 )
ln(n i /n i−1 )
.
(6.28)
The subscript is introduced, since the estimated value for r will vary with i.
Hopefully, r i−1 approaches the correct convergence rate as the number of intervals
increases and i → q.
6.6.3 Finite Precision of Floating-Point Numbers
The test procedures above lead to comparison of numbers for checking that
calculations were correct. Such comparison is more complicated than what a
newcomer might think. Suppose we have a calculation a + b and want to check
that the result is what we expect.
Adding Integers We start with 1 + 2:
In [1]: a = 1; b = 2; expected = 3
In [2]: a + b == expected
Out[2]: True
Adding Real Numbers Then we proceed with 0.1 + 0.2:
In [3]: a = 0.1; b = 0.2; expected = 0.3
In [4]: a + b == expected
Out[4]: False
Approximate Representation of Real Numbers on a Computer So why is 0.1 +
0.2 = 0.3? The reason is that, generally, real numbers cannot be represented exactly
on a computer. They must instead be approximated by a floating-point number 5
that can only store a finite amount of information, usually about 17 digits of a real
number. Let us print 0.1, 0.2, 0.1 + 0.2, and 0.3 with 17 decimals:
In [5]: print(’{:.17f}\n{:.17f}\n{:.17f}\n{:.17f}’\
.format(0.1, 0.2, 0.1 + 0.2, 0.3))
0.10000000000000001
0.20000000000000001
0.30000000000000004
0.29999999999999999
We see that all of the numbers have an inaccurate digit in the 17th decimal place.
Because 0.1 + 0.2 evaluates to 0.30000000000000004 and 0.3 is represented as
0.29999999999999999, these two numbers are not equal. In general, real numbers
in Python have (at most) 16 correct decimals.
5 https://en.wikipedia.org/wiki/Floating_point.
