Skip to content

Invalid integer comparison #11801

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
pitrou opened this issue Aug 22, 2018 · 8 comments
Closed

Invalid integer comparison #11801

pitrou opened this issue Aug 22, 2018 · 8 comments

Comments

@pitrou
Copy link
Member

pitrou commented Aug 22, 2018

With Python 3.7.0 and Numpy 1.15.0 (Anaconda version):

>>> v = np.int64(np.iinfo(np.int64).max)
>>> v
9223372036854775807
>>> type(v)
numpy.int64
>>> v < 2**63
False
>>> int(v) < 2**63
True
@eric-wieser
Copy link
Member

2**63 is converted to a numpy uint64. The comparison int64 < uint64 is undefined, so it promotes to float64 < float64, and the precision is reduced, making them equal.

@eric-wieser
Copy link
Member

Perhaps we ought to deprecate the promotion from integers to floats, and just switch to object arrays instead.

@pitrou
Copy link
Member Author

pitrou commented Aug 22, 2018

Comparison int64 with uint64 sounds at least semantically well-defined to me.

@eric-wieser
Copy link
Member

eric-wieser commented Aug 22, 2018

Sure, in this case I simply meant "we don't define it (yet)". uint64 + int64 is a similar case that is actually ill-defined

@pitrou
Copy link
Member Author

pitrou commented Aug 22, 2018

Well, addition is ambiguous in case of overflow, but comparison is not ;-)

@matthew-brett
Copy link
Contributor

My guess is that numpy is just looking at the types on either side of the operator, and finding a common type to promote them to. Numpy could have been smarter, noticed that this was a comparison operator, and not done that, but, hey, none of us are smart all of the time.

@eric-wieser
Copy link
Member

addition is ambiguous in case of overflow

Worse than that, the type of uint + int is ambiguous in all cases.

You're correct that comparison can be perfectly well-defined, but the C compiler won't define it for us sadly.

@seberg
Copy link
Member

seberg commented May 12, 2022

Closing this one in favor of the related gh-12525. Keeping that open since comparisons are maybe a special case compared to the general promotion problem in gh-20905.

@seberg seberg closed this as completed May 12, 2022
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

5 participants