MIT public class: Introduction to Computer science and programming Python Note 5 floating-point numbers, successive approximation and dichotomy

Source: Internet
Author: User
Tags diff float number square root

Lecture5:floating point number, successive refinement, finding roots float and dichotomy

3wschool Digital

>>>A =2** +>>>A107150860718626732094842504906000181056140481170553360744375038837035105112493612249319837881569585812759467291755314 6825187145285692314043598457757469857480393456777482423098542107460506237114187795418215304647498358194126739876755916554 3946077062914571196477686542167660429831652624386837205668069376L>>>b =2**999>>>B535754303593133660474212524530000905280702405852766803721875194185175525562468061246599189407847929063797336458776573 4125935726428461570217992288787349287401967283887412115492710537302531185570938977091076523237491790970633699383779582771 973038531457285598238843271083830214915826312193418602834034688L>>>A/b2L

IEEE 754 Floating Point
Wiki IEEE floating point
Wiki IEEE 754 Chinese

    • Scientific notation
    • A mantissa mantissa (0=< mantissa < 2) and an exponent index

      worry about = = on float number!

>>> import math>>> a = math.sqrt(2)>>> a1.4142135623730951>>> 2False
# 大约相等2.0) < epsilon
>>> 0.0>>> forin range(100.1... >>> s0.9999999999999999

Why might this is a challenge? What is some of the issues?

    • Might not being an exact answer has no exact value for example: sqrt (2)
    • Can ' t enumerate all guesses cannot enumerate all possible values, because real numbers are not counted
    • Guess, check, and improve
    • Successive approximation method of successive approximation
# 逐次逼近法(伪代码):guess = inital guessforin range(100):    ifreturn guess    else: guess = better guesserror

Finding square root by successive approximation method
-Two method, the square root of X, epsilon (ε) is a small enough number, Ctr iteration number

 def squarerootbi(x, epsilon):    "" " assume x >= 0 and epsilon > 0 Return y s.t. Y*y is within epsilon (ε) of X" ""    assertEpsilon >0,' Epsilon must is postive, not '+ STR (epsilon) low =0High = max (x,1) guess = (low + high)/2.0CTR =1     whileABS (GUESS * *2-X) > Epsilon andCtr <= -:# print ' Low: ', Low, ' High: ', high, ' guess: ', guess if guess**2 < x:        ifguess**2< X:low = GuessElse: High = guess guess = (low + high)/2.0CTR + =1    assertCtr <= -,' iteration count exceeded '    Print ' Bi method. Num. Iterations: ', CTR,' Estimate: ', GuessreturnGuess

 def squarerootnr(x, epsilon):    "" " Return y s.t. Y*y is within epsilon of X" "    assertEpsilon >0,' Epsilon must is postive, not '+ str (epsilon) x = float (x) guess = x/2.0Guess =0.001diff = Guess * *2-X ctr =1     whileABS (diff) > Epsilon andCtr <= -:# print ' Error: ', diff, ' Guess: ', guess guess = guess-diff/(2.0*guess)diff = Guess * *2-X ctr + =1    assertCtr <= -,' iteration count exceeded '    Print ' NR method. Num. Iterations: ', CTR,' Estimate: ', GuessreturnGuess

MIT public class: Introduction to Computer science and programming Python Note 5 floating-point numbers, successive approximation and dichotomy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.