testing.assert_close_nulp¶
- array_api_extra.testing.assert_close_nulp(actual, desired, *, nulp=1, check_dtype=True, check_shape=True, check_scalar=False, xp=None)¶
Compare two arrays relatively to their spacing.
This is an interface to
numpy.testing.assert_array_almost_equal_nulp()which accepts any standard-compatible array and performs additional array namespace, shape, and dtype checks.- Parameters:
actual (
object) – The array produced by the tested function.desired (
object) – The expected array (typically hardcoded).nulp (
int) – The maximum number of units in the last place for the tolerance check. Default:1.check_dtype (
bool) – Whether to check agreement between actual and desired dtypes.check_shape (
bool) – Whether to check agreement between actual and desired shapes.check_scalar (
bool) – NumPy only: whether to check agreement between actual and desired types — 0-Dnumpy.ndarrayvs scalar (e.g.numpy.double).xp (
ModuleType|None) – A standard-compatible namespace which actual and desired must match.
- Raises:
AssertionError – If the spacing between actual and desired for one or more elements is larger than nulp.
ImportError – If
numpyis not importable in the Python environment.
See also
assert_closeSimilar function for inexact equality checks.
numpy.spacingSpacing calculation for NumPy arrays.
numpy.testing.assert_array_almost_equal_nulpSimilar function for NumPy arrays.
- Return type:
Notes
This is a relatively robust method to compare two arrays whose amplitude is variable.
An assertion is raised if the following condition is not met:
abs(actual - desired) <= nulp * spacing(maximum(abs(actual), abs(desired)))
where
spacing(x)is the distance betweenxand the nearest adjacent number representable by in the data type ofx.