Search code examples
pythonfloating-pointprecisionmpmath

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

Solution

  • The issue in this particular case has to do with mpmath's global precision 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.