NumPy参考 >例行程序 >Test Support (numpy.testing) > numpy.testing.assert_approx_equal
numpy.testing.
assert_approx_equal
(实际,期望,有意义= 7,err_msg ='',详细= True )[源代码] ¶如果两个项目不等于有效位数,则引发AssertionError。
注意
对于更一致的浮点比较assert_allclose
,
建议使用assert_array_almost_equal_nulp
或之一assert_array_max_ulp
代替此函数。
给定两个数字,请检查它们是否近似相等。近似等于定义为一致的有效位数。
要检查的对象。
预期的对象。
所需精度,默认为7。
发生故障时要打印的错误消息。
如果为True,则冲突值将附加到错误消息中。
如果实际和期望值不等于指定的精度。
也可以看看
assert_allclose
比较两个array_like对象是否相等,并具有所需的相对和/或绝对精度。
assert_array_almost_equal_nulp
,assert_array_max_ulp
,assert_equal
例子
>>> np.testing.assert_approx_equal(0.12345677777777e-20, 0.1234567e-20)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345671e-20,
... significant=8)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345672e-20,
... significant=8)
Traceback (most recent call last):
...
AssertionError:
Items are not equal to 8 significant digits:
ACTUAL: 1.234567e-21
DESIRED: 1.2345672e-21
引发异常的评估条件是
>>> abs(0.12345670e-20/1e-21 - 0.12345672e-20/1e-21) >= 10**-(8-1)
True