[Numpy-discussion] #2522 numpy.diff fails on unsigned integers

Sebastian Wagner sebix at sebix.at
Tue Nov 4 08:50:48 EST 2014


Hello,

I want to bring up Issue #2522 'numpy.diff fails on unsigned integers 
(Trac #1929)' [1], as it was resonsible for an error in one of our 
programs. Short explanation of the bug: np.diff performs a subtraction 
on the input array. If this is of type uint and the data contains 
falling data, it results in an artihmetic underflow.

>>> np.diff(np.array([0,1,0], dtype=np.uint8))
array([  1, 255], dtype=uint8)

@charris proposed either
- a note to the doc string and maybe an example to clarify things
- or raise a warning
but with a discussion on the list.

I would like to start it now, as it is an error which is not easily 
detectable (no errors or warnings are thrown). In our case the type of a 
data sequence, with only zeros and ones, had type f8 as also every other 
one, has been changed to u4. As the programs looked for values ==1 and 
==-1, it broke silently.
In my opinion, a note in the docs is not enough and does not help if the 
type changed or set after the program has been written.
I'd go for automatic upcasting of uints by default and an option to turn 
it off, if this behavior is explicitly wanted. This wouldn't be correct 
from the point of view of a programmer, but as most of the users have a 
scientific background who excpect it 'to work', instead of sth is 
theoretically correct but not convenient. (I count myself to the first 
group)

When a consens has been achieved I could implement it for a PR.

[1]: https://github.com/numpy/numpy/issues/2522

-- 
gpg --keyserver keys.gnupg.net --recv-key DC9B463B




More information about the NumPy-Discussion mailing list