{"id":444,"library":"mpmath","title":"mpmath","description":"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.","status":"active","version":"1.4.1","language":"python","source_language":"en","source_url":"https://github.com/mpmath/mpmath","tags":["arbitrary-precision","floating-point arithmetic","mathematics","Python library"],"install":[{"cmd":"pip install mpmath","lang":"bash","label":"Install mpmath"}],"dependencies":[{"reason":"Optional library for faster arithmetic at high precision","package":"gmpy","optional":true}],"imports":[{"note":"Importing the 'mp' context object allows setting the precision for arithmetic operations.","symbol":"mp","correct":"from mpmath import mp"},{"note":"Importing 'mpf' enables the creation of arbitrary-precision floating-point numbers.","symbol":"mpf","correct":"from mpmath import mpf"}],"quickstart":{"code":"from mpmath import mp\nmp.dps = 50\nprint(mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2)","lang":"python","description":"This example computes 50 digits of pi by numerically evaluating the Gaussian integral using mpmath."},"warnings":[{"fix":"Use explicit imports like 'from mpmath import mp, mpf' or access functions via 'mpmath.'","message":"Importing 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.","severity":"gotcha","affected_versions":"all"},{"fix":"Use explicit imports or access functions via 'mpmath.'","message":"Mixing 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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T13:51:39.007Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the mpmath library using pip or conda:\n`pip install mpmath`\nor\n`conda install mpmath`","cause":"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'.","error":"ImportError: No module named mpmath"},{"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.\n`pip install mpmath==1.3.0`","cause":"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.","error":"AttributeError: module 'mpmath' has no attribute 'rational'"},{"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.\n```python\nfrom mpmath import mpf\nimport numpy as np\n\n# Original problematic code (example with numpy.int64)\n# x_np_int = np.int64(1)\n# m_val = mpf(x_np_int) # This would raise the TypeError\n\n# Corrected code\nx_np_int = np.int64(1)\nm_val = mpf(float(x_np_int)) # Convert to Python float first\nprint(m_val)\n\n# Example with a numpy array (common when plotting)\n# import mpmath\n# arr = np.array([1.0, 2.0, 3.0])\n# mpmath.cos(arr) # This would raise the TypeError\n\n# Corrected code for array element-wise operations\nimport mpmath\narr = np.array([1.0, 2.0, 3.0])\nm_arr = [mpmath.cos(mpmath.mpf(x)) for x in arr]\nprint(m_arr)\n```","cause":"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.","error":"TypeError: cannot create mpf from <value>"},{"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.\n```python\nfrom mpmath import mp\n\n# Original problematic code\n# mp.dps = 15\n# num_iterations_float = mp.sqrt(25.0)\n# for i in range(num_iterations_float): # This would raise the TypeError\n#     print(i)\n\n# Corrected code\nmp.dps = 15\nnum_iterations_mpf = mp.sqrt(25.0) # This is an mpf object equivalent to 5.0\nnum_iterations_int = int(num_iterations_mpf) # Convert mpf to Python int\nfor i in range(num_iterations_int):\n    print(i)\n\n# Example with numpy.linspace num argument\nimport numpy as np\n# interval_len = mp.pi / 2\n# x_values = np.linspace(0, float(mp.pi), num=interval_len) # This would raise TypeError if interval_len is mpf\n\n# Corrected code\ninterval_len_mpf = mp.pi / 2 # This is an mpf object\ninterval_len_int = int(interval_len_mpf.real) # Convert mpf to Python int (using .real for complex mpf)\nx_values = np.linspace(0, float(mp.pi), num=interval_len_int)\nprint(x_values)\n```","cause":"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.","error":"TypeError: 'float' object cannot be interpreted as an integer"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":4.8,"disk_size":"22.0M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":4.8,"disk_size":"22M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":5.5,"disk_size":"25.1M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":5.5,"disk_size":"26M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.15,"mem_mb":5.4,"disk_size":"16.7M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.15,"mem_mb":5.4,"disk_size":"17M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":5.7,"disk_size":"16.3M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.15,"mem_mb":5.7,"disk_size":"17M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":4.7,"disk_size":"21.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":4.7,"disk_size":"22M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}