我是否遇到过 mpmath 浮点精度的限制?



mpmath声称支持"任意精度浮点运算"。

然而……

>>> import mpmath
>>> 1 + mpmath.erf(-5.921)
mpf('1.1102230246251565e-16')
>>> 1 + mpmath.erf(-5.922)  # I expect a smaller positive number here.
mpf('0.0')

我错过了什么吗?或者这是mpmath的基本限制?

@jonrsharpe建议问题是我已经向erf提交了一个float。但是,下面的代码表明这不是问题:

>>> 1 + mpmath.erf(mpmath.mpf('-5.922'))
mpf('0.0')

这个特殊情况下的问题与mpmath的全局prec视觉设置过低有关。prec的默认值是

>>> mpmath.mp.prec
53

当我将其设置为100时,我得到了我期望的结果:

>>> 1 + mpmath.erf(-5.922)
mpf('5.5236667058718205581661131647751e-17')

在这种情况下,速度差异不明显,但请注意,增加精度通常会增加计算结果所需的时间。

最新更新