[Numpy-discussion] abs for max negative integers - desired behavior?

"V. Armando Solé" sole at esrf.fr
Wed Oct 12 04:18:19 EDT 2011


 From a pure user perspective, I would not expect the abs function to 
return a negative number. Returning +127 plus a warning the first time 
that happens seems to me a good compromise.

Armando

On 12/10/2011 09:46, David Cournapeau wrote:
> On Tue, Oct 11, 2011 at 8:16 PM, Charles R Harris
> <charlesr.harris at gmail.com>  wrote:
>>
>> On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett<matthew.brett at gmail.com>
>> wrote:
>>> Hi,
>>>
>>> I recently ran into this:
>>>
>>> In [68]: arr = np.array(-128, np.int8)
>>>
>>> In [69]: arr
>>> Out[69]: array(-128, dtype=int8)
>>>
>>> In [70]: np.abs(arr)
>>> Out[70]: -128
>>>
>> This has come up for discussion before, but no consensus was ever reached.
>> One solution is for abs to return an unsigned type, but then combining that
>> with signed type of the same number of bits will cause both to be cast to
>> higher precision. IIRC, matlab was said to return +127 as abs(-128), which,
>> if true, is quite curious.
> In C, abs(INT_MIN) is undefined, so both 127 and -128 work :)
>
> David
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>





More information about the NumPy-Discussion mailing list