{"id":1022878,"date":"2024-12-27T13:39:21","date_gmt":"2024-12-27T05:39:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1022878.html"},"modified":"2024-12-27T13:39:23","modified_gmt":"2024-12-27T05:39:23","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e8%be%be%e7%ba%a6%e7%ad%89%e4%ba%8e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1022878.html","title":{"rendered":"python\u5982\u4f55\u8868\u8fbe\u7ea6\u7b49\u4e8e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25165315\/b0cb8652-81c5-4b1d-825d-06e8119bfccf.webp\" alt=\"python\u5982\u4f55\u8868\u8fbe\u7ea6\u7b49\u4e8e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7ea6\u7b49\u4e8e\u901a\u5e38\u7528\u6765\u8868\u793a\u4e24\u4e2a\u6570\u503c\u4e4b\u95f4\u7684\u5dee\u5f02\u5728\u4e00\u4e2a\u53ef\u63a5\u53d7\u7684\u8303\u56f4\u5185\u3002<strong>Python\u4e2d\u8868\u8fbe\u7ea6\u7b49\u4e8e\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u201cmath.isclose()\u51fd\u6570\u3001\u624b\u52a8\u8bbe\u7f6e\u4e00\u4e2a\u516c\u5dee\u8303\u56f4\u3001Numpy\u7684allclose()\u51fd\u6570<\/strong>\u3002\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5176\u4e2d\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u4e24\u4e2a\u6570\u503c\u7684\u8fd1\u4f3c\u76f8\u7b49\u5224\u65ad\u3002<strong>\u201cmath.isclose()\u201d\u51fd\u6570\u662fPython\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u5728\u4e00\u4e2a\u53ef\u63a5\u53d7\u7684\u76f8\u5bf9\u6216\u7edd\u5bf9\u516c\u5dee\u8303\u56f4\u5185\u3002<\/strong>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u6d6e\u70b9\u6570\u6bd4\u8f83\u4e2d\u5e38\u89c1\u7684\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528math.isclose()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cmath.isclose()\u201d\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u5728\u67d0\u4e2a\u6307\u5b9a\u7684\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002\u8be5\u51fd\u6570\u5728Python 3.5\u4e2d\u88ab\u5f15\u5165\uff0c\u6781\u5927\u5730\u65b9\u4fbf\u4e86\u5bf9\u4e8e\u6d6e\u70b9\u6570\u7684\u8fd1\u4f3c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u201cmath.isclose()\u201d\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u662f\u9700\u8981\u6bd4\u8f83\u7684\u4e24\u4e2a\u6d6e\u70b9\u6570\uff0c<code>rel_tol<\/code>\u662f\u76f8\u5bf9\u516c\u5dee\uff0c<code>abs_tol<\/code>\u662f\u7edd\u5bf9\u516c\u5dee\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>rel_tol<\/code>\u4e3a<code>1e-9<\/code>\uff0c\u800c<code>abs_tol<\/code>\u4e3a<code>0.0<\/code>\u3002\u51fd\u6570\u8fd4\u56de<code>True<\/code>\u8868\u793a\u4e24\u4e2a\u6570\u5728\u6307\u5b9a\u7684\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u76f8\u5bf9\u516c\u5dee\u4e0e\u7edd\u5bf9\u516c\u5dee<\/h4>\n<\/p>\n<p><p>\u201cmath.isclose()\u201d\u51fd\u6570\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u76f8\u5bf9\u516c\u5dee\uff08<code>rel_tol<\/code>\uff09\u548c\u7edd\u5bf9\u516c\u5dee\uff08<code>abs_tol<\/code>\uff09\u3002\u76f8\u5bf9\u516c\u5dee\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u503c\u4e4b\u95f4\u7684\u76f8\u5bf9\u53d8\u5316\uff0c\u800c\u7edd\u5bf9\u516c\u5dee\u5219\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u503c\u4e4b\u95f4\u7684\u7edd\u5bf9\u5dee\u5f02\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u76f8\u5bf9\u516c\u5dee<\/strong>: \u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u6570\u503c\u5728\u5176\u6570\u91cf\u7ea7\u4e0a\u7684\u76f8\u4f3c\u5ea6\uff0c\u9002\u5408\u7528\u4e8e\u6570\u503c\u8f83\u5927\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7edd\u5bf9\u516c\u5dee<\/strong>: \u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u6570\u503c\u7684\u7edd\u5bf9\u5dee\u5f02\uff0c\u9002\u5408\u7528\u4e8e\u6570\u503c\u8f83\u5c0f\u6216\u8005\u63a5\u8fd1\u96f6\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574<code>rel_tol<\/code>\u548c<code>abs_tol<\/code>\u7684\u503c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u7cbe\u5ea6\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u624b\u52a8\u8bbe\u7f6e\u516c\u5dee\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u5458\u53ef\u80fd\u5e0c\u671b\u624b\u52a8\u8bbe\u7f6e\u4e00\u4e2a\u516c\u5dee\u8303\u56f4\u6765\u5224\u65ad\u4e24\u4e2a\u6570\u503c\u662f\u5426\u7ea6\u7b49\u4e8e\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6ca1\u6709<code>math.isclose()<\/code>\u51fd\u6570\u65b9\u4fbf\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u601d\u60f3<\/h4>\n<\/p>\n<p><p>\u57fa\u672c\u601d\u60f3\u662f\u8ba1\u7b97\u4e24\u4e2a\u6570\u503c\u7684\u7edd\u5bf9\u5dee\u5f02\uff0c\u5e76\u5c06\u5176\u4e0e\u4e00\u4e2a\u9884\u5148\u8bbe\u5b9a\u7684\u516c\u5dee\u8303\u56f4\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u7edd\u5bf9\u5dee\u5f02\u5c0f\u4e8e\u516c\u5dee\u8303\u56f4\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u4e24\u4e2a\u6570\u503c\u7ea6\u7b49\u4e8e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_approximately_equal(a, b, tolerance):<\/p>\n<p>    return abs(a - b) &lt;= tolerance<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9009\u62e9\u5408\u9002\u7684\u516c\u5dee<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u516c\u5dee\u662f\u624b\u52a8\u8bbe\u7f6e\u516c\u5dee\u8303\u56f4\u7684\u5173\u952e\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u516c\u5dee\u7684\u9009\u62e9\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u7cbe\u5ea6\u8981\u6c42\u6765\u51b3\u5b9a\u3002\u5728\u91d1\u878d\u8ba1\u7b97\u4e2d\uff0c\u516c\u5dee\u53ef\u80fd\u9700\u8981\u975e\u5e38\u5c0f\uff0c\u800c\u5728\u7269\u7406\u6a21\u62df\u4e2d\uff0c\u53ef\u80fd\u5141\u8bb8\u66f4\u5927\u7684\u8bef\u5dee\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u7684allclose()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\u4e2d\u7684<code>math.isclose()<\/code>\u51fd\u6570\uff0cNumpy\u5e93\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>numpy.allclose()<\/code>\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u5728\u67d0\u4e2a\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u6279\u91cf\u6570\u503c\u6bd4\u8f83\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u201cnumpy.allclose()\u201d\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>result = np.allclose(array1, array2, rtol=1e-05, atol=1e-08)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>array1<\/code>\u548c<code>array2<\/code>\u662f\u9700\u8981\u6bd4\u8f83\u7684\u4e24\u4e2a\u6570\u7ec4\uff0c<code>rtol<\/code>\u662f\u76f8\u5bf9\u516c\u5dee\uff0c<code>atol<\/code>\u662f\u7edd\u5bf9\u516c\u5dee\u3002\u51fd\u6570\u8fd4\u56de<code>True<\/code>\u8868\u793a\u4e24\u4e2a\u6570\u7ec4\u5728\u6307\u5b9a\u7684\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>Numpy\u7684<code>allclose()<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u7b49\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u6bd4\u8f83\u4e24\u4e2a\u77e9\u9635\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5728\u67d0\u4e2a\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6d6e\u70b9\u6570\u6bd4\u8f83\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u6d6e\u70b9\u6570\u6bd4\u8f83\u4e2d\uff0c\u76f4\u63a5\u4f7f\u7528\u7b49\u53f7\uff08<code>==<\/code>\uff09\u8fdb\u884c\u6bd4\u8f83\u901a\u5e38\u662f\u4e0d\u53ef\u9760\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u8fd1\u4f3c\u8868\u793a\u7684\uff0c\u53ef\u80fd\u5b58\u5728\u7cbe\u5ea6\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u6bd4\u8f83\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u8fdb\u884c\u8fd1\u4f3c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u907f\u514d\u76f4\u63a5\u4f7f\u7528\u7b49\u53f7<\/h4>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7684\u5b58\u50a8\u673a\u5236\u51b3\u5b9a\u4e86\u5176\u5728\u8ba1\u7b97\u4e2d\u53ef\u80fd\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u3002\u4f8b\u5982\uff0c0.1\u5728\u8ba1\u7b97\u673a\u4e2d\u4e0d\u80fd\u88ab\u7cbe\u786e\u8868\u793a\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bef\u5dee\u7d2f\u79ef\u3002\u76f4\u63a5\u4f7f\u7528\u7b49\u53f7\u6bd4\u8f83\u6d6e\u70b9\u6570\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9009\u62e9\u5408\u9002\u7684\u7cbe\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u7cbe\u5ea6\u662f\u5173\u952e\u3002\u592a\u5927\u7684\u516c\u5dee\u53ef\u80fd\u5bfc\u81f4\u6bd4\u8f83\u5931\u53bb\u610f\u4e49\uff0c\u800c\u592a\u5c0f\u7684\u516c\u5dee\u5219\u53ef\u80fd\u5bfc\u81f4\u8bef\u5224\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u503c\u7684\u6570\u91cf\u7ea7\u6765\u5408\u7406\u8bbe\u7f6e\u516c\u5dee\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6d6e\u70b9\u6570\u7684\u7ea6\u7b49\u4e8e\u6bd4\u8f83\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5178\u578b\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u8fdb\u884c\u6d6e\u70b9\u6570\u7684\u8fd1\u4f3c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u6570\u503c\u8bef\u5dee\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u4e3a\u4e86\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u8ba1\u7b97\u7ed3\u679c\u8fdb\u884c\u8fd1\u4f3c\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c\u5728\u6570\u503c\u79ef\u5206\u4e2d\uff0c\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u8ba1\u7b97\u7ed3\u679c\u7684\u76f8\u4f3c\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.isclose()<\/code>\u6216<code>numpy.allclose()<\/code>\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u91d1\u878d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u8ba1\u7b97\u4e2d\uff0c\u7cbe\u5ea6\u8981\u6c42\u901a\u5e38\u975e\u5e38\u9ad8\u3002\u5728\u8fdb\u884c\u8d27\u5e01\u8ba1\u7b97\u65f6\uff0c\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u8ba1\u7b97\u7ed3\u679c\u8fdb\u884c\u8fd1\u4f3c\u6bd4\u8f83\u4ee5\u786e\u4fdd\u7ed3\u679c\u7684\u5408\u7406\u6027\u3002\u4f8b\u5982\uff0c\u5728\u5229\u606f\u8ba1\u7b97\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u8ba1\u7b97\u65b9\u6cd5\u7684\u7ed3\u679c\u662f\u5426\u5728\u4e00\u4e2a\u5c0f\u7684\u516c\u5dee\u8303\u56f4\u5185\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7269\u7406\u6a21\u62df<\/h4>\n<\/p>\n<p><p>\u7269\u7406\u6a21\u62df\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6a21\u62df\u7ed3\u679c\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u6a21\u62df\u7684\u51c6\u786e\u6027\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.allclose()<\/code>\u51fd\u6570\u5bf9\u6a21\u62df\u7ed3\u679c\u4e0e\u7406\u8bba\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ee5\u5224\u65ad\u6a21\u62df\u7684\u51c6\u786e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u6d41\u4f53\u52a8\u529b\u5b66\u6a21\u62df\u4e2d\uff0c\u6bd4\u8f83\u901f\u5ea6\u573a\u7684\u6570\u503c\u89e3\u4e0e\u89e3\u6790\u89e3\u7684\u76f8\u4f3c\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7684\u6bd4\u8f83\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f\u7528Python\u63d0\u4f9b\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u6d6e\u70b9\u6570\u6bd4\u8f83\u4e2d\u7684\u7cbe\u5ea6\u95ee\u9898\u3002<strong>\u201cmath.isclose()\u201d\u51fd\u6570\u3001\u624b\u52a8\u8bbe\u7f6e\u516c\u5dee\u8303\u56f4\u3001Numpy\u7684\u201callclose()\u201d\u51fd\u6570<\/strong>\u90fd\u662f\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u65f6\uff0c\u5e94\u8003\u8651\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u503c\u7684\u6570\u91cf\u7ea7\uff0c\u4ee5\u786e\u4fdd\u6bd4\u8f83\u7684\u51c6\u786e\u6027\u548c\u5408\u7406\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u4f7f\u7528\u7b26\u53f7\u8868\u793a\u7ea6\u7b49\u4e8e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ea6\u7b49\u4e8e\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u6570\u7684\u76f8\u5bf9\u548c\u7edd\u5bf9\u8bef\u5dee\uff0c\u5224\u65ad\u5b83\u4eec\u662f\u5426\u201c\u8db3\u591f\u63a5\u8fd1\u201d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\n\na = 0.1 + 0.2\nb = 0.3\n\nif math.isclose(a, b, rel_tol=1e-9):\n    print(&quot;a\u7ea6\u7b49\u4e8eb&quot;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>rel_tol<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u6307\u5b9a\u76f8\u5bf9\u5bb9\u5fcd\u5ea6\uff0c\u4ee5\u4fbf\u6bd4\u8f83\u6d6e\u70b9\u6570\u7684\u7cbe\u786e\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u7ea6\u7b49\u4e8e\u7684\u6bd4\u8f83\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u7ea6\u7b49\u4e8e\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u4e24\u4e2a\u6570\u5b57\u548c\u4e00\u4e2a\u5bb9\u5fcd\u5ea6\u503c\u3002\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6bd4\u8f83\u7684\u7cbe\u5ea6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def approximately_equal(x, y, tolerance=1e-9):\n    return abs(x - y) &lt; tolerance\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u5224\u65ad\u4e24\u4e2a\u6570\u662f\u5426\u5728\u6307\u5b9a\u7684\u5bb9\u5fcd\u5ea6\u5185\u63a5\u8fd1\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5224\u65ad\u6570\u503c\u662f\u5426\u7ea6\u7b49\u4e8e\uff1f<\/strong><br \/>\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u7cbe\u786e\u5ea6\u95ee\u9898\u5c24\u5176\u91cd\u8981\u3002\u7528\u6237\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\uff0c\u5e76\u7ed3\u5408<code>numpy.isclose()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u6570\u7ec4\u4e2d\u5143\u7d20\u662f\u5426\u7ea6\u7b49\u4e8e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narr1 = np.array([0.1, 0.2, 0.3])\narr2 = np.array([0.1, 0.2, 0.3])\n\nresult = np.isclose(arr1, arr2, atol=1e-9)\nprint(result)  # \u8f93\u51fa\u5e03\u5c14\u6570\u7ec4\uff0c\u6307\u793a\u54ea\u4e9b\u5143\u7d20\u662f\u7ea6\u7b49\u4e8e\u7684\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u80fd\u591f\u5feb\u901f\u8fd4\u56de\u6bcf\u5bf9\u5143\u7d20\u7684\u76f8\u4f3c\u6027\u5224\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ea6\u7b49\u4e8e\u901a\u5e38\u7528\u6765\u8868\u793a\u4e24\u4e2a\u6570\u503c\u4e4b\u95f4\u7684\u5dee\u5f02\u5728\u4e00\u4e2a\u53ef\u63a5\u53d7\u7684\u8303\u56f4\u5185\u3002Python\u4e2d\u8868\u8fbe\u7ea6\u7b49\u4e8e\u7684\u65b9\u6cd5\u5305\u62ec [&hellip;]","protected":false},"author":3,"featured_media":1022886,"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\/1022878"}],"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=1022878"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022878\/revisions"}],"predecessor-version":[{"id":1022887,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022878\/revisions\/1022887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1022886"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1022878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1022878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1022878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}