MCPcopy
hub / github.com/pytorch/pytorch / assert_almost_equal

Function assert_almost_equal

torch/_numpy/testing/utils.py:326–448  ·  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

324
325
326def assert_almost_equal(actual, desired, decimal=7, err_msg="", verbose=True):
327 """
328 Raises an AssertionError if two items are not equal up to desired
329 precision.
330
331 .. note:: It is recommended to use one of `assert_allclose`,
332 `assert_array_almost_equal_nulp` or `assert_array_max_ulp`
333 instead of this function for more consistent floating point
334 comparisons.
335
336 The test verifies that the elements of `actual` and `desired` satisfy.
337
338 ``abs(desired-actual) < float64(1.5 * 10**(-decimal))``
339
340 That is a looser test than originally documented, but agrees with what the
341 actual implementation in `assert_array_almost_equal` did up to rounding
342 vagaries. An exception is raised at conflicting values. For ndarrays this
343 delegates to assert_array_almost_equal
344
345 Parameters
346 ----------
347 actual : array_like
348 The object to check.
349 desired : array_like
350 The expected object.
351 decimal : int, optional
352 Desired precision, default is 7.
353 err_msg : str, optional
354 The error message to be printed in case of failure.
355 verbose : bool, optional
356 If True, the conflicting values are appended to the error message.
357
358 Raises
359 ------
360 AssertionError
361 If actual and desired are not equal up to specified precision.
362
363 See Also
364 --------
365 assert_allclose: Compare two array_like objects for equality with desired
366 relative and/or absolute precision.
367 assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal
368
369 Examples
370 --------
371 >>> from torch._numpy.testing import assert_almost_equal
372 >>> assert_almost_equal(2.3333333333333, 2.33333334)
373 >>> assert_almost_equal(2.3333333333333, 2.33333334, decimal=10)
374 Traceback (most recent call last):
375 ...
376 AssertionError:
377 Arrays are not almost equal to 10 decimals
378 ACTUAL: 2.3333333333333
379 DESIRED: 2.33333334
380
381 >>> assert_almost_equal(np.array([1.0,2.3333333333333]),
382 ... np.array([1.0,2.33333334]), decimal=9)
383 Traceback (most recent call last):

Callers 15

test_sum_dtypes_2Method · 0.90
test_sum_initialMethod · 0.90
test_floatingMethod · 0.90
test_blockedMethod · 0.90
test_lower_alignMethod · 0.90
test_large_typesMethod · 0.90
test_mixed_typesMethod · 0.90
optimize_compareMethod · 0.90
test_path_type_inputMethod · 0.90

Calls 9

iscomplexobjFunction · 0.90
realFunction · 0.90
imagFunction · 0.90
_build_err_msgFunction · 0.85
isinstanceFunction · 0.85
gisfiniteFunction · 0.85
gisnanFunction · 0.85
absFunction · 0.50

Tested by 15

test_sum_dtypes_2Method · 0.72
test_sum_initialMethod · 0.72
test_floatingMethod · 0.72
test_blockedMethod · 0.72
test_lower_alignMethod · 0.72
test_large_typesMethod · 0.72
test_mixed_typesMethod · 0.72
optimize_compareMethod · 0.72
test_path_type_inputMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…