8000 testing/utils: assert_almost_equal: use the same algorithm for rounding by ignatenkobrain · Pull Request #6601 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

testing/utils: assert_almost_equal: use the same algorithm for rounding #6601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions numpy/testing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ def test_error_message(self):
# remove anything that's not the array string
self.assertEqual(str(e).split('%)\n ')[1], b)

def test_decimal(self):
self._assert_func(0.123, 0.129, decimal=2)
self.assertRaises(AssertionError,
self._assert_func(0.123, 0.129, decimal=3))


class TestApproxEqual(unittest.TestCase):

Expand Down
4 changes: 2 additions & 2 deletions numpy/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True):

"""
__tracebackhide__ = True # Hide traceback for py.test
from numpy.core import ndarray
from numpy.core import around, ndarray
from numpy.lib import iscomplexobj, real, imag

# Handle complex numbers: separate into real/imag to handle
Expand Down Expand Up @@ -509,7 +509,7 @@ def _build_err_msg():
return
except (NotImplementedError, TypeError):
pass
if round(abs(desired - actual), decimal) != 0:
if around(abs(desired - actual), decimal) >= 10.0**(-decimal):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this logic. Either just round, or compare the absolute difference with some value.

raise AssertionError(_build_err_msg())


Expand Down
0