简体   繁体   中英

Have I encountered a limit to mpmath's floating-point precision?

mpmath purports to support "arbitrary-precision floating-point arithmetic."

And yet . . .

>>> 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')

Am I missing something? Or is this a fundamental limitation of mpmath ?

@jonrsharpe has suggested that the problem is that I've submitted a float to erf . However, the code below shows that this is not the problem:

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

The issue in this particular case has to do with mpmath 's global prec ision setting being too low. The default value for prec is

>>> mpmath.mp.prec
53

When I set it to 100 , I get the result I was expecting:

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

In this case the speed difference isn't noticeable, but note that increasing the precision generally increases the time required to compute the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM