Skip to content

ENH: guard against incorrect use of mp.dps? #657

@mdhaber

Description

@mdhaber

I am a SciPy maintainer, and we frequently use mpmath to compute reference values. (Thank you for your work! It's very helpful.)

A common mistake is for contributors to do, e.g.:

import numpy as np
import mpmath as mp  # should be from mpmath import mp
mp.dps = 50
a, b = mp.mpf(1e-11), mp.mpf(1.001e-11)
print(np.float64(mp.ncdf(b) - mp.ncdf(a)))  # 3.885780586188048e-15

This suffers from catastrophic cancellation just as:

from scipy.special import ndtr
a, b = 1e-11, 1.001e-11
print(ndtr(b) - ndtr(a))  # 3.885780586188048e-15

does, but the fact that mpmath is not working with 50 digits is obscured by the conversion to float. (I've run into other, subtler reasons for not noticing the problem, too.)

Another example in the wild: scipy/scipy#18088 (comment)

I know that this is user error and not a bug in mpmath, but I wonder if it is possible to guard against this. For instance, mpmath doesn't currently have a dps attribute. Perhaps it could be added as a property, and a warning (or error) could be raised if modification is attempted, directing the user to mpmath.mp.dps?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementnew feature requests (or implementation)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions