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')
在这种情况下,速度差异不明显,但请注意,增加精度通常会增加计算结果所需的时间。