Skip to content

BUG: incorrect equality of np.int64 and np.uint64 #12525

Closed
@h-vetinari

Description

@h-vetinari

EDIT: since this issue has become somewhat of a stand-in for uint64-problems, here's a summary pulled up from the discussion further down:

PS. Here's all the duckies in one neat row:


There's a range of values of class np.uint64 that fail a trivial comparison test: v > v - 1

The issue is IMO that it effectively uses float-logic of some kind in the background, which is obviously neither desirable nor necessary.

Reproducing code example:

The following comparisons should all return True:

>>> import numpy as np
>>> for v in np.logspace(50, 63.9, num=20, base=2):
...     v = int(v)
...     c = np.array([v], dtype='uint64').max() > v - 1
...     print(c, v)
...
True 1125899906842624
True 1869506547705232
True 3104232188555715
True 5154439010814696
True 8558715940823230
False 14211365854171536
False 23597338764077220
False 39182327895891032
False 65060506809279480
False 108030067981851424
False 179379106627232000
False 297851001073046464
False 494568294537084288
False 821208581069482368
False 1363580199279397888
False 2264164065900722176
False 3759543384412024832
False 6242554005755503616
True 10365482328612339712
True 17211420807207079936

The way I encountered this, is that 'uint64' is the default dtype for values between np.iinfo('int64').max + 1 and np.iinfo('uint64').max (which makes sense), but leads to the following problem:

>>> v = np.iinfo('int64').max
>>> np.array([v + 1]) > v
array([ True])
>>> np.array([v + 1]).max() > v
False
>>> np.array([v + 1000]).max() == v
True

Numpy/Python version information:

Numpy version: 1.15.4
Python version: 3.6.6 (conda-forge)

Metadata

Metadata

Assignees

No one assigned

    Labels

    01 - Enhancement23 - Wish List60 - Major releaseIssues that need or may be better addressed in a major release62 - Python APIChanges or additions to the Python API. Mailing list should usually be notified.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions