-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
np.left_shift and np.right_shift fail on np.uint64 scalar types (Trac #1931) #2524
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
Comments
@charris wrote on 2011-08-13 This is because the shift number is converted as a signed type and there is no signed integer type big enough to hold a uint64. The shift operators need to be special cased, they can also fail on Intel because the hardware masks out the unneeded bits, i.e., for int32 only the lower 5 bits are looked at. |
trac user parejkoj wrote on 2012-07-27 I was able to code around this with the following lambda:
This works, but is not ideal. Getting this fixed in numpy would be very useful. |
it's five years later... isn't this an important issue? unsigned integers should have no ambiguity about shifts. |
The easier work-around is to use an unsigned integer for the shift:
As @charris mentioned above, the problem is in the way dtypes are made uniform. It is not that trivial to fix generically (see also #5668), though perhaps the shift operators can be special-cased. (PR welcome, though, as said, this is not trivial, but requires fair understanding of how the internal loops in ufucs are decided on...). |
...which does work if you know you are operating on numpy integers, but if you are using generic code (esp. something that has already been written in another module and is out of your control) like
then you can't make it work on both regular Python integers and np.uint64 types. I'm not trying to state that this is trivial (wayyyy above my skillset) but I do think it should be a high priority. |
@jason-s - I agree that the problem is very annoying... Sadly, there not that many of us well-enough versed in how the ufuncs work internally... Perhaps one could at least special-case scalars for the shift in the method, though, instead of relying on the general ufunc machinery. @charris: was this what you had in mind (if you can remember that after 6 years ;-). |
See also #8002. Personally, if we could do everything over again, I would favor redoing all the casting/coercion so that numpy casting essentially behaves like C casting, and follows the spirit of C-casting for cases that don't exist in C. The numpy casting rules are kind of weird and sometimes confusing (eg the conversion of When I come across situations like this (see my comment there) I just wrap every single value in |
…mestamp is packed into unsigned 64bit int. To retreive the timestamp in python client the following snippet can be used: ##Note that the timestamp needs to be received as two unsigned 32 bit integers ## instead of a single 64bit int. This due to the fact the rshift on 64bit int doesn't ## work on numpy [numpy/numpy#2524] raw_data=socket.recv() value = numpy.fromstring(raw_data, dtype='u4') secPastEpich = value[1] nsec = value[0]
Closing as duplicate of gh-22624 |
Original ticket http://projects.scipy.org/numpy/ticket/1931 on 2011-08-10 by trac user tlatorre, assigned to unknown.
The text was updated successfully, but these errors were encountered: