{"id":1140753,"date":"2025-01-08T22:24:31","date_gmt":"2025-01-08T14:24:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140753.html"},"modified":"2025-01-08T22:24:34","modified_gmt":"2025-01-08T14:24:34","slug":"python%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e4%ba%8c%e4%b8%aa%e6%b5%ae%e7%82%b9%e6%95%b0%e6%98%af%e5%90%a6%e7%9b%b8%e7%ad%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1140753.html","title":{"rendered":"python\u5982\u4f55\u6bd4\u8f83\u4e8c\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103649\/a03f67e1-55e0-4a5e-abd2-6897fd52e671.webp\" alt=\"python\u5982\u4f55\u6bd4\u8f83\u4e8c\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\" \/><\/p>\n<p><p> <strong>Python\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528math.isclose()\u3001\u81ea\u5b9a\u4e49\u8bef\u5dee\u8303\u56f4\u3001decimal\u6a21\u5757\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u5728\u6307\u5b9a\u7684\u76f8\u5bf9\u548c\u7edd\u5bf9\u8bef\u5dee\u8303\u56f4\u5185\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u7684\u76f8\u7b49\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\uff0c\u6d6e\u70b9\u6570\u7684\u6bd4\u8f83\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\uff0c\u56e0\u4e3a\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u5e76\u4e0d\u80fd\u7cbe\u786e\u8868\u793a\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5c0f\u6570\u70b9\u7684\u8ba1\u7b97\u65f6\u3002\u8fd9\u5bfc\u81f4\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u5f80\u5f80\u4f1a\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u6bd4\u8f83\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\uff0c\u5e76\u5206\u6790\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528math.isclose()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python 3.5\u5f15\u5165\u4e86<code>math.isclose()<\/code>\u51fd\u6570\uff0c\u8fd9\u662f\u6bd4\u8f83\u6d6e\u70b9\u6570\u6700\u5e38\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee\uff0c\u4ece\u800c\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u5224\u65ad\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 0.1 + 0.2<\/p>\n<p>b = 0.3<\/p>\n<p>print(math.isclose(a, b))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.1 \u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee<\/h3>\n<\/p>\n<p><p><code>math.isclose()<\/code>\u51fd\u6570\u6709\u4e24\u4e2a\u91cd\u8981\u53c2\u6570<code>rel_tol<\/code>\u548c<code>abs_tol<\/code>\uff0c\u5206\u522b\u8868\u793a\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u76f8\u5bf9\u8bef\u5dee\uff08rel_tol\uff09<\/strong>\uff1a\u8fd9\u662f\u4e24\u4e2a\u6570\u503c\u76f8\u5bf9\u5927\u5c0f\u7684\u8bef\u5dee\uff0c\u9002\u7528\u4e8e\u6570\u503c\u8f83\u5927\u4e14\u6bd4\u4f8b\u91cd\u8981\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7edd\u5bf9\u8bef\u5dee\uff08abs_tol\uff09<\/strong>\uff1a\u8fd9\u662f\u4e24\u4e2a\u6570\u503c\u7edd\u5bf9\u5927\u5c0f\u7684\u8bef\u5dee\uff0c\u9002\u7528\u4e8e\u6570\u503c\u8f83\u5c0f\u4e14\u7cbe\u5ea6\u91cd\u8981\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 0.1 + 0.2<\/p>\n<p>b = 0.3<\/p>\n<h2><strong>\u8bbe\u7f6e\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee<\/strong><\/h2>\n<p>print(math.isclose(a, b, rel_tol=1e-9, abs_tol=1e-9))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u8bef\u5dee\u8303\u56f4<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684Python\u7248\u672c\u4f4e\u4e8e3.5\uff0c\u6216\u8005\u4f60\u6709\u7279\u5b9a\u7684\u8bef\u5dee\u63a7\u5236\u8981\u6c42\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a\u8bef\u5dee\u8303\u56f4\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_close(a, b, tol=1e-9):<\/p>\n<p>    return abs(a - b) &lt; tol<\/p>\n<p>a = 0.1 + 0.2<\/p>\n<p>b = 0.3<\/p>\n<p>print(is_close(a, b))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.1 \u8bef\u5dee\u8303\u56f4\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u8bef\u5dee\u8303\u56f4\u975e\u5e38\u91cd\u8981\uff0c\u8fc7\u5927\u6216\u8fc7\u5c0f\u7684\u8bef\u5dee\u8303\u56f4\u90fd\u4f1a\u5bfc\u81f4\u6bd4\u8f83\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u901a\u5e38\uff0c1e-9\u662f\u4e00\u4e2a\u6bd4\u8f83\u5e38\u7528\u7684\u8bef\u5dee\u8303\u56f4\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6d6e\u70b9\u6570\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528decimal\u6a21\u5757\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\uff0c\u9002\u7528\u4e8e\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>a = Decimal(&#39;0.1&#39;) + Decimal(&#39;0.2&#39;)<\/p>\n<p>b = Decimal(&#39;0.3&#39;)<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.1 decimal\u6a21\u5757\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u7cbe\u5ea6\u9ad8\uff0c\u9002\u7528\u4e8e\u91d1\u878d\u8ba1\u7b97\u7b49\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u6027\u80fd\u8f83\u5dee\uff0c\u8ba1\u7b97\u901f\u5ea6\u6bd4\u666e\u901a\u6d6e\u70b9\u6570\u8fd0\u7b97\u6162\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528numpy\u7684allclose()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c<code>numpy.allclose()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = 0.1 + 0.2<\/p>\n<p>b = 0.3<\/p>\n<p>print(np.allclose(a, b))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.1 allclose()\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>numpy.allclose()<\/code>\u51fd\u6570\u4e0e<code>math.isclose()<\/code>\u7c7b\u4f3c\uff0c\u4e5f\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = 0.1 + 0.2<\/p>\n<p>b = 0.3<\/p>\n<h2><strong>\u8bbe\u7f6e\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee<\/strong><\/h2>\n<p>print(np.allclose(a, b, rtol=1e-9, atol=1e-9))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6d6e\u70b9\u6570\u6bd4\u8f83\u4e2d\u7684\u5e38\u89c1\u95ee\u9898<\/h2>\n<\/p>\n<p><h3>5.1 \u7cbe\u5ea6\u4e22\u5931<\/h3>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u4e8c\u8fdb\u5236\u8868\u793a\u7684\uff0c\u8fd9\u5bfc\u81f4\u4e00\u4e9b\u5341\u8fdb\u5236\u6570\u4e0d\u80fd\u88ab\u7cbe\u786e\u8868\u793a\u3002\u4f8b\u5982\uff0c0.1\u5728\u4e8c\u8fdb\u5236\u4e2d\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u5c0f\u6570\uff0c\u56e0\u6b64\u4f1a\u6709\u7cbe\u5ea6\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(0.1 + 0.2 == 0.3)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u8ba1\u7b97\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97\u987a\u5e8f\u4e5f\u4f1a\u5f71\u54cd\u7ed3\u679c\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8fd0\u7b97\u90fd\u4f1a\u6709\u5fae\u5c0f\u7684\u8bef\u5dee\u79ef\u7d2f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 1.0e16 + 1.0 - 1.0e16<\/p>\n<p>b = 1.0e16 - 1.0e16 + 1.0<\/p>\n<p>print(a == b)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u4e0d\u540c\u7684\u6d6e\u70b9\u6570\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u548c\u786c\u4ef6\u5e73\u53f0\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u6d6e\u70b9\u6570\u8868\u793a\u548c\u8fd0\u7b97\u65b9\u5f0f\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u5f97\u5230\u4e0d\u540c\u7684\u6bd4\u8f83\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><h3>6.1 \u4f7f\u7528\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u7684Python\u7248\u672c\u652f\u6301\uff0c\u4f18\u5148\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\uff1b\u5982\u679c\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\uff0c\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>6.2 \u8bbe\u7f6e\u5408\u7406\u7684\u8bef\u5dee\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684\u5e94\u7528\u573a\u666f\u8bbe\u7f6e\u5408\u7406\u7684\u76f8\u5bf9\u8bef\u5dee\u548c\u7edd\u5bf9\u8bef\u5dee\u3002\u901a\u5e38\uff0c1e-9\u662f\u4e00\u4e2a\u6bd4\u8f83\u5e38\u7528\u7684\u8bef\u5dee\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>6.3 \u907f\u514d\u76f4\u63a5\u4f7f\u7528==\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83\u6d6e\u70b9\u6570\uff0c\u9664\u975e\u4f60\u975e\u5e38\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6570\u503c\u662f\u7cbe\u786e\u76f8\u7b49\u7684\u3002<\/p>\n<\/p>\n<p><h3>6.4 \u6d4b\u8bd5\u548c\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u51c6\u786e\u6027\u3002\u7279\u522b\u662f\u5728\u91d1\u878d\u8ba1\u7b97\u7b49\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\uff0c\u66f4\u8981\u8c28\u614e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>6.5 \u8003\u8651\u8ba1\u7b97\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u590d\u6742\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\u65f6\uff0c\u5c3d\u91cf\u51cf\u5c11\u8ba1\u7b97\u987a\u5e8f\u5bf9\u7ed3\u679c\u7684\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u6392\u5217\u8fd0\u7b97\u987a\u5e8f\u6765\u51cf\u5c11\u8bef\u5dee\u79ef\u7d2f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u91cd\u65b0\u6392\u5217\u8fd0\u7b97\u987a\u5e8f<\/p>\n<p>a = (1.0e16 + 1.0) - 1.0e16<\/p>\n<p>b = 1.0e16 - 1.0e16 + 1.0<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.6 \u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u6d6e\u70b9\u6570\u5df2\u7ecf\u8db3\u591f\uff1b\u5bf9\u4e8e\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>decimal<\/code>\u6216\u5176\u4ed6\u9ad8\u7cbe\u5ea6\u5e93\u3002<\/p>\n<\/p>\n<p><h3>6.7 \u4e86\u89e3\u6d6e\u70b9\u6570\u7684\u9650\u5236<\/h3>\n<\/p>\n<p><p>\u7406\u89e3\u6d6e\u70b9\u6570\u7684\u8868\u793a\u548c\u8fd0\u7b97\u65b9\u5f0f\uff0c\u4e86\u89e3\u5176\u5c40\u9650\u6027\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5904\u7406\u6d6e\u70b9\u6570\u6bd4\u8f83\u95ee\u9898\u3002\u9605\u8bfb\u76f8\u5173\u6587\u732e\u548c\u8d44\u6599\uff0c\u4f8b\u5982IEEE 754\u6807\u51c6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u7406\u89e3\u6d6e\u70b9\u6570\u8fd0\u7b97\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u6d6e\u70b9\u6570\u7684\u6bd4\u8f83\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528\u5408\u9002\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u6700\u4f73\u5b9e\u8df5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u5904\u7406\u6d6e\u70b9\u6570\u6bd4\u8f83\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\uff1f<\/strong><br \/>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u5e76\u4e0d\u80fd\u7cbe\u786e\u8868\u793a\uff0c\u56e0\u6b64\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u5916\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u6d6e\u70b9\u6570\u7684\u76f8\u7b49\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u76f8\u5bf9\u6216\u7edd\u5bf9\u5bb9\u5dee\uff0c\u4ece\u800c\u66f4\u7075\u6d3b\u5730\u5224\u65ad\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u5728\u53ef\u63a5\u53d7\u7684\u8303\u56f4\u5185\u76f8\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u6bd4\u8f83\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u4f7f\u7528<code>math.isclose()<\/code>\u662f\u4e00\u4e2a\u5b89\u5168\u7684\u65b9\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u8c03\u7684\u5bb9\u5dee\uff0c\u80fd\u591f\u5904\u7406\u7531\u4e8e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u5bfc\u81f4\u7684\u6bd4\u8f83\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>abs(a - b) &lt; tolerance<\/code>\u7684\u65b9\u5f0f\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u6bd4\u8f83\uff0c\u5176\u4e2d<code>tolerance<\/code>\u662f\u4e00\u4e2a\u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u6bd4\u8f83\u5bb9\u5dee\uff0c\u901a\u5e38\u9009\u7528\u4e00\u4e2a\u5f88\u5c0f\u7684\u503c\uff0c\u6bd4\u5982<code>1e-9<\/code>\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u76f4\u63a5\u4f7f\u7528\u201c==\u201d\u8fd0\u7b97\u7b26\u6bd4\u8f83\u6d6e\u70b9\u6570\u53ef\u80fd\u4e0d\u53ef\u9760\uff1f<\/strong><br \/>\u76f4\u63a5\u4f7f\u7528\u201c==\u201d\u8fd0\u7b97\u7b26\u6bd4\u8f83\u6d6e\u70b9\u6570\u662f\u4e0d\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u6d6e\u70b9\u6570\u5728\u5185\u5b58\u4e2d\u7684\u8868\u793a\u53ef\u80fd\u4f1a\u56e0\u4e3a\u7cbe\u5ea6\u9650\u5236\u800c\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff0c0.1\u548c0.2\u7684\u548c\u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u65e0\u6cd5\u7cbe\u786e\u8868\u793a\u4e3a0.3\uff0c\u56e0\u6b64\u6bd4\u8f83\u65f6\u53ef\u80fd\u4f1a\u8fd4\u56deFalse\u3002\u91c7\u7528\u5408\u9002\u7684\u6d6e\u70b9\u6570\u6bd4\u8f83\u65b9\u6cd5\u80fd\u907f\u514d\u8fd9\u79cd\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528math.isclose()\u3001\u81ea\u5b9a\u4e49\u8bef\u5dee\u8303\u56f4\u3001decimal [&hellip;]","protected":false},"author":3,"featured_media":1140762,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140753"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1140753"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140753\/revisions"}],"predecessor-version":[{"id":1140765,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140753\/revisions\/1140765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140762"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}