Better way to calculate difference between numpy unsigned ints in Python
Question
I am calculating the absolute difference between two numpy.uint8
s in python with
d = abs(a - b)
, which results in an overflow error if b
is bigger than a
. What's the best way to avoid this?
Solution
As your comment indicates, they aren't int
s; they're numpy.uint8
s. Just convert them to int
s:
>>> a, b = map(numpy.uint8, (50, 60))
>>> a - b
__main__:1: RuntimeWarning: overflow encountered in ubyte_scalars
246
>>> a, b = map(int, (a, b))
>>> a - b
-10
Since you are concerned about speed, here are a couple of tests (borrowing Sven's form, with thanks):
>>> %timeit abs(int(a) - int(b))
1000000 loops, best of 3: 410 ns per loop
>>> %timeit a - b if a > b else b - a
1000000 loops, best of 3: 470 ns per loop
So yes, it's faster, but unless we're talking about doing it hundreds of millions of times, it won't matter a bit.
OTHER TIPS
The easiest way is to manually convert your numbers to Python ints first:
d = abs(int(a) - int(b))
Python ints can't overflow (unless the memory is full).
For me it does not throw overflow error, just results in false values if b
is bigger than a
. To stay in uint8
boundaries use this function:
def abs_dev (a, b) :
sub1 = a - b
sub2 = b - a
mask = a < b
sub1[mask] = sub2[mask]
return sub1