1

Similar to Python: OverflowError: math range error. Below is my code, along with the error (and variable values) I get when I try to debug. Doing math manually in python console works fine. Is it because sigma is an int? For what it's worth, the MSE variable is generated by summing a numpy array, and the simga variable is just "100" hardcoded.

def normalize_error(sigma, mse):
    return math.exp(-mse/(2*(sigma**2)))

enter image description here

Community
  • 1
  • 1
jfalkson
  • 3,471
  • 4
  • 20
  • 25
  • 1
    What parameters are you passing to this code snippet? This works for me for sample values. – Anshul Goyal Apr 11 '15 at 22:28
  • Yeah it works for me as well, in python console and type the numbers manually, but not in my script. The mse variable is from summing a numpy array, is that possibly the issue? – jfalkson Apr 11 '15 at 23:17
  • Yes, thats possible. Check my answer below. Basically, if the sum of your numpy array is a sufficiently large negative number, you will get the error, for example: `normalize_error(100, -20000000)`. – Anshul Goyal Apr 11 '15 at 23:21

1 Answers1

2

You will get this error if your exponential value becomes too great. Since you are using math.exp, this value will be a float.

Depending on your system, the largest float number in your system will be defined by your sys.float_info.

>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

So on my system, 1.7976931348623157e+308 is the largest float I could possibly have.


You can check the following runs for an analysis of the same:

>>> import math
>>> def normalize_error(sigma, mse):
...     return math.exp(-mse/(2*(sigma**2)))
... 
>>> normalize_error(3, 4)
0.36787944117144233
>>> normalize_error(3, -4)
1.0
>>> normalize_error(.3, -4)
4477014353.361036
>>> normalize_error(.3, -100)
1.8824011022575583e+241
>>> normalize_error(.02, -100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in normalize_error
OverflowError: math range error
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Calling int(mse) resolved the issue. I guess having a uint64 (from numpy.sum) with an int was causing issue. Marking this as the best answer, thanks for the detail. – jfalkson Apr 11 '15 at 23:19