Introduction To CS and Programming in Python, Lecture 5 - Floats and Approximation Methods - Mit6 - 100l - f22 - Lec05
Introduction To CS and Programming in Python, Lecture 5 - Floats and Approximation Methods - Mit6 - 100l - f22 - Lec05
APPROXIMATION
METHODS
(download slides and .py files to follow along)
6.100L Lecture 5
Ana Bell
1
OUR MOTIVATION FROM LAST
LECTURE
x = 0
for i in range(10):
x += 0.1
print(x == 1)
print(x, '==', 10*0.1)
6.100L Lecture 5
INTEGERS
6.100L Lecture 4
FRACTIONS
6.100L Lecture 5
FRACTIONS
6.100L Lecture 5
WHAT ABOUT FRACTIONS?
6.100L Lecture 5
BUT…
6.100L Lecture 5
TRACE THROUGH THIS ON YOUR OWN
Python Tutor LINK
x =0.625
p = 0
while ((2**p)*x)%1 != 0:
print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
p += 1
num = int(x*(2**p))
result = ''
if num == 0:
result = '0'
while num > 0:
result = str(num%2) + result
num = num//2
print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
8
6.100L Lecture 4
WHY is this a PROBLEM?
6.100L Lecture 5
THE POINT?
10
6.100L Lecture 5
FLOATS
11
6.100L Lecture 5
STORING FLOATING POINT NUMBERS
#.#
Floating point is a pair of integers
Significant digits and base 2 exponent
(1, 1) 1*21 102 2.0
(1, -1) 1*2-1 0.12 0.5
(125, -2) 125*2-2 11111.012 31.25
125 is 1111101 then move the decimal point over 2
12
6.100L Lecture 5
USE A FINITE SET OF BITS TO REPRESENT A
POTENTIALLY INFINITE SET OF BITS
The maximum number of significant digits governs the
precision with which numbers can be represented
Most modern computers use 32 bits to represent significant
digits
If a number is represented with more than 32 bits in binary, the
number will be rounded
Error will be at the 32nd bit
Error will only be on order of 2*10-10
13
6.100L Lecture 5
SURPRISING RESULTS!
x = 0 x = 0
for i in range(10): for i in range(10):
x += 0.125 x += 0.1
print(x == 1.25) print(x == 1)
14
6.100L Lecture 5
MORAL of the STORY
15
6.100L Lecture 5
APPROXIMATION
METHODS
16
6.100L Lecture 5
LAST LECTURE
17
6.100L Lecture 5
BETTER than GUESS-and-CHECK
18
6.100L Lecture 5
EFFECT of APPROXIMATION on
our ALGORITHMS?
19
6.100L Lecture 5
APPROXIMATE sqrt(x)
Good
guess? enough
x
-2 -1 0 1 2 3 4 5 6 7 8
20
6.100L Lecture 5
FINDING ROOTS
21
6.100L Lecture 5
APPROXIMATION
22
6.100L Lecture 5
APPROXIMATION ALGORITHM
-2 -1 0 1 2 3 4 5 6 7 8
23
epsilon epsilon
6.100L Lecture 5
APPROXIMATION ALGORITHM
-2 -1 0 1 2 3 4 5 6 7 8
24 epsilon epsilon
6.100L Lecture 5
BIG IDEA
Approximation is like
guess-and-check
except…
1) We increment by some small amount
2) We stop when close enough (exact is not possible)
25
6.100L Lecture 5
IMPLEMENTATION
x = 36
epsilon = 0.01
num_guesses = 0
guess = 0.0
increment = 0.0001
26
6.100L Lecture 5
OBSERVATIONS with DIFFERENT
VALUES for x
For x = 36
Didn’t find 6
Took about 60,000 guesses
Let’s try:
24
2
12345
54321
27
6.100L Lecture 5
x = 54321
epsilon = 0.01
numGuesses = 0
guess = 0.0
increment = 0.0001
6.100L Lecture 5
WE OVERSHOT the EPSILON!
x = 54321
epsilon epsilon
29
6.100L Lecture 5
SOME OBSERVATIONS
30
6.100L Lecture 5
LET’S FIX IT
x = 54321
epsilon = 0.01
numGuesses = 0
guess = 0.0
increment = 0.0001
while abs(guess**2 - x) >= epsilon and guess**2 <= x:
guess += increment
numGuesses += 1
print('numGuesses =', numGuesses)
if abs(guess**2 - x) >= epsilon:
print('Failed on square root of', x)
else:
print(guess, 'is close to square root of', x)
31
6.100L Lecture 5
BIG IDEA
It’s possible to overshoot
the epsilon, so you need
another end condition
32
6.100L Lecture 5
SOME OBSERVATIONS
33
6.100L Lecture 5
BIG IDEA
Be careful when
comparing floats.
34
6.100L Lecture 5
LESSONS LEARNED in
APPROXIMATION
Can’t use == to check an exit condition
Need to be careful that looping mechanism doesn’t jump over
exit test and loop forever
Tradeoff exists between efficiency of algorithm and accuracy of
result
Need to think about how close an answer we want when
setting parameters of algorithm
To get a good answer, this method can be painfully slow.
Is there a faster way that still gets good answers?
YES! We will see it next lecture….
35
6.100L Lecture 5
SUMMARY
36
6.100L Lecture 5
MITOpenCourseWare
https://ocw.mit.edu
37