数学溢出在python中不是很溢出的计算 [英] math overflow for a not very overflowing calculation in python
问题描述
我得到数学溢出数的计算公式是:
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?
我收到此错误:
OverflowError: math range error
推荐答案
math.exp()
对标量进行运算,而不对矩阵进行运算.
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
如果需要在矩阵上进行运算,则可以使用 numpy.exp()
:
If you need to operate on a matrix, you could use numpy.exp()
:
>>> numpy.exp([[-20.7313399283991]])
array([[ 9.91958416e-10]])
这将按元素计算e**x
并返回与输入形状相同的数组. (请注意,这与矩阵指数不同;为此,存在 scipy.linalg.expm()
.)
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()
.)
这篇关于数学溢出在python中不是很溢出的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!