[Numpy-discussion] should abs gives negative at end of integer range?

Charles R Harris charlesr.harris@gmail....
Sun May 25 13:19:55 CDT 2008


On Sun, May 25, 2008 at 11:38 AM, Jonathan Wright <wright@esrf.fr> wrote:

> This one comes up in a Java puzzler, but applies equally to numpy.
>
> http://www.youtube.com/watch?v=wDN_EYUvUq0
>
>  >>> import numpy, sys
>  >>> abs(numpy.array([-sys.maxint-1],numpy.int)) > 0
> array([False], dtype=bool)
>  >>> abs(numpy.array([-129,-128,-127],numpy.int8)) > 0
> array([ True, False,  True], dtype=bool)
>
> ... etc. Sort of surprising that abs gives something negative. Is this
> the intended behaviour as covered by a unit test and doc already?
>

I don't think it's specified. However

In [1]: x = array([-128], dtype=int8)

In [2]: -x
Out[2]: array([-128], dtype=int8)

Because that's how two's complement operates. In two's complement the
positive and negative values aren't symmetric. OTOH, abs has to return a
positive value. Hmm..., we could return the corresponding unsigned type in
this case, but folks might not like changing the type, either.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080525/7e6ae3e3/attachment.html 


More information about the Numpy-discussion mailing list