Skip to content Skip to sidebar Skip to footer

Python Overflowerror: Math Range Error Being Raised Differently In Different Runs

My program seems to be crashing almost arbitrarily. My code includes these two lines: z[i, j] = -math.exp(oneminusbeta[j, i]) weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])

Solution 1:

The result of your calculation cannot be represented on your computer. This probably means that math.exp(...) is greater than about 10, or the argument passed to math.exp() is greater than about 710.

Try printing the values of beta[j,i] and oneminusbeta[j,i] before each calculation.

In fact, you don't have to print comments before every line of code. Instead, try wrapping the calculations with a try block, like so:

try:
  weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])
except OverflowError:
  print"Calculation failed! j=%d i=%d beta=%f oneminusbeta=%f"%(j,i,beta[j,i],oneminusbeta[j,i])
  raise

Solution 2:

Your overflow is almost certainly a real overflow; one of your values is too large to fit in a Python float (meaning a C double).


So, why does it happen in different places each time?

Because you're using Numba.

Numba JIT-compiles your code. If it detects that there's no contention, it can reorder your code—or even, at least in theory, run it in parallel on multiple cores or on the GPU (although I think at present you only get GPU computation if you code it explicitly with numba.cuda).

At any rate, this means that the path through your code can be nondeterministic. If there's more than one place an overflow could happen, you can't predict which one will fail and trigger the exception.


At any rate, that doesn't really matter. If your calculations are overflowing, you have to fix that. And the fact that different ones overflow each time shouldn't make it that much harder to debug—especially given that it apparently usually happens in a single place, just not always.

Post a Comment for "Python Overflowerror: Math Range Error Being Raised Differently In Different Runs"