8000 numpy.allclose does not deal well with the minimal value of int* dtypes (Trac #1684) · Issue #2280 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

numpy.allclose does not deal well with the minimal value of int* dtypes (Trac #1684) #2280

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
numpy-gitbot opened this issue Oct 19, 2012 · 2 comments

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1684 on 2010-11-22 by trac user lamblin, assigned to unknown.

If I create an array containing the minimal value of an int dtype, for instance an 'int8' array containing -128, numpy.allclose fails to detect it is "close" to itself, although it is equal.

>>> a = numpy.asarray(-128, dtype='int8')
>>> numpy.all(a == a)
True
>>> numpy.allclose(a, a)
False

I would expect:

>>> numpy.allclose(a, a)
True

This is due to the fact that the tolerance uses numpy.absolute, and in that case, it is negative:

>>> numpy.absolute(a)
array([-128], dtype=int8)

I reproduced the same with int16 and -215, int32 and -231, and int64 and -2**63.

@numpy-gitbot
Copy link
Author

trac user lamblin wrote on 2010-11-22

A workaround is to upcast the second argument to float64 if its absolute value has a negative element:

def _allclose(a, b, rtol=1e-5, atol=1e-8):
    if str(b.dtype).startswith('int') and (numpy.absolute(b) < 0).any(): 
        b = numpy.asarray(b, dtype='float64') 

    return numpy.allclose(a, b, rtol=rtol, atol=atol) 

I don't know if this should be fixed at the level of allclose, or of absolute.

@charris
Copy link
Member
charris commented Feb 15, 2014

Fixed by merging #4105. Integers are now cast to floats.

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

No branches or pull requests

2 participants
0