1. Python numeric type
Python has two numeric types:INTEGER (int)AndFloat ).
For Python, integer types can be infinitely large.
Python integer can take any precision
For example, you can enter 2 ** to the power of 1000, and the correct result will still be returned.
The floating point type of Python follows the IEE754 standard. For 64-bit computers, it is represented as follows:
1 bit: Symbol bit
11: Exponential
52 bits: The ending bits
In this way, a total of 17-bit long precision can be expressed. If it exceeds 17-bit precision, Python cannot handle it, for example, 0.1 ** to the power of 1000.
In the tutorial video, if x = 0.1 is input, the final value is 0.10000000000000001. The reason is very simple, because the computer processes numbers in binary mode, while the binary 0.1 is 0. 0001100110011 ..., in this way, an error occurs during the conversion process (the new version of python may process the result and display it again, so it is still 0.1 ). For example, the following program accumulates s, and each time accumulates 0.1
s = 0.1for i in range(100): s += 0.1 #=>10.09999999999998
Due to the accumulation of errors, the last digit becomes 8.
The existence of Errors often leads to unexpected results when the float type is = judged:
import matha = math.sqrt(2)a*a == 2 #=>False
Therefore, for the floating point type, we do not need to judge whether the results are equal, but whether the results are smaller than a certain allowable error value:
Abs (a * a-2.0) <a very small positive number
2. Calculate the square root using the bipartite Method
I have learned how to calculate the square root of X before. However, X is assumed to be an integer after square open, so we can start enumeration from 0. Now, in a more general form, X is not necessarily a definite number after the start, because one-to-one enumeration cannot be performed within the range of real numbers, other methods should be used to solve the problem.
The basic solution is the method of successive approximation, that is, each time the result approaches the correct answer, that is, takingConjecture-check-improvementProcess:
Guess = initial guess
Iterations 100:
If f (guess) is close to the correct answer: Return guess
Otherwise: Make guess = better guess
If no return value is returned, an error is returned.
To determine whether f (guess) is close to the correct answer, it is easy to verify: abs (guess ** 2-x) <a very small positive number
# Square root of the binary method # epsilon: A very small positive number ctr: number of iterations def squareRootBi1 (x, epsilon): assert epsilon> 0, 'epsilon must be postive, not '+ str (epsilon) low = 0 high = x guess = (low + high)/2.0 ctr = 1 while abs (guess ** 2-x)> epsilon and ctr <= 100: if guess ** 2 <x: low = guess else: high = guess = (low + high) /2.0 ctr + = 1 assert ctr <= 100, 'iteration count exceeded' print ('bi method. num. iterations: ', ctr, 'estimate:', guess) return guess
The assert statement is equivalent to inserting a debug breakpoint into a program. For example, if the input epsilon value is-1, the following error is displayed:
File "/Users/ihuangmx/Program/Python/Untitled. py", line 2, in squarerootraumatic
Assert epsilon> 0, 'epsilon must be postive, not '+ str (epsilon)
AssertionError: epsilon must be postive, not-1
The above program also has bugs, which will be improved in the next lecture.