{"id":202,"date":"2010-03-20T15:15:41","date_gmt":"2010-03-20T06:15:41","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2010\/03\/20\/_6\/"},"modified":"2010-03-20T15:15:41","modified_gmt":"2010-03-20T06:15:41","slug":"_6","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2010\/03\/20\/_6\/","title":{"rendered":"\u4e00\u4eba\u8aad\u66f8\u4f1a \u300c\u8a08\u7b97\u6a5f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u69cb\u9020\u3068\u89e3\u91c8\u300d (6)"},"content":{"rendered":"<p><a href=\"http:\/\/www.amazon.co.jp\/dp\/489471163X?tag=typea09-22&amp;link_code=as3&amp;creativeASIN=489471163X&amp;creative=3999&amp;camp=767\"><img decoding=\"async\" src=\"http:\/\/images-jp.amazon.com\/images\/P\/489471163X.09.MZZZZZZZ.jpg\" complete=\"complete\"><\/a> <\/p>\n<p>&nbsp;<\/p>\n<h2>1.1.7 Newton \u6cd5\u306b\u3088\u308b\u5e73\u65b9\u6839<\/h2>\n<p>&nbsp;<\/p>\n<h3>\u554f\u984c\u30001.7<\/h3>\n<p>\u3055\u3066\u3001\u3088\u3046\u3084\u304f\u554f\u984c\u306e\u5185\u5bb9\u81ea\u4f53\u3092\u7406\u89e3\u3067\u304d\u308b\u6e96\u5099\u304c\u6574\u3063\u305f\u306e\u3067\u3001\u8003\u3048\u3066\u307f\u308b\u3002\u554f\u984c\u306e\u6982\u8981\u3002<\/p>\n<pre class=\"prettyprint linenums\">(define (square x) (* x x))\r\n(define (good-enough? guess x)\r\n  (&lt; (abs (- (square guess) x)) 0.001))\r\n(define (average x y) (\/ (+ x y) 2))\r\n(define (improve guess x)\r\n  (average guess (\/ x guess)))\r\n(define (sqrt-itr guess x) \r\n  (if (good-enough? guess x)\r\n      guess\r\n      (sqrt-itr (improve guess x)\r\n                x)))\r\n(define (sqrt2 x) (sqrt-itr 1.0 x))\r\n<\/pre>\n<p>\u307e\u305a\u3001\u52d5\u304d\u3092\u691c\u8a3c\u3059\u308b\u306e\u306b\u3001\u30c7\u30d0\u30c3\u30b0\u6587\u3092\u4ed5\u8fbc\u3093\u3060\u308a\u3057\u305f\u3044\u306e\u3060\u304c\u3001Scheme \u3067\u3069\u3046\u3059\u308b\u306e\u304b\u3055\u3063\u3071\u308a\u308f\u304b\u3089\u306a\u3044\u306e\u3067\u3001Python \u306b\u4e0a\u8a18\u3092\u79fb\u690d\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\"># -*-encoding:utf-8-*-\r\n'''\r\nhttp:\/\/ja.wikipedia.org\/wiki\/IEEE_754\r\n'''\r\nfrom decimal import *\r\n\r\ndef init_env():\r\n    '''\u74b0\u5883\u306e\u8a2d\u5b9a'''\r\n    global use_decimal   # Decimal \u3092\u5229\u7528\u3059\u308b\u304b\r\n    global judge         # \u8a08\u7b97\u6253\u3061\u5207\u308a\u5224\u5b9a\r\n    global initial_guess # \u63a8\u6e2c\u521d\u671f\u5024\r\n    \r\n    use_decimal = True  \r\n    getcontext().prec  = 6          \r\n    judge              = dcm('0.00001') \r\n    initial_guess      = '1.0'         \r\n    \r\ndef dcm(x):\r\n    '''\u6570\u5024\u3092\u6271\u3046\u578b\u3092\u5207\u308a\u66ff\u3048\u308b float, decimal'''\r\n    if use_decimal:\r\n        return Decimal(x)\r\n    else:\r\n        return float(x)\r\ndef square(x):\r\n    return dcm(x) * dcm(x)\r\ndef good_enough(guess ,x):\r\n    g2 = square(guess)\r\n    v = g2 - dcm(x)\r\n    if v &lt; dcm('0'):\r\n        v = v * dcm('-1')\r\n    print '*** %s -  %s = %s ***' % (g2, dcm(x), v)\r\n    return v &lt; judge\r\ndef avarage(x, y):\r\n    return (dcm(x) + dcm(y)) \/ dcm('2')\r\ndef improve(guess, x):\r\n    av = avarage((dcm(x) \/ dcm(guess)), guess) \r\n    print av\r\n    return av\r\ndef sqrt_itr(guess, x):\r\n    if good_enough(guess, x):\r\n        return dcm(guess)\r\n    else:\r\n        return sqrt_itr(improve(guess, x), x)\r\ndef sqrt2(x):\r\n    init_env()\r\n    return sqrt_itr(initial_guess, x)\r\n\r\nif __name__ == '__main__':\r\n    target = '0.00003'\r\n    print sqrt2(target)\r\n<\/pre>\n<p>init_env \u95a2\u6570\u306b\u3001\u8a2d\u5b9a\u3092\u884c\u3046\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n<h3>1.good-enough? \u30c6\u30b9\u30c8\u306f\u3001\u975e\u5e38\u306b\u5c0f\u3055\u3044\u6570\u306e\u5e73\u65b9\u6839\u3092\u3068\u308b\u3068\u304d\u306b\u306f\u52b9\u679c\u7684\u3067\u306f\u306a\u3044\u3002 <\/h3>\n<p>good-enough? \u30c6\u30b9\u30c8\u306f\u3001\u8aa4\u5dee\u304c 0.001 \u3068\u3044\u3046\u5b9a\u6570 \u4ee5\u4e0b\u306b\u306a\u3063\u305f\u3089\u3001\u8a08\u7b97\u3092\u6253\u3061\u5207\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<p>\u307e\u305a\u3001\u5b9a\u6570\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304b\u3089\u3001\u76f4\u611f\u7684\u306b\u975e\u5e38\u306b\u5c0f\u3055\u3044\u5024\u306e\u5834\u5408\u3001\u8aa4\u5dee\u306e\u5272\u5408\u304c\u76f8\u5bfe\u7684\u306b\u5927\u304d\u304f\u306a\u308b\u3068\u601d\u308f\u308c\u308b\u3002<\/p>\n<p>\u307e\u305f\u3001\u5224\u5b9a\u57fa\u6e96\u304c 0.001 \u3068\u306a\u3063\u3066\u3044\u308b\u304c\u3001\u3053\u306e\u5024\u3092\u6bd4\u8f03\u3059\u308b\u306e\u306f\u3001\u5e73\u65b9\u6839\u3092\u3068\u308b\u524d(\u3068\u308d\u3046\u3068\u3057\u3066\u3044\u308b)\u5024\u306e\u305f\u3081\u3001\u6c42\u3081\u308b\u5024\u304c1\u4ee5\u4e0b\u306e\u5024\u306e\u5834\u5408\u5e73\u65b9\u6839\u3082\u3055\u3089\u306b\u5c0f\u3055\u306a\u5024\u306b\u306a\u308a\u3001\u5224\u5b9a\u57fa\u6e96\u306e\u5024\u30010.001 \u304c\u3001\u6c42\u3081\u308b\u5024\u306b\u5bfe\u3057\u3066\u76f8\u5bfe\u7684\u306b\u304b\u306a\u308a\u5927\u304d\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/2_A5CE\/newton04_01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"newton04_01\" border=\"0\" alt=\"newton04_01\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/2_A5CE\/newton04_01_thumb.jpg\" width=\"431\" height=\"141\"><\/a> <\/p>\n<pre class=\"prettyprint linenums\">    use_decimal = True  \r\ndef init_env():\r\n    (\u7565)\r\n    use_decimal = True                       # Decimal \u3092\u4f7f\u3046  \r\n    getcontext().prec  = 6                   # Decimal \u306e\u6709\u52b9\u6841\u6570\u3092 6\u6841\u306b\r\n    judge              = dcm('0.00001')      # 0.001 \u3088\u308a\u7cbe\u5ea6\u3092\u3042\u3052\u308b\r\n    initial_guess      = '1.0'               \r\n\r\nif __name__ == '__main__':\r\n    target = '0.0003'\r\n    print sqrt2(target)\r\n\r\n<\/pre>\n<p>\u4e0a\u8a18 Python \u306e init_env \u306e\u8a2d\u5b9a\u3092\u4e0a\u8a18\u306b\u3057\u3066\u691c\u8a3c\u3057\u3066\u307f\u308b\u3002 <\/p>\n<p>*** \u4e88\u6e2c\u5024 &#8211; \u5e73\u65b9\u6839\u3092\u3068\u308b\u5024 = good-enough? \u306e\u5224\u5b9a\u5024 <\/p>\n<p>*** 1.00 &#8211;&nbsp; 0.0003 = 0.9997 ***<br \/>0.50015<br \/>*** 0.250150 &#8211;&nbsp; 0.0003 = 0.249850 ***<br \/>0.250375<br \/>*** 0.0626876 &#8211;&nbsp; 0.0003 = 0.0623876 ***<br \/>0.125786<br \/>*** 0.0158221 &#8211;&nbsp; 0.0003 = 0.0155221 ***<br \/>0.0640855<br \/>*** 0.00410695 &#8211;&nbsp; 0.0003 = 0.00380695 ***<br \/><font color=\"#ff0000\">0.0343834<\/font><br \/><font color=\"#ff0000\">*** 0.00118222 &#8211;&nbsp; 0.0003 = 0.00088222 ***&nbsp;&nbsp;&nbsp;&nbsp; \u2190 \u3053\u3053\u3067\u5224\u5b9aOK\u3068\u306a\u3063\u3066\u3057\u307e\u3046\u3002<br \/><\/font>0.0215542<br \/>*** 0.000464584 &#8211;&nbsp; 0.0003 = 0.000164584 ***<br \/>0.0177363<br \/>*** 0.000314576 &#8211;&nbsp; 0.0003 = 0.000014576 ***<br \/>0.0173254<br \/>*** 0.000300169 &#8211;&nbsp; 0.0003 = 1.69E-7 ***<br \/><font color=\"#ff0000\">0.0173254<\/font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<p>\u304b\u306a\u308a(0.001\u3088\u308a)\u7cbe\u5ea6\u304c\u60aa\u3044\u7d50\u679c\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u3002 <\/p>\n<p>&nbsp;<\/p>\n<h3>2.\u9650\u3089\u308c\u305f\u7cbe\u5ea6\u3067\u5b9f\u884c\u3055\u308c\u308b\u5834\u5408\u3001\u975e\u5e38\u306b\u5927\u304d\u3044\u6570\u306b\u3082\u4e0d\u9069\u5207\u3002 <\/h3>\n<p>\u7ffb\u3063\u3066\u3001\u975e\u5e38\u306b\u5927\u304d\u3044\u5024\u306e\u5834\u5408\u3002\u304a\u305d\u3089\u304f\u3001\u5185\u90e8\u6570\u5024\u8868\u73fe\u306e\u7cbe\u5ea6\u306b\u3088\u3063\u3066\u3001\u5bfe\u8c61\u304c\u5927\u304d\u3044\u5024\u3060\u3068 0.001 \u3068\u3044\u3046\u7cbe\u5ea6\u81ea\u4f53\u3092\u4fdd\u3066\u306a\u3044\u305f\u3081\u3060\u3068\u601d\u308f\u308c\u308b\u304c\u3001\u5b9f\u969b\u3069\u3046\u306a\u306e\u304b\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/2_A5CE\/newton04_02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"newton04_02\" border=\"0\" alt=\"newton04_02\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/2_A5CE\/newton04_02_thumb.jpg\" width=\"331\" height=\"138\"><\/a> <\/p>\n<p>\u3069\u3093\u3069\u3093\u5024\u3092\u5927\u304d\u304f\u3057\u3066\u3044\u304f\u3068\u3001\u7d50\u679c\u304c\u304b\u3048\u3063\u3066\u3053\u306a\u304f\u306a\u3063\u305f\u3002<\/p>\n<p>Python \u3067\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">    use_decimal = True  \r\ndef init_env():\r\n    (\u7565)\r\n    use_decimal = False                      # Decimal \u306f\u4f7f\u308f\u306a\u3044(float\u3068\u306a\u308b)\r\n    judge              = dcm('0.001')  \r\n    initial_guess      = '1.0'               \r\n\r\nif __name__ == '__main__':\r\n    target = '33333333333333333333333333333333333'\r\n    print sqrt2(target)\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3084\u306f\u308a\u3001\u3042\u308b\u7a0b\u5ea6\u4ee5\u4e0a\u306f\u3001\u4e88\u6e2c\u5024\u304c\u8fd1\u3065\u304b\u305a\u3001\u5909\u5316\u3057\u306a\u3044\u305f\u3081\u3001\u7121\u9650\u30eb\u30fc\u30d7\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u3088\u3046\u3060\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; (\u7565)<\/p>\n<p>*** 3.33333333333e+34 &#8211;&nbsp; 3.33333333333e+34 = 4.61168601843e+18 ***<br \/>1.82574185835e+17<br \/>*** 3.33333333333e+34 &#8211;&nbsp; 3.33333333333e+34 = 4.61168601843e+18 ***<br \/>1.82574185835e+17 <\/p>\n<p>&nbsp;&nbsp;&nbsp; (\u540c\u3058\u51e6\u7406\u306e\u7e70\u308a\u8fd4\u3057\u30fb\u30fb\u30fb)<\/p>\n<p>Traceback (most recent call last):<br \/>&nbsp; File &#8220;C:\\test.py&#8221;, line 50, in &lt;module&gt;<br \/>&nbsp;&nbsp;&nbsp; print sqrt2(target)<br \/>&nbsp; File &#8220;C:\\test.py&#8221;, line 46, in sqrt2<br \/>&nbsp;&nbsp;&nbsp; return sqrt_itr(initial_guess, x)<br \/>&nbsp; File &#8220;C:\\test.py&#8221;, line 43, in sqrt_itr<br \/>&nbsp;&nbsp; (\u7565)<br \/>RuntimeError: maximum recursion depth exceeded while calling a Python object<\/p>\n<p>Python \u3067\u3001 \u8a08\u7b97\u306b\u5229\u7528\u3059\u308b\u6570\u5024\u30c7\u30fc\u30bf\u578b\u3092\u3001<a href=\"http:\/\/ja.wikipedia.org\/wiki\/IEEE_754\" target=\"_blank\">float<\/a> \u304b\u3089 Decimal \u306b\u5909\u3048\u3066\u3001\u5341\u5206\u306b\u6841\u6570\u3092\u3068\u308c\u3070\u3001\u51e6\u7406\u306f\u5b8c\u4e86\u3059\u308b\u306e\u3067\u3001\u3042\u304f\u307e\u3067 \u5185\u90e8\u6570\u5024\u30c7\u30fc\u30bf\u578b\u306e\u554f\u984c\u3060\u308d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1.1.7 Newton \u6cd5\u306b\u3088\u308b\u5e73\u65b9\u6839 &nbsp; \u554f\u984c\u30001.7 \u3055\u3066\u3001\u3088\u3046\u3084\u304f\u554f\u984c\u306e\u5185\u5bb9\u81ea\u4f53\u3092\u7406\u89e3\u3067\u304d\u308b\u6e96\u5099\u304c\u6574\u3063\u305f\u306e\u3067\u3001\u8003\u3048\u3066\u307f\u308b\u3002\u554f\u984c\u306e\u6982\u8981\u3002 (define (square x) (* x x) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[33,20,34,36],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-lisp","category-python","category-scheme","category-reading"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}