我是否遇到了 mpmath 的浮点精度限制?
Have I encountered a limit to mpmath's floating-point precision?
mpmath
声称支持 "arbitrary-precision floating-point arithmetic."
还有。 . .
>>> 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 表示问题是我提交了 float
到 erf
。但是,下面的代码表明这不是问题所在:
>>> 1 + mpmath.erf(mpmath.mpf('-5.922'))
mpf('0.0')
本例中的问题与 mpmath
的 global prec
ision setting 太低有关。 prec
的默认值为
>>> mpmath.mp.prec
53
当我将其设置为 100
时,我得到了预期的结果:
>>> 1 + mpmath.erf(-5.922)
mpf('5.5236667058718205581661131647751e-17')
在这种情况下,速度差异并不明显,但请注意,提高精度通常会增加计算结果所需的时间。
mpmath
声称支持 "arbitrary-precision floating-point arithmetic."
还有。 . .
>>> 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 表示问题是我提交了 float
到 erf
。但是,下面的代码表明这不是问题所在:
>>> 1 + mpmath.erf(mpmath.mpf('-5.922'))
mpf('0.0')
本例中的问题与 mpmath
的 global prec
ision setting 太低有关。 prec
的默认值为
>>> mpmath.mp.prec
53
当我将其设置为 100
时,我得到了预期的结果:
>>> 1 + mpmath.erf(-5.922)
mpf('5.5236667058718205581661131647751e-17')
在这种情况下,速度差异并不明显,但请注意,提高精度通常会增加计算结果所需的时间。