Registry / data / mpmath

mpmath

JSON →
library1.4.1pypypi✓ verified 49d ago

mpmath is a Python library for arbitrary-precision floating-point arithmetic, currently at version 1.4.1, with a release cadence of approximately every 1-2 years.

data
pip install mpmath
Install & Compatibility
Where this runs
tested against v1.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.128s · 22MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.8s · import 0.120s · 22MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mp
from mpmath import mp
Importing the 'mp' context object allows setting the precision for arithmetic operations.
mpf
from mpmath import mpf
Importing 'mpf' enables the creation of arbitrary-precision floating-point numbers.

This example computes 50 digits of pi by numerically evaluating the Gaussian integral using mpmath.

from mpmath import mp mp.dps = 50 print(mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2)
Debug
Known issues
gotchaImporting all functions with 'from mpmath import *' can lead to name conflicts with other libraries. It's recommended to import only the necessary functions or use the 'mpmath.' namespace.
fix
Use explicit imports like 'from mpmath import mp, mpf' or access functions via 'mpmath.'
affects: all
gotchaMixing mpmath with other libraries that define functions with the same names can cause unexpected behavior. To avoid this, import only the needed functions or use the 'mpmath.' namespace.
fix
Use explicit imports or access functions via 'mpmath.'
affects: all
Errors
Common errors & fixes
ImportError: No module named mpmath
The mpmath library is not installed in the current Python environment or the Python interpreter cannot find it. This can also manifest as 'ImportError: No module named mpmath.libmp'.
fix
Install the mpmath library using pip or conda:
`pip install mpmath`
or
`conda install mpmath`
AttributeError: module 'mpmath' has no attribute 'rational'
This error typically occurs when a dependent library (like SymPy) expects an `mpmath.rational` attribute, but a newer version of mpmath (e.g., 1.4.0a0 or later) has removed or moved this attribute, leading to an API incompatibility.
fix
Pin the mpmath version to a compatible older release, such as 1.3.0, or update the dependent library if a version compatible with newer mpmath releases is available.
`pip install mpmath==1.3.0`
TypeError: cannot create mpf from <value>
This error occurs when attempting to convert an object of an unsupported type (e.g., a NumPy integer type like `numpy.int64`, a NumPy array, or other non-standard numeric types) directly into an `mpmath.mpf` (arbitrary-precision float) object. `mpmath` expects standard Python numbers, strings, or other `mpmath` types for direct conversion.
fix
Explicitly convert the input to a standard Python float or a string before passing it to `mpmath.mpf` or an `mpmath` function. For NumPy types, cast to `float` first.
```python
from mpmath import mpf
import numpy as np

# Original problematic code (example with numpy.int64)
# x_np_int = np.int64(1)
# m_val = mpf(x_np_int) # This would raise the TypeError

# Corrected code
x_np_int = np.int64(1)
m_val = mpf(float(x_np_int)) # Convert to Python float first
print(m_val)

# Example with a numpy array (common when plotting)
# import mpmath
# arr = np.array([1.0, 2.0, 3.0])
# mpmath.cos(arr) # This would raise the TypeError

# Corrected code for array element-wise operations
import mpmath
arr = np.array([1.0, 2.0, 3.0])
m_arr = [mpmath.cos(mpmath.mpf(x)) for x in arr]
print(m_arr)
```
TypeError: 'float' object cannot be interpreted as an integer
An `mpmath.mpf` object (or sometimes a standard Python float that results from mpmath calculations) was passed to a Python function or operation that strictly requires an integer argument, such as Python's built-in `range()`, indexing a list, or `numpy.linspace`'s `num` argument.
fix
Convert the `mpmath.mpf` object (or the float) to a standard Python integer using `int()` before passing it to functions or operations that expect an integer.
```python
from mpmath import mp

# Original problematic code
# mp.dps = 15
# num_iterations_float = mp.sqrt(25.0)
# for i in range(num_iterations_float): # This would raise the TypeError
#     print(i)

# Corrected code
mp.dps = 15
num_iterations_mpf = mp.sqrt(25.0) # This is an mpf object equivalent to 5.0
num_iterations_int = int(num_iterations_mpf) # Convert mpf to Python int
for i in range(num_iterations_int):
    print(i)

# Example with numpy.linspace num argument
import numpy as np
# interval_len = mp.pi / 2
# x_values = np.linspace(0, float(mp.pi), num=interval_len) # This would raise TypeError if interval_len is mpf

# Corrected code
interval_len_mpf = mp.pi / 2 # This is an mpf object
interval_len_int = int(interval_len_mpf.real) # Convert mpf to Python int (using .real for complex mpf)
x_values = np.linspace(0, float(mp.pi), num=interval_len_int)
print(x_values)
```
Upgrade
Version history
1.4.1latest on PyPI
Audit
Dependencies
gmpyoptionalOptional library for faster arithmetic at high precision
Agent activity
12 hits · last 30 days
seranking-bot
4
node
2
ahrefsbot
2
Amazon
1
bingbot
1
googlebot
1
Resources