{"id":214,"date":"2023-10-26T16:41:36","date_gmt":"2023-10-26T08:41:36","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=214"},"modified":"2023-11-01T13:49:40","modified_gmt":"2023-11-01T05:49:40","slug":"14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b14. \u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u4ee5\u4e8c\u8fdb\u5236\u7684\u9664\u6cd5\u8868\u793a. \u6bd4\u5982, \u5341\u8fdb\u5236\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.125<\/p>\n\n\n\n<p>\u5176\u503c\u4e3a 1\/10 + 2\/100 + 5\/1000, \u540c\u6837, \u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u5219\u4e3a:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.001<\/p>\n\n\n\n<p>\u5176\u503c\u4e3a 0\/2 + 0\/4 + 1\/8. \u8fd9\u4e24\u79cd\u8868\u793a\u6cd5\u7684\u503c\u662f\u4e00\u6837\u7684, \u552f\u4e00\u7684\u533a\u522b\u662f, \u524d\u8005\u4ee5\u5341\u8fdb\u5236\u8868\u793a, \u800c\u540e\u8005\u5219\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a.<\/p>\n\n\n\n<p>\u4e0d\u5e78\u7684\u662f, \u5927\u591a\u6570\u7684\u5341\u8fdb\u5236\u5c0f\u6570\u90fd\u65e0\u6cd5\u4e25\u683c\u7684\u4ee5\u4e8c\u8fdb\u5236\u6765\u8868\u793a. \u4e00\u4e2a\u7ed3\u679c\u5c31\u662f, \u666e\u904d\u6765\u8bf4, \u4f60\u8f93\u5165\u7684\u5341\u8fdb\u5236\u7684\u5c0f\u6570, \u901a\u5e38\u53ea\u662f\u4ee5\u63a5\u8fd1\u7684\u4e8c\u8fdb\u5236\u6570\u8868\u793a.<\/p>\n\n\n\n<p>\u5728\u5341\u8fdb\u5236\u4e2d\u8fd9\u4e2a\u95ee\u9898\u5f88\u5bb9\u6613\u7406\u89e3. \u8003\u8651\u5206\u6570 1\/3 . \u4f60\u53ef\u4ee5\u7528\u4e00\u4e2a\u63a5\u8fd1\u7684\u5341\u8fdb\u5236\u6570\u8868\u793a:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.3<\/p>\n\n\n\n<p>\u8981\u66f4\u4e00\u4e9b,<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.33<\/p>\n\n\n\n<p>\u66f4\u597d\u4e00\u4e9b,<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.333<\/p>\n\n\n\n<p>\u7b49\u7b49. \u4f46\u662f\u4e0d\u7ba1\u4f60\u600e\u4e48\u5199, \u90fd\u4e0d\u662f\u4e25\u683c\u7684\u7b49\u4e8e 1\/3, \u4f46\u662f\u53ef\u4ee5\u4f7f\u7ed3\u679c\u66f4\u63a5\u8fd1\u4e8e 1\/3.<\/p>\n\n\n\n<p>\u540c\u6837, \u65e0\u8bba\u4f60\u7528\u4e86\u591a\u5c11\u4f4d, \u4e8c\u8fdb\u5236\u7684\u6570\u4e5f\u65e0\u6cd5\u7cbe\u786e\u8868\u793a\u5341\u8fdb\u5236\u7684 0.1 . \u5728\u4ee5\u4e8c\u4e3a\u5e95\u7684\u60c5\u51b5\u4e0b, \u5c06\u662f\u4e2a\u65e0\u9650\u5faa\u73af<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.0001100110011001100110011001100110011001100110011&#8230;<\/p>\n\n\n\n<p>\u5728\u4efb\u610f\u6709\u9650\u7684\u4f4d\u4e0a\u505c\u6b62, \u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u8fd1\u4f3c\u503c. \u5728\u4eca\u5929\u7684\u5927\u591a\u6570\u673a\u5b50\u4e0a, \u6d6e\u70b9\u6570\u8fd1\u4f3c\u5730\u4f7f\u7528\u4e8c\u5143\u7684\u5206\u6570\u6765\u8868\u793a, \u5176\u5206\u5b50\u662f\u4e00\u4e2a 53 \u4f4d\u7684\u6570\u5b57, \u5206\u6bcd\u5219\u662f 2 \u7684\u5e42. \u50cf 1\/10, \u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a&nbsp;<code>3602879701896397&nbsp;\/&nbsp;2&nbsp;**&nbsp;55<\/code>. \u8fd9\u4e2a\u5f88\u63a5\u8fd1, \u4f46\u4e0d\u662f\u51c6\u786e\u7684\u7b49\u4e8e.<\/p>\n\n\n\n<p>\u5f88\u591a\u7528\u6237\u56e0\u4e3a\u8fd9\u4e9b\u503c\u663e\u793a\u7684\u65b9\u6cd5\u800c\u5e76\u4e0d\u77e5\u9053\u8fd1\u4f3c. Python \u4ec5\u4ec5\u6253\u5370\u4e00\u4e2a\u5408\u9002\u7684\u5341\u8fdb\u5236\u5206\u6570, \u800c\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u8fd1\u4f3c\u8fd8\u662f\u5b58\u50a8\u4e8e\u673a\u5668\u4e2d. \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b, \u5982\u679c\u8ba9 Python \u6253\u5370\u4e00\u4e2a\u5341\u8fdb\u5236\u5c0f\u6570, \u90a3\u4e48\u5176\u4f1a\u4ee5\u771f\u5b9e\u5b58\u50a8\u7684\u6570\u5b57\u663e\u793a, \u4f8b\u5982 0.1<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>0.1 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.1000000000000000055511151231257827021181583404541015625<\/p>\n\n\n\n<p>\u6b64\u5904\u7684\u4f4d\u6570\u6bd4\u4e00\u822c\u7528\u5230\u7684\u8981\u591a, \u6240\u4ee5 Python \u4f7f\u7528\u4e00\u4e2a\u8fd1\u4f3c\u7684\u503c\u4ee3\u66ff\u4e4b:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>1 \/ 10 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.1<\/p>\n\n\n\n<p>\u53ea\u8981\u8bb0\u4f4f, \u5c3d\u7ba1\u6253\u5370\u7684\u503c\u770b\u8d77\u6765\u662f\u51c6\u786e\u7684 1\/10 , \u4f46\u662f\u771f\u6b63\u5b58\u50a8\u7684\u53ea\u662f\u6700\u63a5\u8fd1\u7684\u4e8c\u5143\u5206\u6570\u7f62\u4e86.<\/p>\n\n\n\n<p>\u6709\u8da3\u7684\u662f, \u6709\u5f88\u591a\u4e0d\u540c\u7684\u503c\u5171\u4eab\u540c\u6837\u7684\u5206\u6570. \u4e3e\u4e2a\u4f8b\u5b50, \u6570\u5b57&nbsp;<code>0.1<\/code>&nbsp;\u548c&nbsp;<code>0.10000000000000001<\/code>&nbsp;\u53ca&nbsp;<code>0.1000000000000000055511151231257827021181583404541015625<\/code>&nbsp;\u90fd\u662f&nbsp;<code>3602879701896397&nbsp;\/&nbsp;2&nbsp;**&nbsp;55<\/code>&nbsp;\u7684\u8fd1\u4f3c. \u56e0\u4e3a\u540c\u6837\u7684\u5206\u6570\u503c\u8868\u793a\u4e0d\u540c\u503c\u7684\u8fd1\u4f3c, \u8fd9\u6837\u4efb\u4f55\u7684\u503c\u90fd\u53ef\u4ee5\u4fdd\u8bc1\u4e0d\u53d8\u5f0f&nbsp;<code>eval(repr(x))&nbsp;==&nbsp;x<\/code>.<\/p>\n\n\n\n<p>\u5386\u53f2\u539f\u56e0, Python \u7684\u63d0\u793a\u7b26\u548c\u5185\u7f6e\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>&nbsp;\u51fd\u6570\u4f1a\u9009\u62e9 17 \u4f4d,&nbsp;<code>0.10000000000000001<\/code>&nbsp;. \u5728 Python 3.1 \u5f00\u59cb, Python (\u5728\u7edd\u5927\u6570\u7684\u7cfb\u7edf\u4e0a), \u4f1a\u9009\u62e9\u6700\u7b80\u7684\u8868\u793a&nbsp;<code>0.1<\/code>.<\/p>\n\n\n\n<p>\u6ce8\u610f, \u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6d6e\u70b9\u6570\u5728\u6b64\u5904\u662f\u975e\u5e38\u81ea\u7136\u7684: \u8fd9\u4e2a\u4e0d\u662f Python \u4e2d\u7684 bug, \u4e5f\u4e0d\u662f\u4f60\u4ee3\u7801\u4e2d\u7684 bug . \u5728\u5f88\u591a\u652f\u6301\u786c\u4ef6\u6d6e\u70b9\u578b\u7684\u8bed\u8a00\u4e2d\u4e5f\u53ef\u4ee5\u770b\u5230\u8fd9\u6837\u7684\u4e8b\u60c5 (\u5c3d\u7ba1\u6709\u4e9b\u8bed\u8a00\u9ed8\u8ba4\u4e0b\u4e0d\u4f1a\u663e\u793a\u4e0d\u540c, \u6216\u5728\u6240\u6709\u7684\u8f93\u51fa\u6a21\u5f0f\u4e0b).<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u66f4\u591a\u53cb\u597d\u7684\u8f93\u51fa, \u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u4ea7\u751f\u4e00\u4e2a\u6709\u9650\u5236\u7684\u6570\u5b57:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>format(math.pi, &#8216;.12g&#8217;) <em># give 12 significant digits<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;3.14159265359&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>format(math.pi, &#8216;.2f&#8217;) <em># give 2 digits after the point<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;3.14&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>repr(math.pi)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;3.141592653589793&#8217;<\/p>\n\n\n\n<p>\u6709\u4e00\u70b9\u5f88\u91cd\u8981, \u4f60\u9700\u8981\u610f\u8bc6\u5230, \u5728\u771f\u5b9e\u60c5\u51b5\u4e0b, \u8fd9\u662f\u4e2a\u5e7b\u89c9: \u4f60\u4ec5\u4ec5\u662f\u56db\u820d\u4e94\u5165\u4e86\u663e\u793a\u7684\u771f\u5b9e\u503c.<\/p>\n\n\n\n<p>\u5176\u4e2d\u4e00\u4e2a\u5e7b\u89c9\u4f1a\u4ea7\u751f\u53e6\u4e00\u4e2a. \u4e3e\u4e2a\u4f8b\u5b50, \u56e0\u4e3a 0.1 \u5e76\u4e0d\u662f\u4e25\u683c\u7684 1\/10, \u4e09\u4e2a 0.1 \u76f8\u52a0\u5e76\u4e0d\u4f1a\u751f\u6210\u51c6\u786e\u7684 0.3:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>.1 + .1 + .1 == .3 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">False<\/p>\n\n\n\n<p>\u540c\u6837, \u56e0\u4e3a 0.1 \u4e0d\u80fd\u591f\u5f97\u5230\u66f4\u63a5\u8fd1 1\/10 \u7684\u503c, \u800c 0.3 \u4e0d\u80fd\u5f97\u5230\u66f4\u63a5\u8fd1 3\/10 \u7684\u503c, \u56e0\u6b64\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#round\"><code>round()<\/code><\/a>\u00a0\u51fd\u6570\u6765\u8fdb\u884c\u56db\u820d\u4e94\u5165\u4e5f\u662f\u4e0d\u8d77\u4f5c\u7528\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> False<\/p>\n\n\n\n<p>\u5c3d\u7ba1\u6570\u5b57\u4e0d\u80fd\u591f\u66f4\u63a5\u8fd1\u5b83\u4eec\u7406\u60f3\u4e0a\u7684\u51c6\u786e\u503c,\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#round\"><code>round()<\/code><\/a>\u00a0\u51fd\u6570\u5728\u8ba1\u7b97\u540e\u4f7f\u7528, \u786e\u5b9e\u53ef\u4ee5\u5b9e\u73b0\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u6bd4\u8f83:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>round(.1 + .1 + .1, 10) == round(.3, 10) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">True<\/p>\n\n\n\n<p>\u50cf\u8fd9\u6837\u7684\u6d6e\u70b9\u6570\u7b97\u6cd5, \u4f1a\u5bfc\u81f4\u5f88\u591a\u4ee4\u4eba\u5947\u602a\u7684\u4e8b\u60c5. \u201c0.1\u201d \u7684\u95ee\u9898\u5c06\u5728\u540e\u9762\u66f4\u8be6\u7ec6\u7684\u89e3\u91ca, \u5177\u4f53\u770b \u201cRepresentation Error\u201d \u90a3\u8282. \u66f4\u591a\u5e38\u89c1\u7684\u4ee4\u4eba\u5403\u60ca\u7684\u4e8b\u60c5, \u53ef\u4ee5\u53c2\u8003&nbsp;<a href=\"http:\/\/www.lahey.com\/float.htm\">The Perils of Floating Point<\/a>&nbsp;.<\/p>\n\n\n\n<p>\u5c31\u50cf\u540e\u9762\u8bf4\u7684, \u201c\u6ca1\u6709\u7b80\u5355\u7684\u7b54\u6848.\u201d \u4f46\u662f, \u5bf9\u4e8e\u6d6e\u70b9\u6570\u8bf7\u4e0d\u8981\u8fc7\u5ea6\u8c28\u614e. \u5728 Python \u4e2d\u8fd9\u4e9b\u6d6e\u70b9\u6570\u7684\u95ee\u9898\u6765\u6e90\u4e8e\u5176\u786c\u4ef6, \u5728\u591a\u6570\u7684\u673a\u5668\u4e2d, \u6d6e\u70b9\u7684\u7cbe\u5ea6\u6ca1\u6709\u5fc5\u8981\u8fbe\u5230 1\/2**53 . \u5bf9\u4e8e\u666e\u901a\u7684\u4efb\u52a1\u5df2\u7ecf\u8db3\u591f\u4e86, \u4f60\u9700\u8981\u8bb0\u4f4f\u7684\u5c31\u662f, \u8fd9\u4e0d\u662f\u7b97\u6570\u7684\u95ee\u9898, \u6bcf\u4e2a\u6d6e\u70b9\u6570\u64cd\u4f5c\u90fd\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u95ee\u9898.<\/p>\n\n\n\n<p>\u5f53\u4e0d\u5408\u7406\u7684\u60c5\u51b5\u771f\u7684\u5b58\u5728\u65f6, \u5bf9\u4e8e\u591a\u6570\u60c5\u51b5, \u4f60\u6700\u7ec8\u8fd8\u662f\u80fd\u5f97\u5230\u5e0c\u671b\u7684\u7ed3\u679c, \u5982\u679c\u4f60\u5c06\u663e\u793a\u7684\u6570\u503c\u8fdb\u884c\u56db\u820d\u4e94\u5165, \u5e76\u786e\u4fdd\u4f60\u6240\u9700\u8981\u7684\u4f4d\u6570.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>&nbsp;\u51fd\u6570\u7ecf\u5e38\u5c31\u80fd\u6ee1\u8db3\u9700\u8981\u4e86, \u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>&nbsp;\u65b9\u6cd5, \u6765\u6307\u660e\u5176&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#formatstrings\">Format String Syntax<\/a>.<\/p>\n\n\n\n<p>\u5728\u9700\u8981\u4e25\u683c\u7684\u6570\u503c\u8868\u793a\u65f6, \u8bd5\u8bd5\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/decimal.html#module-decimal\"><code>decimal<\/code><\/a>&nbsp;\u6a21\u5757, \u8fd9\u4e2a\u6a21\u5757\u5b9e\u73b0\u4e86\u7528\u4e8e\u8d26\u76ee\u8fd0\u7b97\u6216\u66f4\u9ad8\u7cbe\u5ea6\u65f6\u7528\u5230\u7684\u6570\u503c\u7b97\u6cd5.<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u5c31\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/fractions.html#module-fractions\"><code>fractions<\/code><\/a>&nbsp;\u6a21\u5757, \u5b83\u5b9e\u73b0\u4e86\u57fa\u4e8e\u6709\u7406\u6570\u7684\u7b97\u6cd5 ( \u6240\u4ee5 1\/3 \u5c31\u53ef\u4ee5\u51c6\u786e\u7684\u8868\u8ff0 ).<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u5927\u91cf\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97, \u90a3\u4e48\u4f60\u53ef\u4ee5\u770b\u770b Python \u7684\u6570\u503c\u5e93\u6216\u5176\u4ed6\u7684\u8ba1\u7b97\u548c\u7edf\u8ba1\u7684\u5305, SciPy \u8fd9\u4e2a\u9879\u76ee\u5bf9\u6b64\u6709\u5f88\u597d\u7684\u652f\u6301. \u53c2\u8003 &lt;<a href=\"http:\/\/scipy.org\/\">http:\/\/scipy.org<\/a>&gt;.<\/p>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86\u5de5\u5177\u6765\u5e2e\u52a9\u4f60\u83b7\u5f97\u6d6e\u70b9\u6570\u7684\u51c6\u786e\u503c. \u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#float.as_integer_ratio\"><code>float.as_integer_ratio()<\/code><\/a>\u00a0\u65b9\u6cd5\u6765\u8868\u793a\u4e00\u4e2a\u5206\u6570:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x = 3.14159<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>x.as_integer_ratio() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">(3537115888337719, 1125899906842624)<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u6bd4\u7387\u662f\u51c6\u786e\u7684, \u5b83\u5c31\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u539f\u59cb\u7684\u6570\u5b57:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x == 3537115888337719 \/ 1125899906842624 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">True<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#float.hex\"><code>float.hex()<\/code><\/a>\u00a0\u65b9\u6cd5\u4ee5\u5341\u516d\u8fdb\u5236\u8868\u8ff0, \u8fd9\u4e5f\u540c\u6837\u7ed9\u51fa\u4e86\u4e00\u4e2a\u88ab\u4f60\u8ba1\u7b97\u673a\u51c6\u786e\u5b58\u50a8\u7684\u503c:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x.hex() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;0x1.921f9f01b866ep+1&#8217;<\/p>\n\n\n\n<p>\u524d\u9762\u7684\u5341\u516d\u8fdb\u5236\u8868\u793a, \u53ef\u4ee5\u7528\u6765\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u6d6e\u70b9\u503c:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x == float.fromhex(&#8216;0x1.921f9f01b866ep+1&#8217;) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">True<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u8868\u793a\u662f\u4e25\u683c\u7684, \u6240\u4ee5\u5bf9\u4e8e\u4e0d\u540c\u7248\u672c\u7684 Python (\u8de8\u5e73\u53f0) \u90fd\u662f\u517c\u5bb9\u7684, \u800c\u4e14\u4e5f\u53ef\u4ee5\u548c\u5176\u4ed6\u7684\u8bed\u8a00\u8fdb\u884c\u4ea4\u6362 (\u6bd4\u5982 java \u548c C99).<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177\u5c31\u662f\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/math.html#math.fsum\"><code>math.fsum()<\/code><\/a>\u00a0\u51fd\u6570. \u5b83\u53ef\u4ee5\u5728\u8ba1\u7b97\u603b\u548c\u65f6\u51cf\u5c11\u7cbe\u5ea6\u7684\u4e22\u5931. \u5b83\u4f1a\u8bb0\u5f55\u5728\u6c42\u548c\u65f6\u4e22\u5931\u7684\u7cbe\u5ea6. \u8fd9\u6837\u8bef\u5dee\u5c31\u4e0d\u4f1a\u79ef\u7d2f\u800c\u6700\u7ec8\u5f71\u54cd\u7ed3\u679c\u4e86.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>sum([0.1] * 10) == 1.0 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">False <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>math.fsum([0.1] * 10) == 1.0 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">True<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">14.1. \u8868\u793a\u9519\u8bef<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/floatingpoint.html#tut-fp-error\"><\/a><\/h2>\n\n\n\n<p>\u672c\u8282\u4f1a\u66f4\u8be6\u7ec6\u7684\u89e3\u91ca \u201c0.1\u201d \u7684\u4f8b\u5b50, \u5e76\u4e14\u6559\u4f60\u5982\u4f55\u8fdb\u884c\u51c6\u786e\u7684\u5206\u6790. \u6b64\u5904\u5047\u8bbe\u4f60\u5df2\u6709\u4e86\u57fa\u672c\u7684\u4e8c\u5143\u6d6e\u70b9\u6570\u8868\u793a\u7684\u57fa\u7840.<\/p>\n\n\n\n<p><em>Representation error<\/em>&nbsp;\u6d89\u53ca\u5230\u8fd9\u6837\u7684\u4e8b\u5b9e, \u6709\u4e9b (\u66f4\u51c6\u786e\u6765\u4e66\u662f\u5927\u591a\u6570) \u5c0f\u6570\u7684\u5206\u6570\u8868\u793a\u4e0d\u80fd\u591f\u4ee5\u4e8c\u8fdb\u5236\u4e3a\u5e95\u7684\u5206\u6570\u8868\u8ff0. \u8fd9\u5c31\u662f\u4e3b\u8981\u7684\u539f\u56e0, \u4e3a\u4f55 Python (\u6216\u8005 Perl, C, C++, Java, Fortran, \u8fd8\u6709\u66f4\u591a\u7684) \u5e38\u5e38\u4e0d\u80fd\u591f\u5982\u4f60\u6240\u613f\u7684\u8868\u793a.<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u90a3\u6837? 1\/10 \u4e0d\u80fd\u591f\u88ab\u4e8c\u8fdb\u5236\u7684\u5206\u6570\u51c6\u786e\u8868\u793a. \u57fa\u672c\u4e0a\u5168\u90e8\u7684\u673a\u5668\u5728\u4eca (2000\u5e7411\u6708) \u6765\u8bf4\u90fd\u662f\u4f7f\u7528\u4e86 IEEE-754 \u6d6e\u70b9\u6570\u7b97\u6cd5, \u5e76\u4e14\u51e0\u4e4e\u5168\u90e8\u7684\u5e73\u53f0\u5c06 Python \u7684\u6d6e\u70b9\u6620\u5c04\u4e3a IEEE-754 \u201cdouble\u7cbe\u5ea6\u201d. 754 doubles \u5305\u542b\u4e86 53 \u4f4d\u7684\u7cbe\u5ea6, \u6240\u4ee5\u5728\u8ba1\u7b97\u673a\u4e2d, 0.1 \u88ab\u8f6c\u6210\u4e00\u4e2a\u5f88\u63a5\u8fd1\u7684\u5206\u6570, \u800c\u5b83\u53c8\u53ef\u4ee5\u8fd9\u79cd\u00a0<em>J<\/em>\/2**<em>N<\/em>\u00a0\u7684\u5f62\u5f0f\u8868\u793a, \u6b64\u5904\u7684\u00a0<em>J<\/em>\u00a0\u662f\u4e00\u4e2a\u5305\u542b 53 \u4f4d\u7684\u6574\u6570. \u91cd\u5199:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">1 \/ 10 ~= J \/ (2**N)<\/p>\n\n\n\n<p>\u4e3a:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">J ~= 2**N \/ 10<\/p>\n\n\n\n<p>\u5e76\u4e14\u8bb0\u7740\u00a0<em>J<\/em>\u00a0\u6709\u4e25\u683c\u7684 53 \u4f4d (\u4e5f\u5c31\u662f\u00a0<code>>=\u00a02**52<\/code>\u00a0\u4f46\u00a0<code>&lt;\u00a02**53<\/code>), \u5bf9\u4e8e\u00a0<em>N<\/em>\u00a0\u6700\u597d\u7684\u503c\u5c31\u662f 56:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>2**52 &lt;= 2**56 \/\/ 10 &lt; 2**53<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> True<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4, 56 \u662f\u552f\u4e00\u80fd\u8ba9\u00a0<em>J<\/em>\u00a0\u4e3a 53 \u4f4d\u7684\u00a0<em>N<\/em>\u00a0\u503c. \u800c\u00a0<em>J<\/em>\u00a0\u6700\u6709\u53ef\u80fd\u7684\u503c\u5c31\u662f\u90a3\u4e2a\u5546:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>q, r = divmod(2**56, 10)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>r <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">6<\/p>\n\n\n\n<p>\u56e0\u4e3a\u5269\u4f59\u7684\u5982\u679c\u5927\u4e8e10\u7684\u4e00\u534a, \u90a3\u4e48\u6700\u597d\u7684\u8fd1\u4f3c\u5c31\u662f\u8fdb\u4e00\u4f4d:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>q+1 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">7205759403792794<\/p>\n\n\n\n<p>\u6240\u4ee5\u4ee5 754 double \u7cbe\u5ea6\u8868\u793a\u7684 1\/10 \u6700\u5408\u9002\u7684\u503c\u5c31\u662f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">7205759403792794 \/ 2 ** 56<\/p>\n\n\n\n<p>\u5c06\u5206\u5b50\u5206\u6bcd\u7ea6\u5316:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">3602879701896397 \/ 2 ** 55<\/p>\n\n\n\n<p>\u6ce8\u610f, \u56e0\u4e3a\u6211\u4eec\u8fdb\u4e86\u4e00\u4f4d, \u6240\u4ee5\u503c\u4f1a\u6bd4 1\/10 \u7a0d\u5fae\u5927\u4e00\u70b9; \u5982\u679c\u6211\u4eec\u6ca1\u6709\u8fdb\u4f4d, \u90a3\u4e48\u5546\u53c8\u4f1a\u6bd4 1\/10 \u7a0d\u5fae\u5c0f\u70b9. \u4f46\u65e0\u8bba\u5982\u4f55, \u90fd\u4e0d\u662f\u51c6\u786e\u7684 1\/10!<\/p>\n\n\n\n<p>\u6240\u4ee5\u8ba1\u7b97\u673a\u4ece\u6ca1\u6709\u770b\u8fc7 1\/10: \u5b83\u770b\u5230\u7684\u662f\u524d\u9762\u7ed9\u51fa\u7684\u5206\u6570, \u4ee5 754 \u53cc\u8fdb\u5ea6\u8fd1\u4f3c\u7684\u7ed3\u679c:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>0.1 * 2 ** 55 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">3602879701896397.0<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5206\u6570\u4e58\u4ee5 10**55, \u6211\u4eec\u53ef\u4ee5\u770b\u523055\u4f4d\u7684\u6570\u5b57:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>3602879701896397 * 10 ** 55 \/\/ 2 ** 55 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">1000000000000000055511151231257827021181583404541015625<\/p>\n\n\n\n<p>\u8fd9\u610f\u5473\u5b58\u4e8e\u8ba1\u7b97\u673a\u4e2d\u7684\u51c6\u786e\u503c\u7b49\u4e8e 0.1000000000000000055511151231257827021181583404541015625. \u5f88\u591a\u8bed\u8a00 (\u5305\u62ec Python \u7684\u65e7\u7248\u672c) \u4e0d\u662f\u76f4\u63a5\u663e\u793a\u6240\u6709\u7684\u4f4d\u6570, \u800c\u662f\u5c06\u5176\u4fdd\u7559\u4e3a17\u4f4d\u6709\u6548\u6570\u5b57:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>format(0.1, &#8216;.17f&#8217;) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;0.10000000000000001&#8217;<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/fractions.html#module-fractions\"><code>fractions<\/code><\/a>\u00a0\u548c\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/decimal.html#module-decimal\"><code>decimal<\/code><\/a>\u00a0\u6a21\u5757\u4f7f\u8fd9\u4e9b\u8ba1\u7b97\u53d8\u5f97\u7b80\u5355:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> fromdecimalimport<\/strong> Decimal <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> fromfractionsimport<\/strong> Fraction <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>Fraction.from_float(0.1) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Fraction(3602879701896397, 36028797018963968)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>(0.1).as_integer_ratio() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">(3602879701896397, 36028797018963968) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>Decimal.from_float(0.1) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Decimal(&#8216;0.1000000000000000055511151231257827021181583404541015625&#8217;) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>format(Decimal.from_float(0.1), &#8216;.17&#8217;)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;0.10000000000000001&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u4ee5\u4e8c\u8fdb\u5236\u7684\u9664\u6cd5\u8868\u793a. \u6bd4\u5982, \u5341\u8fdb\u5236\u7684: 0.125 \u5176\u503c\u4e3a 1\/10 + 2\/100 + 5 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":6,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":433,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions\/433"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}