should abs gives negative at end of integer range?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

should abs gives negative at end of integer range?

Jon Wright
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?

-Jon

_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: should abs gives negative at end of integer range?

Charles R Harris


On Sun, May 25, 2008 at 11:38 AM, Jonathan Wright <[hidden email]> 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



_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion