Skip to content
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

assert_allclose reports NaN as max relative difference if arrays have zeros at the same positions #13801

Closed
CakeWithSteak opened this issue Jun 18, 2019 · 0 comments · Fixed by #13802

Comments

@CakeWithSteak
Copy link
Contributor

If two different arrays share a zero at the same position the max relative difference will be reported as NaN by assert_allclose. This is because in assert_array_compare the error is simply calculated as max_rel_error = (error / abs(y)).max() which can result in 0/0 = NaN.
This hides the actual biggest relative difference and makes it more difficult to solve the problem in your code that is triggering the assertion.

Reproducing code example:

import numpy as np
x = np.array([0, 1])
y = np.array([0, 2])
np.testing.assert_allclose(x, y, rtol=0.001)

Output:

AssertionError: 
Not equal to tolerance rtol=0.001, atol=0

Mismatch: 50%
Max absolute difference: 1
Max relative difference: nan
 x: array([0, 1])
 y: array([0, 2])

Expected output:

AssertionError: 
Not equal to tolerance rtol=0.001, atol=0

Mismatch: 50%
Max absolute difference: 1
Max relative difference: 0.5
 x: array([0, 1])
 y: array([0, 2])

Numpy/Python version information:

1.16.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants