testing.assert_close¶
- array_api_extra.testing.assert_close(actual, desired, *, rtol=None, atol=0, equal_nan=True, err_msg='', verbose=True, check_dtype=True, check_shape=True, check_scalar=False, xp=None)¶
Check that two arrays are close, up to tolerance
atol + rtol * abs(desired).This is an interface to
numpy.testing.assert_allclose()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).rtol (
float|object|None) – Relative tolerance. Default: dtype-dependent.equal_nan (
bool) – Whether to consider NaNs in corresponding locations as equal.err_msg (
str) – Error message to display on failure.verbose (
bool) – Whether to include the conflicting arrays in the error message on failure.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 actual and desired are not equal up to the defined tolerance.
ImportError – If
numpyis not importable in the Python environment.
See also
assert_equalSimilar function for exact equality checks.
array_api_extra.iscloseSimilar function checking closeness, returning a bool.
numpy.testing.assert_allcloseSimilar function for NumPy arrays.
- Return type:
Notes
The default atol and rtol differ from
xp.all(xpx.isclose(a, b)). For inexact dtypes, the default rtol isxp.finfo(actual.dtype).eps ** 0.5 * 4, which forfloat64is roughly halfway between \(\sqrt{\epsilon}\) and the default fornumpy.testing.assert_allclose(),1e-7. This gives a more reasonable default for lower precision dtypes, for example approximately1e-3forfloat32. For exact dtypes, the default1e-7is used.Array arguments to atol and rtol must be valid input to
float.