numpy.testing.assert_array_almost_equal_nulp#
- testing.assert_array_almost_equal_nulp(x, y, nulp=1)[源代码]#
相对于它们的间距比较两个数组。
这是一个相对稳健的方法,用于比较幅度可变的两个数组。
- 参数:
- x, y类数组
输入数组。
- nulpint, optional
最后一个有效位(unit in the last place)的最大容差值(参见 Notes)。默认为 1。
- 返回:
- None
- 引发:
- AssertionError
如果 x 和 y 之间的一个或多个元素的间距大于 nulp。
另请参阅
assert_array_max_ulp检查数组中的所有项最多相差 N 个最后位单位。
spacing返回 x 与最近的相邻数字之间的距离。
备注
如果以下条件不满足,则会引发断言
abs(x - y) <= nulp * spacing(maximum(abs(x), abs(y)))
示例
>>> x = np.array([1., 1e-10, 1e-20]) >>> eps = np.finfo(x.dtype).eps >>> np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x)
>>> np.testing.assert_array_almost_equal_nulp(x, x*eps + x) Traceback (most recent call last): ... AssertionError: Arrays are not equal to 1 ULP (max is 2)