math.ldexp(0.5, 1025)导致OverflowError。Numpy的等效函数返回inf。但是,在另一端,math.ldexp(0.5, -1074)不引发异常,而是返回0.0,如下所示:
In [275]: math.ldexp(0.5, 1024)
Out[275]: 8.98846567431158e+307
In [276]: math.ldexp(0.5, 1025)
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-276-ce1573e0249b> in <module>()
----> 1 math.ldexp(0.5, 1025)
OverflowError: math range error
In [277]: math.ldexp(0.5, -1073)
Out[277]: 5e-324
In [278]: math.ldexp(0.5, -1074)
Out[278]: 0.0为什么当指数太大,但当指数太小时,Python却存在OverflowError?是否有合理的理由,或是否应将其视为一个bug?
发布于 2017-02-16 14:58:36
IEEE浮点算法存在一定程度的不精确性。0.0是一个非常接近math.ldexp(0.5, -1074)的值。但是,没有有效的方法来表示接近math.ldexp(0.5, 1025)的值,所以我认为这就是为什么它会引发异常。
https://stackoverflow.com/questions/42277307
复制相似问题