Web Developer

MIT Python Programming #5

Lesson 5: Floating point numbers, successive refinement, finding roots

Massachusetts Institute of Technology (OpenCoarseWare): Introduction to Computer Science and Programming

Lecture 5: Floating point numbers, successive refinement, finding roots

Instructors: Prof. Eric Grimson, Prof. John Guttag

View the complete course at: http://ocw.mit.edu/6-00F08

Introduction to Computer Science & Programming Class Notes

Floating point numbers, successive refinement, finding roots

Python has 2 types of numbers:
int (integers – whole numbers)
Arbitrary precision (Long Integer)

The following code displays arbitrary precision:
a = 2**1000
Type a into python and python returs the full answer followed by the
letter L. L stands for "long integer". There is a better way for results.
b = 2**999
Type b and once again Python returns the "long integer" results.
Type a/b and python displays 2L.
x = 0.1
Type x and Python returns: 0.1000000000000001
Python and almost every other modern program language represent numbers using

IEEE Floating Standard.

IEEE 754 Floating Point

(A Variant of Scientific Notation – How to Handle Large Numbers)

Mantissa – The decimal point of a logarithm.
In the logarithm 1.587264 the Mantissa is 0.587264.

Mantissa is also known as a Significand.

Computers run off of 64 bits.
1 bit sign (Value or No Value 0 1) (Binary)
Base 10: 125 x10(-1)
Base 2:   1.0 x 2-(3)    (Binary 0.001

1/10 = 0.1
Base 10: 1 x 10(-1)
Base 2: ???? Their is no binary number that represents 1/10. An infinite
sequence is produced by this equation .00001100110011…….

Base 2 is binary. Numbers are represented as values or no values, or
(on/off). 0/1
The decimal number 26 would be represented as 00001110 in binary format.

Worry about == on floats.

a = math.sqrt(2)     (A equals the square
root of 2).
Type a and python returns 1.41421356 (Approximate value).
Enter this equation into python:
a * a == 2      (Mathematically this
makes sense)
Python returns 'False"
The square root of 2 (defined previously) is an approximation to the square
a * a   Python returns 2.0000004

Never use == to compare floating points.
Never test for equality, rather test for "close enough".

Ask the following question:
Is the absolute value of a * a – <e epsilon   (Are these two values
within an epsilon of each other? If true then Im going to treat them as equal.
abs(a*a – 2.0) < epsilon

Professor stated that when he writes a program dealing with floating point
numbers he introduces a function called "almost equal(or epsilon)" which better
defines the correct values of floating points.

Rather then writing
x == y     (Two Floating Points)
He writes
Near x,y    (Computes it for him)

Solving Problems using computers
Finding the square root of a real number.

Lets pretend our job is to implement a math problem to solve the square root
of a real number.

What are the issues?
– Might not be an exact answer
– Previously we used exhaustive enumeration to enumerate all possibilities and
then returning the results. In this problem you cannot enumerate all guesses.
(real guesses are uncountable).

Guess, check (Previous Farmyard problem guessed possibilities but did not
know if the previous answer was better then the next.)

For the square root of a real number we will guess, check,
and improve.

Guess, Check, Improve

Successive Approximation
guess = initial guess
for int in range (100):
if f(guess) close enough: return guess
else: guess = better guess

Bisection method: Linear arranged space of possible answers. If possible
answer is between 1-100 first guess will be in middle. Guess will be less then
or more then the correct answer. If the correct answer is less then, then all
numbers greater then the guess are counted as incorrect. The next guess is a
guess in the middle of all possible answers.

Another method is now used, called the Newton Ralphson Method.

Newton Ralphson Method better defines numbers with an x & y axis with an
angled curve displaying the spectrum of guesses. First guessing point is marked
and the angle of the curve is used to make contact with the x axis. The point in
the spectrum of guesses is seen to be much closer then the first guess. The only
time that this method DOES NOT work is in instances where the guess is directly
in the middle of the spectrum.

Newton Ralphson Method
Newton Ralphson Method

The above is my personal notes in regards to this class to help me in the
learning process.

  Related Posts