[Numpy-discussion] should abs gives negative at end of integer range?
Charles R Harris
Sun May 25 13:19:55 CDT 2008
On Sun, May 25, 2008 at 11:38 AM, Jonathan Wright <email@example.com> wrote:
> This one comes up in a Java puzzler, but applies equally to numpy.
> >>> 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 : x = array([-128], dtype=int8)
In : -x
Out: 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion