Skip to content

Invalid integer comparison #11801

Closed
Closed

Description

@pitrou

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

Activity

  1. eric-wieser commented on Aug 22, 2018

    @eric-wieser
    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.

  2. eric-wieser commented on Aug 22, 2018

    @eric-wieser
    Member

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

  3. pitrou commented on Aug 22, 2018

    @pitrou
    MemberAuthor

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

  4. eric-wieser commented on Aug 22, 2018

    @eric-wieser
    Member

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

  5. pitrou commented on Aug 22, 2018

    @pitrou
    MemberAuthor

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

  6. matthew-brett commented on Aug 22, 2018

    @matthew-brett
    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.

  7. eric-wieser commented on Aug 23, 2018

    @eric-wieser
    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.

  8. seberg commented on May 12, 2022

    @seberg
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions