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)
| 510 | |
| 511 | |
| 512 | def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True): |
| 513 | """ |
| 514 | Raises an AssertionError if two items are not equal up to desired |
| 515 | precision. |
| 516 | |
| 517 | .. note:: It is recommended to use one of `assert_allclose`, |
| 518 | `assert_array_almost_equal_nulp` or `assert_array_max_ulp` |
| 519 | instead of this function for more consistent floating point |
| 520 | comparisons. |
| 521 | |
| 522 | The test verifies that the elements of `actual` and `desired` satisfy:: |
| 523 | |
| 524 | abs(desired-actual) < float64(1.5 * 10**(-decimal)) |
| 525 | |
| 526 | That is a looser test than originally documented, but agrees with what the |
| 527 | actual implementation in `assert_array_almost_equal` did up to rounding |
| 528 | vagaries. An exception is raised at conflicting values. For ndarrays this |
| 529 | delegates to assert_array_almost_equal |
| 530 | |
| 531 | Parameters |
| 532 | ---------- |
| 533 | actual : array_like |
| 534 | The object to check. |
| 535 | desired : array_like |
| 536 | The expected object. |
| 537 | decimal : int, optional |
| 538 | Desired precision, default is 7. |
| 539 | err_msg : str, optional |
| 540 | The error message to be printed in case of failure. |
| 541 | verbose : bool, optional |
| 542 | If True, the conflicting values are appended to the error message. |
| 543 | |
| 544 | Raises |
| 545 | ------ |
| 546 | AssertionError |
| 547 | If actual and desired are not equal up to specified precision. |
| 548 | |
| 549 | See Also |
| 550 | -------- |
| 551 | assert_allclose: Compare two array_like objects for equality with desired |
| 552 | relative and/or absolute precision. |
| 553 | assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal |
| 554 | |
| 555 | Examples |
| 556 | -------- |
| 557 | >>> from numpy.testing import assert_almost_equal |
| 558 | >>> assert_almost_equal(2.3333333333333, 2.33333334) |
| 559 | >>> assert_almost_equal(2.3333333333333, 2.33333334, decimal=10) |
| 560 | Traceback (most recent call last): |
| 561 | ... |
| 562 | AssertionError: |
| 563 | Arrays are not almost equal to 10 decimals |
| 564 | ACTUAL: 2.3333333333333 |
| 565 | DESIRED: 2.33333334 |
| 566 | |
| 567 | >>> assert_almost_equal(np.array([1.0,2.3333333333333]), |
| 568 | ... np.array([1.0,2.33333334]), decimal=9) |
| 569 | Traceback (most recent call last): |
searching dependent graphs…