Skip to content

bitshift typing on unsigned operands is tedious to work with #8002

Closed

Description

@pitrou
>>> np.uint64(18446744073709551615) >> 2
Traceback (most recent call last):
  File "<ipython-input-10-988f6401a88f>", line 1, in <module>
    np.uint64(18446744073709551615) >> 2
TypeError: ufunc 'right_shift' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Activity

  1. pitrou commented on Sep 1, 2016

    @pitrou
    MemberAuthor

    In Numba we are considering typing based only on the first operand, see numba/numba#2072

  2. sklam commented on Sep 1, 2016

    @sklam

    Numpy is probably casting (unsigned, signed) to (float64, float64), which is not defined. Then, it tries to downcast it back to integer, but it is unsafe.

  3. ahaldane commented on Sep 22, 2016

    @ahaldane
    Member

    I've hit the same problem. I have thought it would have been better to copy C typing as closely as possible. Then for an algorithm already written in C I can just paste it into python, and not have to worry about wrapping all the constants in uint64.

    Here's something I had to write last week that demonstrates the annoyance:

        assert(type(crc) is uint16 and type(onech) is uint8)
    
        ans = uint32(crc ^ onech << uint16(8))
        for ind in range(8):
            if ans & uint32(0x8000):
              ans <<= uint32(1)
              ans = ans ^ uint32(4129)
            else:
              ans <<= uint32(1)
  4. seberg commented on Jan 28, 2024

    @seberg
    Member

    Effectively fixed by NEP 50 in main/NumPy 2.0. Closing.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions