numpy.testing.assert_approx_equal#

testing.assert_approx_equal(actual, desired, significant=7, err_msg='', verbose=True)[原始碼]#

若兩個項目在有效位數內不相等,則引發 AssertionError。

注意

建議使用 assert_allcloseassert_array_almost_equal_nulpassert_array_max_ulp 其中之一,而非此函數,以獲得更一致的浮點數比較。

給定兩個數字,檢查它們是否近似相等。近似相等定義為有效位數一致的數量。

參數:
actual純量

要檢查的物件。

desired純量

預期的物件。

significantint,選用

期望的精確度,預設值為 7。

err_msgstr,選用

在失敗情況下要印出的錯誤訊息。

verbosebool,選用

若為 True,則衝突的值會附加到錯誤訊息中。

引發:
AssertionError

若 actual 和 desired 在指定的精確度內不相等。

另請參閱

assert_allclose

比較兩個 array_like 物件是否在期望的相對和/或絕對精確度內相等。

assert_array_almost_equal_nulpassert_array_max_ulpassert_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