擅长:python、mysql、java
<p>从Python 3.5开始,执行此操作的标准方法(使用标准库)是使用<a href="https://docs.python.org/3/library/math.html#math.isclose">^{<cd1>}</a>函数。</p>
<p>它有以下签名:</p>
<pre><code>isclose(a, b, rel_tol=1e-9, abs_tol=0.0)
</code></pre>
<p>使用绝对误差公差的示例:</p>
<pre><code>from math import isclose
a = 1.0
b = 1.00000001
assert isclose(a, b, abs_tol=1e-8)
</code></pre>
<p>如果需要精度为<strong><em>n</em></strong>有效数字,只需将最后一行替换为:</p>
<pre><code>assert isclose(a, b, abs_tol=10**-n)
</code></pre>