Python溢出错误: math range error

标签 python python-2.7

类似于Python: OverflowError: math range error 。下面是我的代码,以及我尝试调试时得到的错误(和变量值)。在 python 控制台中手动进行数学运算效果很好。是因为 sigma 是 int 吗?就其值(value)而言,MSE 变量是通过对 numpy 数组求和生成的,而 simga 变量只是硬编码的“100”。

def normalize_error(sigma, mse):
    return math.exp(-mse/(2*(sigma**2)))

enter image description here

最佳答案

如果您的指数值变得太大,您将收到此错误。由于您使用的是 math.exp,因此该值将是 float 。

根据您的系统,系统中最大的 float 将由 sys.float_info 定义。 .

>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

因此,在我的系统上,1.7976931348623157e+308 是我可能拥有的最大 float 。

<小时/>

您可以检查以下运行以进行相同的分析:

>>> import math
>>> def normalize_error(sigma, mse):
...     return math.exp(-mse/(2*(sigma**2)))
... 
>>> normalize_error(3, 4)
0.36787944117144233
>>> normalize_error(3, -4)
1.0
>>> normalize_error(.3, -4)
4477014353.361036
>>> normalize_error(.3, -100)
1.8824011022575583e+241
>>> normalize_error(.02, -100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in normalize_error
OverflowError: math range error

关于Python溢出错误: math range error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29583780/

相关文章:

python - 无法创建 Django 模型的实例

python - Zlib 在 python 中压缩

python - 同时求和 4 维矩阵的 3 个维度

python - 在 python 中使用 compile 和 eval 时出现奇怪的问题

python - 如何在训练过程中纠正不稳定的损失和准确率? (二元分类)

python - 两个数据帧的 Pandas 合并的输出不会产生预期的形状

python - 在python中用数字替换单词中的多个字母?

python - 如何在 python 中创建 virtualenv?

python - 循环遍历多个 Excel 文件以使用 pandas 修改和重写原始文件

python - Python 项目的调试和发布构建策略