mpmath Python
Produce arbitrary-precision numerical code with explicit input precision,
working precision, conditioning, convergence evidence, and output accuracy.
Core model
| Object/context |
Meaning |
Rule |
mp.mpf |
Arbitrary-precision real floating value. |
Construct from strings/integers when decimal input must be preserved. |
mp.mpc |
Arbitrary-precision complex value. |
Define branch and complex-domain expectations. |
mp context |
Global/default precision and algorithms. |
Avoid leaking precision changes across callers. |
workdps/workprec |
Scoped absolute decimal/binary working precision. |
Prefer when the function owns a precision target. |
extradps/extraprec |
Scoped precision added to the current context. |
Prefer when guard precision is relative to the caller. |
iv context |
Interval arithmetic values. |
Use only with interval-specific containment semantics. |
| matrix |
Dense arbitrary-precision matrix. |
Suitable for modest sizes; define conditioning and shape. |
Arbitrary precision does not repair an inexact Python float. mp.mpf(0.1)
captures the already-rounded binary value; mp.mpf("0.1") represents the
decimal input at current precision. Precision controls arithmetic performed
after construction, not hidden source accuracy. Read numbers and precision.
Ordered workflow
- Define the mathematical quantity, real/complex domain, target error (absolute,
relative, or digits), and credible input accuracy.
- Construct constants without premature binary-float rounding.
- Estimate conditioning/singularities and choose a stable formulation and
algorithm before increasing precision.
- Compute inside scoped guard digits; never change global
mp.dps without
restoring it.
- Repeat at higher working precision or with another method and compare a
residual, enclosure, or known identity.
- Round/serialize only at the output boundary and report achieved evidence,
not merely configured digits.
- Test near singularities, cancellation, complex branches, poor initial guesses,
non-convergence, and inputs whose original accuracy is lower than
mp.dps.
Intent map
- Use direct special functions before reimplementing series or converting
through machine floats.
- Use
quad/specialized quadrature after splitting discontinuities or difficult
intervals and defining endpoint behavior.
- Use
findroot with an initial guess/interval suited to the solver, then check
the residual independently. verify=False removes one check; it does not make
a root valid.
- Use
diff, numerical sums/products, ODE, transform, or matrix routines only
after checking their documented convergence and domain contract.
- Use
workdps(total_digits) for a local absolute precision target; use
extraprec/extradps when guard precision is relative to the caller.
- Use interval arithmetic when a guaranteed enclosure is required and the
interval algorithm actually provides it. Do not assume
iv.findroot encloses
a root; the official documentation explicitly warns otherwise.
Read algorithms and validation.
Canonical anchor
from mpmath import mp
def _exp_ratio_at(x_text: str, work_digits: int) -> mp.mpf:
with mp.workdps(work_digits):
# Construct inside the context: mpf rounds input at current precision.
x = mp.mpf(x_text)
return mp.expm1(x) / x if x else mp.one
def stable_cancellation(x_text: str, digits: int = 50) -> mp.mpf:
if digits < 2:
raise ValueError("digits must be at least 2")
lower = _exp_ratio_at(x_text, digits + 10)
higher = _exp_ratio_at(x_text, digits + 20)
with mp.workdps(digits):
lower_rounded = +lower
higher_rounded = +higher
if lower_rounded != higher_rounded:
raise ArithmeticError("result did not stabilize at requested precision")
return higher_rounded
The stable formulation matters more than simply raising mp.dps. Constructing
the input inside each working context prevents caller precision from truncating
the decimal first. Unary plus inside workdps(digits) explicitly rounds the
returned value to the requested precision; merely leaving a context does not.
High-risk rules
- Never seed a high-precision computation with an accidental Python float when
the original decimal or exact ratio is available.
- Do not promise
dps correct digits. Guard digits and precision-doubling tests
provide evidence; ill-conditioning can consume them.
- Do not compare high-precision outputs to machine-float expected values as the
oracle. Use exact identities, strings, independent methods, or higher precision.
- Root-finding success requires a small residual and the intended root/domain.
Multiple roots and steep/flat functions can defeat naive convergence checks.
- Quadrature requires singularity, oscillation, infinite interval, and branch
analysis. Split intervals or select specialized methods under a documented rule.
- Global context mutations create order-dependent tests and concurrency hazards.
Scope them with context managers or clone a context where isolation is needed.
- Mixing mpmath and NumPy often creates
object arrays or downcasts to float.
Make conversion, vectorization, and precision loss explicit.
- Formatting many digits is not evidence they are correct.
Version grounding and completion
Official current documentation identifies mpmath 1.3.0. It is not installed in
this foundry. Inspect version, context behavior, and exact callable signatures
before relying on them. Read verification.
Completion requires exact input construction, a scoped precision policy, a
stable algorithm, residual/enclosure or precision-doubling evidence, domain and
failure handling, and serialization that does not silently reduce accuracy.
References
- Numbers and precision
- Algorithms and validation
- Verification and grounding
1---2name: mpmath-python3description: Use for writing, reviewing, debugging, testing, or validating Python mpmath arbitrary-precision numerical code. Trigger on mpf, mpc, mp.dps, workdps, interval arithmetic, high-precision quadrature, root finding, special functions, matrices, inverse transforms, or precision/convergence failures. Do not use for ordinary NumPy vectorization, SymPy symbolic manipulation, decimal currency arithmetic, or machine-float code with no precision requirement.4---56# mpmath Python78Produce arbitrary-precision numerical code with explicit input precision,9working precision, conditioning, convergence evidence, and output accuracy.1011## Core model1213| Object/context | Meaning | Rule |14|---|---|---|15| `mp.mpf` | Arbitrary-precision real floating value. | Construct from strings/integers when decimal input must be preserved. |16| `mp.mpc` | Arbitrary-precision complex value. | Define branch and complex-domain expectations. |17| `mp` context | Global/default precision and algorithms. | Avoid leaking precision changes across callers. |18| `workdps`/`workprec` | Scoped absolute decimal/binary working precision. | Prefer when the function owns a precision target. |19| `extradps`/`extraprec` | Scoped precision added to the current context. | Prefer when guard precision is relative to the caller. |20| `iv` context | Interval arithmetic values. | Use only with interval-specific containment semantics. |21| matrix | Dense arbitrary-precision matrix. | Suitable for modest sizes; define conditioning and shape. |2223Arbitrary precision does not repair an inexact Python float. `mp.mpf(0.1)`24captures the already-rounded binary value; `mp.mpf("0.1")` represents the25decimal input at current precision. Precision controls arithmetic performed26after construction, not hidden source accuracy. Read [numbers and precision](references/precision.md).2728## Ordered workflow29301. Define the mathematical quantity, real/complex domain, target error (absolute,31 relative, or digits), and credible input accuracy.322. Construct constants without premature binary-float rounding.333. Estimate conditioning/singularities and choose a stable formulation and34 algorithm before increasing precision.354. Compute inside scoped guard digits; never change global `mp.dps` without36 restoring it.375. Repeat at higher working precision or with another method and compare a38 residual, enclosure, or known identity.396. Round/serialize only at the output boundary and report achieved evidence,40 not merely configured digits.417. Test near singularities, cancellation, complex branches, poor initial guesses,42 non-convergence, and inputs whose original accuracy is lower than `mp.dps`.4344## Intent map4546- Use direct special functions before reimplementing series or converting47 through machine floats.48- Use `quad`/specialized quadrature after splitting discontinuities or difficult49 intervals and defining endpoint behavior.50- Use `findroot` with an initial guess/interval suited to the solver, then check51 the residual independently. `verify=False` removes one check; it does not make52 a root valid.53- Use `diff`, numerical sums/products, ODE, transform, or matrix routines only54 after checking their documented convergence and domain contract.55- Use `workdps(total_digits)` for a local absolute precision target; use56 `extraprec`/`extradps` when guard precision is relative to the caller.57- Use interval arithmetic when a guaranteed enclosure is required and the58 interval algorithm actually provides it. Do not assume `iv.findroot` encloses59 a root; the official documentation explicitly warns otherwise.6061Read [algorithms and validation](references/operations.md).6263## Canonical anchor6465```python66from mpmath import mp676869def _exp_ratio_at(x_text: str, work_digits: int) -> mp.mpf:70 with mp.workdps(work_digits):71 # Construct inside the context: mpf rounds input at current precision.72 x = mp.mpf(x_text)73 return mp.expm1(x) / x if x else mp.one747576def stable_cancellation(x_text: str, digits: int = 50) -> mp.mpf:77 if digits < 2:78 raise ValueError("digits must be at least 2")79 lower = _exp_ratio_at(x_text, digits + 10)80 higher = _exp_ratio_at(x_text, digits + 20)81 with mp.workdps(digits):82 lower_rounded = +lower83 higher_rounded = +higher84 if lower_rounded != higher_rounded:85 raise ArithmeticError("result did not stabilize at requested precision")86 return higher_rounded87```8889The stable formulation matters more than simply raising `mp.dps`. Constructing90the input inside each working context prevents caller precision from truncating91the decimal first. Unary plus inside `workdps(digits)` explicitly rounds the92returned value to the requested precision; merely leaving a context does not.9394## High-risk rules9596- Never seed a high-precision computation with an accidental Python float when97 the original decimal or exact ratio is available.98- Do not promise `dps` correct digits. Guard digits and precision-doubling tests99 provide evidence; ill-conditioning can consume them.100- Do not compare high-precision outputs to machine-float expected values as the101 oracle. Use exact identities, strings, independent methods, or higher precision.102- Root-finding success requires a small residual and the intended root/domain.103 Multiple roots and steep/flat functions can defeat naive convergence checks.104- Quadrature requires singularity, oscillation, infinite interval, and branch105 analysis. Split intervals or select specialized methods under a documented rule.106- Global context mutations create order-dependent tests and concurrency hazards.107 Scope them with context managers or clone a context where isolation is needed.108- Mixing mpmath and NumPy often creates `object` arrays or downcasts to float.109 Make conversion, vectorization, and precision loss explicit.110- Formatting many digits is not evidence they are correct.111112## Version grounding and completion113114Official current documentation identifies mpmath 1.3.0. It is not installed in115this foundry. Inspect version, context behavior, and exact callable signatures116before relying on them. Read [verification](references/verification.md).117118Completion requires exact input construction, a scoped precision policy, a119stable algorithm, residual/enclosure or precision-doubling evidence, domain and120failure handling, and serialization that does not silently reduce accuracy.121122## References123124- [Numbers and precision](references/precision.md)125- [Algorithms and validation](references/operations.md)126- [Verification and grounding](references/verification.md)