Skip to content

ENH: Make signed/unsigned integer comparisons exact#23713

Merged
charris merged 2 commits intonumpy:mainfrom
seberg:uint-int-comparisons
May 7, 2023
Merged

ENH: Make signed/unsigned integer comparisons exact#23713
charris merged 2 commits intonumpy:mainfrom
seberg:uint-int-comparisons

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented May 4, 2023

This makes comparisons between signed and unsigned integers exact by special-casing promotion in comparison to never promote integers to floats, but rather promote them to uint64 or int64 and use a specific loop for that purpose.

This is a bit lazy, it doesn't make the scalar paths fast (they never were though) nor does it try to vectorize the loop.
Thus, for cases that are not int64/uint64 already and require a cast in either case, it should be a bit slower. OTOH, it was never really fast and the int64/uint64 mix is probably faster since it avoids casting.


Now... the reason I was looking into this was, that I had hoped it would help with NEP 50/weak scalar typing to allow:

uint64(1) < -1  # annoying that it fails with NEP 50

but, it doesn't actually, because if I use int64 for the -1 then very large numbers would be a problem...
I could probably(?) add a specific "Python integer" ArrayMethod for comparisons and that could pick object dtype and thus get the original Python object (the loop could then in practice assume a scalar value).


In either case, this works, and unless we worry about keeping the behavior we probably might as well do this.
(Potentially with follow-ups to speed it up.)

This makes comparisons between signed and unsigned integers exact
by special-casing promotion in comparison to never promote integers
to floats, but rather promote them to uint64 or int64 and use a
specific loop for that purpose.

This is a bit lazy, it doesn't make the scalar paths fast (they never were
though) nor does it try to vectorize the loop.
Thus, for cases that are not int64/uint64 already and require a cast in
either case, it should be a bit slower.  OTOH, it was never really fast
and the int64/uint64 mix is probably faster since it avoids casting.

---

Now... the reason I was looking into this was, that I had hoped
it would help with NEP 50/weak scalar typing to allow:

    uint64(1) < -1  # annoying that it fails with NEP 50

but, it doesn't actually, because if I use int64 for the -1 then very
large numbers would be a problem...
I could probably(?) add a *specific* "Python integer" ArrayMethod for comparisons
and that could pick `object` dtype and thus get the original Python object
(the loop could then in practice assume a scalar value).

---

In either case, this works, and unless we worry about keeping the behavior
we probably might as well do this.
(Potentially with follow-ups to speed it up.)
@charris charris merged commit 276cc99 into numpy:main May 7, 2023
@charris
Copy link
Copy Markdown
Member

charris commented May 7, 2023

Thanks Sebastian. There were a couple of long lines that could be fixed, but maybe we should make a sweep for such things before the 2.0 release.

@seberg seberg deleted the uint-int-comparisons branch May 7, 2023 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants