Python: OverflowError: math range error

Python: OverflowError: math range error – Stack Overflow

The number you’re asking math.exp to calculate has, in decimal, over 110,000 digits. That’s slightly outside of the range of a double, so it causes an overflow.

Python: OverflowError: math range error – Stack Overflow

To fix it use:

try:
    ans = math.exp(200000)
except OverflowError:
    ans = float('inf')

Python: OverflowError: math range error – Stack Overflow

I think the value gets too large to fit into a double in python which is why you get the OverflowError. The largest value I can compute the exp of on my machine in Python is just sligthly larger than 709.78271.

Leave a Reply

Your email address will not be published. Required fields are marked *