MCPcopy Index your code
hub / github.com/pytorch/pytorch / assert_approx_equal

Function assert_approx_equal

torch/_numpy/testing/utils.py:451–548  ·  view source on GitHub ↗

Raises an AssertionError if two items are not equal up to significant digits. .. 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, significant=7, err_msg="", verbose=True)

Source from the content-addressed store, hash-verified

449
450
451def assert_approx_equal(actual, desired, significant=7, err_msg="", verbose=True):
452 """
453 Raises an AssertionError if two items are not equal up to significant
454 digits.
455
456 .. note:: It is recommended to use one of `assert_allclose`,
457 `assert_array_almost_equal_nulp` or `assert_array_max_ulp`
458 instead of this function for more consistent floating point
459 comparisons.
460
461 Given two numbers, check that they are approximately equal.
462 Approximately equal is defined as the number of significant digits
463 that agree.
464
465 Parameters
466 ----------
467 actual : scalar
468 The object to check.
469 desired : scalar
470 The expected object.
471 significant : int, optional
472 Desired precision, default is 7.
473 err_msg : str, optional
474 The error message to be printed in case of failure.
475 verbose : bool, optional
476 If True, the conflicting values are appended to the error message.
477
478 Raises
479 ------
480 AssertionError
481 If actual and desired are not equal up to specified precision.
482
483 See Also
484 --------
485 assert_allclose: Compare two array_like objects for equality with desired
486 relative and/or absolute precision.
487 assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal
488
489 Examples
490 --------
491 >>> np.testing.assert_approx_equal(0.12345677777777e-20, 0.1234567e-20) # doctest: +SKIP
492 >>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345671e-20, # doctest: +SKIP
493 ... significant=8)
494 >>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345672e-20, # doctest: +SKIP
495 ... significant=8)
496 Traceback (most recent call last):
497 ...
498 AssertionError:
499 Items are not equal to 8 significant digits:
500 ACTUAL: 1.234567e-21
501 DESIRED: 1.2345672e-21
502
503 the evaluated condition that raises the exception is
504
505 >>> abs(0.12345670e-20/1e-21 - 0.12345672e-20/1e-21) >= 10**-(8-1)
506 True
507
508 """

Callers

nothing calls this directly

Calls 6

build_err_msgFunction · 0.85
gisfiniteFunction · 0.85
gisnanFunction · 0.85
absMethod · 0.45
floorMethod · 0.45
log10Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…