1

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 建议问题是我已经提交floaterf. 但是,下面的代码表明这不是问题:

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

1 回答 1

1

这种特殊情况下的问题与mpmath' 的全局precision 设置太低有关。prec默认值为

>>> mpmath.mp.prec
53

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

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

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

于 2015-10-01T21:39:41.467 回答