math overflow for a not very overflowing calculation in python

穿精又带淫゛_ 提交于 2020-01-07 05:10:24

问题


The calculation for which I'm getting the math overflow number is:

e2 = math.exp([[-20.7313399283991]])

There are actually more extreme numbers that I've done than this, why is this causing an overflow?

I get this error:

OverflowError: math range error

回答1:


math.exp() operates on scalars, not on matrices.

You can use it like so, without the square brackets:

>>> math.exp(-20.7313399283991)
9.919584164742123e-10

If you need to operate on a matrix, you could use numpy.exp():

>>> numpy.exp([[-20.7313399283991]])
array([[  9.91958416e-10]])

This computes the element-by-element e**x and returns an array of the same shape as the input. (Note that this is not the same as the matrix exponential; for that, there is scipy.linalg.expm().)




回答2:


You should call it without the [[]]:

e2 = math.exp(-20.7313399283991)


来源:https://stackoverflow.com/questions/24467709/math-overflow-for-a-not-very-overflowing-calculation-in-python

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