Python: OverflowError: math range error

人盡茶涼 提交于 2019-11-28 10:44:18

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.

To fix it use:

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

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.

This may give you a clue why:

http://www.wolframalpha.com/input/?i=math.exp%28-4*1000000*-0.0641515994108%29

Notice the 111442 exponent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!