MCPcopy Create free account
hub / github.com/numpy/numpy / assert_almost_equal

Function assert_almost_equal

numpy/testing/_private/utils.py:414–537  ·  view source on GitHub ↗

Raises an AssertionError if two items are not equal up to desired precision. .. note:: It is recommended to use one of `assert_allclose`, `assert_array_almost_equal_nulp` or `assert_array_max_ulp` instead of this function for more consistent floating point

(actual, desired, decimal=7, err_msg='', verbose=True)

Source from the content-addressed store, hash-verified

412
413@np._no_nep50_warning()
414def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True):
415 """
416 Raises an AssertionError if two items are not equal up to desired
417 precision.
418
419 .. note:: It is recommended to use one of `assert_allclose`,
420 `assert_array_almost_equal_nulp` or `assert_array_max_ulp`
421 instead of this function for more consistent floating point
422 comparisons.
423
424 The test verifies that the elements of `actual` and `desired` satisfy.
425
426 ``abs(desired-actual) < float64(1.5 * 10**(-decimal))``
427
428 That is a looser test than originally documented, but agrees with what the
429 actual implementation in `assert_array_almost_equal` did up to rounding
430 vagaries. An exception is raised at conflicting values. For ndarrays this
431 delegates to assert_array_almost_equal
432
433 Parameters
434 ----------
435 actual : array_like
436 The object to check.
437 desired : array_like
438 The expected object.
439 decimal : int, optional
440 Desired precision, default is 7.
441 err_msg : str, optional
442 The error message to be printed in case of failure.
443 verbose : bool, optional
444 If True, the conflicting values are appended to the error message.
445
446 Raises
447 ------
448 AssertionError
449 If actual and desired are not equal up to specified precision.
450
451 See Also
452 --------
453 assert_allclose: Compare two array_like objects for equality with desired
454 relative and/or absolute precision.
455 assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal
456
457 Examples
458 --------
459 >>> from numpy.testing import assert_almost_equal
460 >>> assert_almost_equal(2.3333333333333, 2.33333334)
461 >>> assert_almost_equal(2.3333333333333, 2.33333334, decimal=10)
462 Traceback (most recent call last):
463 ...
464 AssertionError:
465 Arrays are not almost equal to 10 decimals
466 ACTUAL: 2.3333333333333
467 DESIRED: 2.33333334
468
469 >>> assert_almost_equal(np.array([1.0,2.3333333333333]),
470 ... np.array([1.0,2.33333334]), decimal=9)
471 Traceback (most recent call last):

Callers 15

test_trapz_matrixFunction · 0.90
test_pinvMethod · 0.90
test_reduceMethod · 0.90
test_roundMethod · 0.90
test_floatingMethod · 0.90
test_simpleMethod · 0.90
test_special_valuesMethod · 0.90
test_simpleMethod · 0.90
test_scalarMethod · 0.90
test_arrayMethod · 0.90
test_simpleMethod · 0.90

Calls 8

iscomplexobjFunction · 0.90
realFunction · 0.90
imagFunction · 0.90
isfiniteFunction · 0.90
_build_err_msgFunction · 0.85
isnanFunction · 0.85
absFunction · 0.85

Tested by 15

test_trapz_matrixFunction · 0.72
test_pinvMethod · 0.72
test_reduceMethod · 0.72
test_roundMethod · 0.72
test_floatingMethod · 0.72
test_simpleMethod · 0.72
test_special_valuesMethod · 0.72
test_simpleMethod · 0.72
test_scalarMethod · 0.72
test_arrayMethod · 0.72
test_simpleMethod · 0.72