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)
| 324 | |
| 325 | |
| 326 | def 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): |
searching dependent graphs…