问题
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