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

"V. Armando Solé" sole@esrf...
Wed Oct 12 04:20:31 CDT 2011


On 12/10/2011 10:46, David Cournapeau wrote:
> On Wed, Oct 12, 2011 at 9:18 AM, "V. Armando Solé" wrote:
>>   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.
> I guess the question is what's the common context to use small
> integers in the first place. If it is to save memory, then upcasting
> may not be the best solution. I may be wrong, but if you decide to use
> those types in the first place, you need to know about overflows. Abs
> is just one of them (dividing by -1 is another, although this one
> actually raises an exception).
>
> Detecting it may be costly, but this would need benchmarking.
>
> That being said, without context, I don't find 127 a better solution than -128.

Well that choice is just based on getting the closest positive number to 
the true value (128). The context can be anything, for instance you 
could be using a look up table based on the result of an integer 
operation ...

In terms of cost, it would imply to evaluate the cost of something like:

a = abs(x);
  if (a < 0) {a -= MIN_INT;}
return a;

Basically is the cost of the evaluation of an if condition since the 
content of the block (with or without warning) will bot be executed very 
often.
I find that even raising an exception is better than returning a 
negative number as result of the abs function.

Anyways, I have just tested numpy.array([129], dtype=numpy.int8) and I 
have got the array as [-127] when I was expecting a sort of unsafe cast 
error/warning. I guess I will just stop here. In any case, I am very 
grateful to the mailing list and the original poster for exposing this 
behavior so that I can keep it in mind.

Best regards,

Armando






More information about the NumPy-Discussion mailing list