Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
mpmath is a pure-Python library for arbitrary-precision floating-point arithmetic: it lets you compute with as many decimal digits as you ask for, instead of the roughly 16 digits of a standard Python float. The current PyPI release is 1.4.1 (requires Python 3.9 or later). Researchers use it when double precision is not enough: checking a numerical result at 30 or 100 digits, root-finding on functions that are numerically delicate, or integrating with a quadrature whose error you want to control.
What mpmath is used for in research
The PyPI summary describes mpmath as a “Python library for arbitrary-precision floating-point arithmetic”. In practice that covers three jobs in a research workflow. First, verification: rerun a double-precision calculation at higher precision to see whether the digits you report are stable. Second, special functions and constants, which mpmath evaluates at the working precision you set. Third, numerical root-finding and quadrature through findroot and quad, both documented in the project’s calculus chapters.
It is also a building block: the SymPy guide notes that SymPy depends on it, and SageMath ships it preinstalled. If you only need ordinary double-precision root-finding for an analysis script, the SciPy fsolve guide covers that route; mpmath is the step up when you need more digits.
One design fact shapes everything below. The mpmath basics documentation states: “Mpmath uses a global working precision; it does not keep track of the precision or accuracy of individual numbers.” Precision is a setting you control, not a property each number carries.
How do I install mpmath?
The mpmath setup documentation gives the standard route: pip install mpmath. The package is also preinstalled under Sage. The PyPI page for mpmath links to the project site (mpmath.org) and to its GitHub repository.
Optional gmpy backend
The setup page states that mpmath detects the gmpy library automatically (version 1.03 or later). To see which backend is active, run:
import mpmath.libmpprint(mpmath.libmp.BACKEND)
The value is 'gmpy' when gmpy is in use. To switch gmpy off, set the MPMATH_NOGMPY environment variable before starting Python.
Pinning note for SymPy users
PyPI metadata for SymPy 1.14.0 (checked 2026-11-14) declares the requirement an mpmath version of at least 1.1.0 and below 1.4, while the latest mpmath release is 1.4.1. If you install both into one environment, let pip resolve the pair together rather than forcing the newest mpmath on its own, and read the resolver output.
Step by step: a precision-controlled calculation
This sequence uses only calls and outputs shown in the official documentation.
- Import the names you need.
from mpmath import mp, mpf, findroot, quad, sin, exp, inf, pi - Set the working precision.
mp.dps = 30sets 30 decimal digits. The documentation gives the defaults asmp.dps15 andmp.prec53 bits; settingdpsto 100 givesprec336, roughly 3.33 bits per decimal digit. - Turn on readable printing (optional).
mp.pretty = Trueprints numbers as digit strings instead ofmpf('...')objects. - Find a root.
findroot(sin, 3)returns3.14159265358979323846264338328at 30 digits, the documented example. - Integrate.
quad(sin, [0, pi])returns2.0at the default 15 digits. For an infinite interval, the documented check isquad(lambda x: exp(-x**2), [-inf, inf])**2, which gives3.14159265358979, that is, the square of the Gaussian integral equals π. - Raise precision for one block only. Use
workdps,workprec,extradpsorextraprecinside awithstatement; the documentation says the precision is restored when the block ends, even if an exception occurs.
Precision model: which setting to use
| You want to | Use | What the documentation says |
|---|---|---|
| Set digits globally | mp.dps = 30 |
Decimal digits; default 15. |
| Set bits globally | mp.prec = 336 |
Binary precision; default 53. dps = 100 corresponds to prec = 336. |
| Change precision for one block | with workdps(50): or with workprec(...): |
Resets afterwards, even when an exception is raised. |
| Add guard digits to a block | extradps / extraprec |
Raises the current precision by the amount given, inside a with block. |
| Print fewer digits without changing precision | nstr(a, 8) |
nstr of one-sixth at 8 digits shows '0.16666667'; the stored number is unchanged. |
The float-input trap
The basics chapter warns: “Be careful when mixing mpmath numbers with Python floats”. A float literal is already rounded to double precision before mpmath sees it. At 30 digits, mpf(10.9) gives mpf('10.9000000000000003552713678800501'), whereas mpf('10.9'), built from a string, is accurate. Values such as 0.5 and 0.75 are exactly representable in binary and are safe. For research data read from a file, convert from strings (or build mpf values directly from the text) rather than from Python floats.
Reading the output of findroot
The signature in the optimization chapter is findroot(f, x0, solver=Secant, tol=None, verbose=False, verify=True, **kwargs). The default solver is the secant method; the solver can be given by name: secant, mnewton, halley, muller, illinois, pegasus, anderson, ridder, anewton or bisect. With verify=True, findroot raises ValueError if the residual check the squared residual exceeds tol fails, so a returned value has passed that test.
The documented findroot calls are tabulated in the data table after this guide. Read them for three behaviours. A real function can have a complex root, so a complex start such as j is needed to reach it. Zeros of zeta are found from a complex start near the line Re(s) = 1/2. A repeated root, the (x−1)^99 example, is the hard case: the default secant solve with verify=False returns a number near, but not at, the root, whereas solver='mnewton' started from −10 reaches 1.0. A complex root can also be reached with the Muller solver, as in findroot(lambda x: x**4 + x + 1, (0, 1, 2), solver='muller'), which returns (0.727136084491197 + 0.934099289460529j).
The repeated-root case is the lesson for research use: a default solve can return a number near, but not at, the root, and verify=False hides that. Keep verify=True unless you have a reason not to.
Integration with quad
The signature is quad(f, *points, **kwargs), available for one-, two- and three-dimensional integrals. The default method is tanh-sinh; method='gauss-legendre' is the alternative. Passing error=True returns a pair, the value and an error estimate. maxdegree and verbose are also documented options. The data table after this guide lists the documented values; three points matter in practice:
- Split at singularities. The integrand
abs(sin(x))has kinks; integrating over[0, pi, 2*pi]instead of[0, 2*pi]changes the answer from the inaccurate value to the correct 4.0. - Choose the method deliberately. For
logon [0, 1], tanh-sinh gives −1.0 and Gauss-Legendre gives −0.999932197413801, so the choice of method is visible in the digits. - Raise
maxdegreefor long intervals. The documentation showsquad(sin, [0, 1000], maxdegree=10)giving 0.437620923709297 where the default degree fails, and describes the default degree heuristic as sometimes wrong.
Troubleshooting
ValueError: Could not find root within given tolerance
The documented example is findroot(lambda x: x**2, (-1, .5), solver='anderson'), which raises ValueError with the advice “Try another starting point or tweak arguments.” Interval-type solvers need a sign change across the interval, and x**2 has none. Change the solver or the starting points.
Digits look right but the answer is wrong at high precision
Check whether a Python float entered the calculation (see the float-input trap above). Rebuild the inputs from strings and compare.
Results differ when you change the global precision
Because precision is global, a function that changes mp.dps without restoring it affects every later call. Wrap temporary changes in workdps so the reset is automatic.
Slow performance
Check mpmath.libmp.BACKEND. If it is not 'gmpy' and you have installed gmpy, confirm that MPMATH_NOGMPY is not set in the environment.
Alternatives and companion libraries
| Tool | What it is (PyPI or project documentation) | Relationship to mpmath |
|---|---|---|
| SymPy 1.14.0 | “Computer algebra system (CAS) in Python” | Declares an mpmath dependency (1.1.0 or later, below 1.4); symbolic work with mpmath numerics underneath. |
| SciPy 1.18.1 | “Fundamental algorithms for scientific computing in Python”; requires Python 3.12 or later, NumPy 2.0 or later | Double-precision root-finding and quadrature; the faster choice when 15 digits suffice. |
| SageMath | Mathematics system that mpmath’s setup page says includes mpmath preinstalled | Use mpmath without a separate install. |
| gmpy2 2.3.2 | “gmpy2 interface to GMP, MPFR, and MPC for Python” | Related multiple-precision library; mpmath’s own gmpy backend detection is documented for gmpy 1.03 or later. |
Frequently asked questions
What is mpmath used for?
Arbitrary-precision numerical computation in Python: setting the number of digits, then evaluating functions, finding roots and computing integrals at that precision.
How do I install mpmath?
Run pip install mpmath, as the setup documentation states. It is already included in Sage.
What is the default precision?
15 decimal digits (mp.dps) or 53 bits (mp.prec). Setting dps to 100 gives prec 336.
Why is mpf(10.9) not exactly 10.9?
The float 10.9 is rounded to double precision before mpmath receives it. Build the number from the string ‘10.9’ instead.
Does mpmath track the accuracy of each number?
No. The documentation says it uses a global working precision and does not track the precision or accuracy of individual numbers.
Does mpmath use gmpy?
Yes, when gmpy (version 1.03 or later) is installed it is detected automatically; check mpmath.libmp.BACKEND.
Sources
- mpmath, PyPI: pypi.org/project/mpmath (JSON metadata read 2026-11-14).
- mpmath documentation, setup: mpmath.org/doc/current/setup.html (read 2026-11-14).
- mpmath documentation, basics: mpmath.org/doc/current/basics.html (read 2026-11-14).
- mpmath documentation, optimization (findroot): mpmath.org/doc/current/calculus/optimization.html (read 2026-11-14).
- mpmath documentation, numerical integration (quad): mpmath.org/doc/current/calculus/integration.html (read 2026-11-14).
- SymPy, SciPy and gmpy2 package metadata on PyPI: sympy, scipy, gmpy2 (read 2026-11-14).
Data: mpmath documented worked examples: findroot and quad outputs at stated precision
Calls from the official mpmath documentation for findroot and quad, with the working precision stated in the docs and the printed result, compiled to show how solver, method and interval choice change the digits returned.
Period covered: mpmath 1.4.x documentation.
| Documented call | Working precision (decimal digits) | Documented result |
|---|---|---|
| findroot(sin, 3) | 30 | 3.14159265358979323846264338328 |
| findroot(lambda x: x**3+2*x+1, j) | 15 | (0.226698825758202 + 1.46771150871022j) |
| findroot(zeta, 0.5+14j) | 30 | (0.5 + 14.1347251417346937904572519836j) |
| findroot((x-1)**99, 0.9, verify=False) | 15 | 0.918073542444929 |
| findroot((x-1)**99, -10, solver='mnewton') | 15 | 1.0 |
| quad(sin, [0, pi]) | 15 | 2.0 |
| quad(lambda x: 2/(x**2+1), [0, inf]) | 15 | 3.14159265358979 |
| quad(log, [0, 1]), tanh-sinh | 15 | -1.0 |
| quad(log, [0, 1]), gauss-legendre | 15 | -0.999932197413801 |
| quad(lambda x: abs(sin(x)), [0, pi, 2*pi]) | 15 | 4.0 |
| quad(lambda x: abs(sin(x)), [0, 2*pi]) (unsplit) | 15 | 3.99900894176779 |
| quad(sin, [0, 1000], maxdegree=10) | 15 | 0.437620923709297 |
Sources
- mpmath documentation: Root-finding and optimization (accessed 2026-11-14)
- mpmath documentation: Numerical integration (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







