Question

I am calculating the absolute difference between two numpy.uint8s 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?

Was it helpful?

Solution

As your comment indicates, they aren't ints; they're numpy.uint8s. Just convert them to ints:

>>> 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top