{"id":976,"date":"2010-11-09T18:00:40","date_gmt":"2010-11-09T18:00:40","guid":{"rendered":"http:\/\/pchero21.com\/?p=976"},"modified":"2010-11-09T18:00:40","modified_gmt":"2010-11-09T18:00:40","slug":"lec-5-mit-6-00-introduction-to-computer-science-and-programming-fall-2008","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=976","title":{"rendered":"Lec 5 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008"},"content":{"rendered":"<p><span style=\"font-weight: bold;\">5: Floating Point Numbers, Successive Refinement, Finding Roots<\/span><\/p>\n<p><object width=\"480\" height=\"385\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/Pfo7r6bjSqI?fs=1&amp;hl=ko_KR\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/Pfo7r6bjSqI?fs=1&amp;hl=ko_KR\" type=\"application\/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"480\" height=\"385\"><\/embed><\/object><\/p>\n<blockquote><p>def squareRootBi(x, epsilon):<br \/>&nbsp; &nbsp; &#8220;&#8221;&#8221;Return y s.t. y*y is within epsilon of x&#8221;&#8221;&#8221;<br \/>&nbsp; &nbsp; assert epsilon &gt; 0, &#8216;epsilon must be positive, no &#8216; + str(epsilon)<br \/>&nbsp; &nbsp; low = 0<br \/>&nbsp; &nbsp; high = max(x, 1)<br \/>&nbsp; &nbsp; guess = (low + high) \/ 2.0<br \/>&nbsp; &nbsp; ctr = 1<br \/>&nbsp; &nbsp; while abs(guess ** 2 &#8211; x) &gt; epsilon and ctr &lt;= 100:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; #print &#8216;low:&#8217;, low, &#8216;high:&#8217;, high, &#8216;guess:&#8217;, guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; if guess ** 2 &lt; x:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; low = guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; high = guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; guess = (low + high) \/ 2.0<br \/>&nbsp; &nbsp; &nbsp; &nbsp; ctr += 1<br \/>&nbsp; &nbsp; assert ctr &lt;= 100, &#8216;Iteration count exceeded&#8217;<br \/>&nbsp; &nbsp; print &#8216;Bi method. Num. iterations:&#8217;, ctr, &#8216;Estimate:&#8217;, guess<br \/>&nbsp; &nbsp; return guess<br \/>&nbsp; &nbsp; <br \/>&nbsp; &nbsp; <br \/>def squareRootNR(x, epsilon):<br \/>&nbsp; &nbsp; &#8220;&#8221;&#8221;Return y s.t. y*y is within epsilon of x&#8221;&#8221;&#8221;<br \/>&nbsp; &nbsp; assert epsilon &gt; 0, &#8216;epsilon must be positive, not&#8217; + str(epsilon)<br \/>&nbsp; &nbsp; x = float(x)<br \/>&nbsp; &nbsp; guess = x \/ 2.0<br \/>&nbsp; &nbsp; guess = 0.001<br \/>&nbsp; &nbsp; diff = guess ** 2 &#8211; x<br \/>&nbsp; &nbsp; ctr = 1<br \/>&nbsp; &nbsp; while ab(diff) &lt; epsilon and ctr &lt;= 100:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; #print &#8216;Error:&#8217;, diff, &#8216;guess:&#8217;, guess<br \/>&nbsp; &nbsp; &nbsp; &nbsp; guess = guess &#8211; diff \/ (2.0 * guess)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; diff = guess ** 2 &#8211; x<br \/>&nbsp; &nbsp; &nbsp; &nbsp; ctr += 1<br \/>&nbsp; &nbsp; assert ctr &lt;= 100, &#8216;Iteration count exceeded&#8217;<br \/>&nbsp; &nbsp; print &#8216;NR method. Num. iterations:&#8217;, ctr, &#8216;Estimate:&#8217;, guess<br \/>&nbsp; &nbsp; return guess<br \/>&nbsp; &nbsp; <br \/>&nbsp; &nbsp; <br \/>a = squareRootBi(2, 0.01)<br \/>print a<br \/>a = squareRootNR(2, 0.01)<br \/>print a<\/p><\/blockquote>\n<p><img loading=\"lazy\" src=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XIRcMJzAzb.png\" class=\"aligncenter\" width=\"484\" height=\"316\" alt=\"\" \/> \uc65c \uc5d0\ub7ec\uac00 \ub730\uae4c&#8230;. \uc774\ud574\uac00 \ud544\uc694\ud558\ub2e4..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5: Floating Point Numbers, Successive Refinement, Finding Roots def squareRootBi(x, epsilon):&nbsp; &nbsp; &#8220;&#8221;&#8221;Return y s.t. y*y is within epsilon of x&#8221;&#8221;&#8221;&nbsp; &nbsp; assert epsilon &gt; 0, &#8216;epsilon must be positive, no &#8216; + str(epsilon)&nbsp; &nbsp; low = 0&nbsp; &nbsp; high &hellip; <a href=\"http:\/\/pchero21.com\/?p=976\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[259,293],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/976"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=976"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/976\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}