numpy.testing.assert_array_almost_equal_nulp#
- testing.assert_array_almost_equal_nulp(x, y, nulp=1)[源代码]#
根据其间距比较两个数组。
这是一种相对稳健的方法,用于比较幅值可变的两个数组。
- 参数:
- x, y类数组
输入数组。
- nulp整型, 可选
容差的最后一位单位(ULP)的最大数量(参见备注)。默认值为 1。
- 返回:
- None
- 引发:
- AssertionError
如果一个或多个元素中 x 和 y 之间的间距大于 nulp。
另请参见
assert_array_max_ulp
检查数组所有项的差异至多为 N 个最后一位的单位(ULP)。
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)