# mpmath

**mpmath** 是一个自由、采用 BSD 许可证的 Python 库，用于任意精度的实数与复数浮点运算——所谓“任意精度”，是指计算中使用的位数可由用户自行设定，而不由硬件固定。<sup>[1](https://mpmath.org/)</sup> 该库由 Fredrik Johansson（弗雷德里克·约翰松）自 2007 年起开发，他是在 Inria Bordeaux（波尔多法国国家信息与自动化研究所）从事计算机代数与任意精度算术的研究员，开发过程中得到了众多贡献者的帮助。<sup>[1](https://mpmath.org/)</sup><sup> • </sup><sup>[2](https://fredrikj.net/)</sup> 该库完全用纯 Python 编写，其算术建立在 Python 的任意精度 `long` 整数之上，并充当 SymPy 计算机代数系统的数值求值引擎。<sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup><sup> • </sup><sup>[4](https://docs.sympy.org/latest/contributing/dependencies.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 纯 Python 库，用于任意精度的实数与复数浮点算术，BSD 许可证<sup>[1](https://mpmath.org/)</sup> |
| 首次发布 | 0.1 版于 2007 年 9 月 27 日；1.0 版于 2017 年 9 月 27 日<sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup> |
| 最新版本 | 1.4.1，2026 年 3 月 15 日发布<sup>[5](https://github.com/mpmath/mpmath/blob/master/README.rst)</sup> |
| 默认精度 | 53 位（`mp.prec = 53`），等于 15 位十进制数字（`mp.dps = 15`），与 IEEE 754 双精度一致<sup>[6](https://www.mpmath.org/doc/current/basics.html)</sup><sup> • </sup><sup>[7](https://mpmath.org/doc/current/technical.html)</sup> |
| 内部表示 | 每个 `mpf` 是一个四元组（符号、尾数、指数、位数），建立在任意精度整数之上<sup>[7](https://mpmath.org/doc/current/technical.html)</sup> |
| 在生态系统中的角色 | SymPy 唯一的硬依赖；同时也是 Sage 的标准组件<sup>[4](https://docs.sympy.org/latest/contributing/dependencies.html)</sup><sup> • </sup><sup>[1](https://mpmath.org/)</sup> |
| 可选加速 | 安装 gmpy2 2.3.0 或更高版本后自动使用 gmpy2 整数，在约 100 位以上快得多<sup>[8](https://mpmath.readthedocs.io/en/latest/setup.html)</sup> |

## 历史

约翰松最初是在大学头几年里用 Python 写一个业余的计算机代数系统，随后于 2007 年初加入 SymPy 团队，贡献了 `sympy.numerics` 模块，用于任意精度浮点数、特殊函数、求根和数值积分。该模块后来被剥离为一个独立项目，mpmath 的 0.1 版于 2007 年 9 月 27 日发布。<sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup>

2000 年代后期，随着多位贡献者的加入，该库不断成长：Mario Pernici（马里奥·佩尔尼奇）带来了更快的初等函数算法，Mike Taschuk（迈克·塔舒克）带来了椭圆函数与雅可比 theta 函数，Vinzent Steinberg（文岑特·施泰因贝格）带来了矩阵与求根，Case Van Horsen（凯斯·范霍森）带来了 GMPY 后端。2009 年，mpmath 成为 Sage 的标准包；在 William Stein（威廉·斯坦）获得的 NSF 项目经费资助下，约翰松于 2009 年和 2010 年的夏天为其工作，添加了广义超几何函数与梅耶尔 G 函数。<sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup> 1.0 版于 2017 年 9 月 27 日发布，恰在 0.1 版之后整十年。<sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup> 在长期停顿之后，活跃的维护重新恢复：1.3.0 于 2023 年 3 月面世，1.4.0 于 2026 年 2 月，1.4.1 于 2026 年 3 月 15 日，近期版本由 Sergey B Kirpichev（谢尔盖·B·基尔皮切夫）担任发布管理员。<sup>[5](https://github.com/mpmath/mpmath/blob/master/README.rst)</sup><sup> • </sup><sup>[9](https://github.com/mpmath/mpmath/releases)</sup>

## 算术与精度

mpmath 将一个浮点数表示为一个四元组（符号、尾数、指数、位数），其中尾数是一个非负的任意精度整数，位数指其以比特计的大小；独立的符号字段与存储的位数可加快算术运算。由于尾数与指数都是任意精度整数，数值可以大到内存允许的程度，而且每一个二进制浮点值都是精确的有理数。只要尾数保持在 \( 2^{prec} \) 以下，加、减、乘都是精确的；超出之后即被截断，从而引入舍入误差。除法一般是不精确的，因为没有任何一种精度能精确表示 1/3。<sup>[7](https://mpmath.org/doc/current/technical.html)</sup>

工作精度是一个由 `mp` 上下文对象控制的全局设置，默认为 53 位和 15 位十进制数字；二进制精度与十进制精度大致满足 `prec = 3.33*dps`，因此圆周率的 100 位十进制数字约需 333 位。<sup>[6](https://www.mpmath.org/doc/current/basics.html)</sup> 在默认设置下，`mpf` 类型大致模拟 Python 的 `float`，但 mpmath 并不模拟 float 指数上下限处的溢出与下溢，而且对超越函数能给出更精确的结果。<sup>[7](https://mpmath.org/doc/current/technical.html)</sup>

**正确性保证。** `mp` 上下文中的基本算术始终是正确舍入的，可精确表示的结果保证精确。更高层的运算则只提供较弱的保证：mpmath 一般只承诺使用不低于用户设定的精度，而超越函数、线性代数和积分方面的函数通常会临时提高工作精度（例如对 50 位的请求使用 70 位），以便以很高的概率给出正确舍入的结果。文档建议通过以更高精度重算、与其他软件交叉比对或作解析验证来核实结果。<sup>[7](https://mpmath.org/doc/current/technical.html)</sup>

十进制输入需要小心：0.1、0.01、0.001 都没有精确的二进制浮点表示，因此在高精度下，有小数部分的 `mpf` 值应当由字符串或整数创建，而不要用 Python 浮点数。<sup>[7](https://mpmath.org/doc/current/technical.html)</sup><sup> • </sup><sup>[10](https://mpmath.readthedocs.io/en/1.4.1/basics.html)</sup>

## 上下文

mpmath 提供三种执行上下文。`mp` 上下文支持的函数最多，测试最充分，也是几乎所有文档示例的默认上下文。`fp` 上下文将输入转换为普通的 Python `float` 或 `complex`（硬件双精度），在需要大量函数求值而双精度足够时，可以带来显著的加速。`iv` 上下文实现任意精度区间算术，其中的值是一个闭区间 \( [a,b] \)，每次运算返回一个保证包含精确结果的区间；这以速度为代价（通常耗时至少为 `mp` 的两倍）提供了严格的误差追踪，有时边界会过于宽泛，其支持仍属实验性质。<sup>[11](https://mpmath.readthedocs.io/en/stable/contexts.html)</sup><sup> • </sup><sup>[12](https://mpmath.org/doc/current/contexts.html)</sup>

## 功能

在算术之上，mpmath 以任意精度实现了 Python `math` 与 `cmath` 模块中针对实数与复数的标准函数，并实现了大量特殊函数，包括伽马函数、黎曼 zeta 函数、误差函数、贝塞尔函数、超几何函数、椭圆函数和正交多项式，以及圆周率、欧拉常数、卡塔兰常数和阿佩里常数等常数。<sup>[13](https://www.mpmath.org/doc/current/functions/index.html)</sup>

数值微积分模块涵盖多项式求值与求根，标量与多维系统的求根（割线法、牛顿法、哈利法、穆勒法、基于二分的方法以及布伦特法），带收敛加速的无穷级数求和，极限，包括分数阶的数值微分，数值积分，通过高阶泰勒法（`odefun`）求解常微分方程，切比雪夫与傅里叶逼近，以及数值逆拉普拉斯变换。<sup>[14](https://mpmath.readthedocs.io/en/latest/calculus/index.html)</sup><sup> • </sup><sup>[15](https://mpmath.readthedocs.io/en/latest/calculus/odes.html)</sup> 线性代数包括线性方程组求解、LU 与 QR 分解、特征值、奇异值和矩阵函数。<sup>[1](https://mpmath.org/)</sup>

在积分方面，mpmath 实现了两种求积算法：tanh-sinh（双曲正切-正弦，默认）和高斯-勒让德。当求值点数加倍时，两者都把精度大致翻倍，这使它们适用于数百或数千位数字的高精度求积。tanh-sinh 对端点奇异性与无穷区间处理得更好，且节点计算成本低；高斯-勒让德需要的函数求值更少，对重复的光滑积分更快。<sup>[16](https://mpmath.org/doc/current/mpmath.pdf)</sup> 一个典型用法是通过计算高斯积分来得到圆周率的 50 位数字：<sup>[1](https://mpmath.org/)</sup>

```python
>>> from mpmath import mp
>>> mp.dps = 50
>>> print(mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2)
3.1415926535897932384626433832795028841971693993751
```

## 性能与后端

由于 mpmath 是纯 Python 的，它比基于 C 的多精度库要慢。在 MPFR 项目 2007 年于 16 核 AMD Opteron 上进行的对比中，启用 gmpy 后端的 mpmath 0.9 做一次 1000 位乘法耗时 0.0139 毫秒，而 MPFR 为 0.0039 毫秒；做一次 10000 位指数运算分别为 42.7 毫秒和 14.3 毫秒；在 1000 位时，它在若干运算上比 Maple、Mathematica 的任意精度路径（部分条目）、NTL 和 CLN 更快。<sup>[17](https://www.mpfr.org/mpfr-2.3.0/timings.html)</sup>

性能的主要杠杆是整数后端。mpmath 的算术建立在 Python 的 `long` 类型之上，这使它不依赖任何 C 扩展；但如果安装了 gmpy2 2.3.0 或更高版本，它会自动且透明地切换到 gmpy2 整数，从而快得多，尤其是在约 100 位精度以上。该切换可用环境变量 `MPMATH_NOGMPY` 禁用，`python-gmp` 扩展则是另一种可选后端。<sup>[8](https://mpmath.readthedocs.io/en/latest/setup.html)</sup><sup> • </sup><sup>[3](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)</sup>

## 局限

项目自己的文档列出了若干已知的主要问题：某些函数在参数极大或极接近奇点时可能返回错误的值；定向舍入对算术运算是精确的，但在其他场合是启发式的，结果可能在末位上差一两个单位；缺少某些 IEEE 754 特性，包括带符号零与非规格化数的舍入，无穷与 NaN 只得到部分支持；切换精度与舍入方式的接口不是线程安全的。<sup>[18](https://github.com/mpmath/mpmath)</sup> 需要精确十进制小数的用户被指引去使用 Python 的 `decimal` 模块或更快的 `fractions` 模块。<sup>[7](https://mpmath.org/doc/current/technical.html)</sup>

## 采用情况

mpmath 是 Sage 的标准组件，也是 SymPy 的数值求值引擎，后者没有它便无法导入。<sup>[1](https://mpmath.org/)</sup><sup> • </sup><sup>[4](https://docs.sympy.org/latest/contributing/dependencies.html)</sup> 项目网站称，截至 2026 年，Google Scholar 在科学文献中列出了 1500 多处对 mpmath 的引用。<sup>[1](https://mpmath.org/)</sup> 当前版本要求 Python 3.10 或更高版本，并已在 CPython 3.10 至 3.15、PyPy 7.3.23 和 GraalPy 25.3 上测试过。<sup>[5](https://github.com/mpmath/mpmath/blob/master/README.rst)</sup>

## References

1. [mpmath: Python library for arbitrary-precision floating-point arithmetic (project website)](https://mpmath.org/)
2. [Fredrik Johansson's website](https://fredrikj.net/)
3. [Fredrik Johansson (2017). mpmath 1.0, and a ten-year retrospective](https://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/)
4. [SymPy 1.14.0 documentation: Dependencies](https://docs.sympy.org/latest/contributing/dependencies.html)
5. [mpmath README.rst (GitHub)](https://github.com/mpmath/mpmath/blob/master/README.rst)
6. [mpmath 1.3.0 documentation: Basic usage](https://www.mpmath.org/doc/current/basics.html)
7. [mpmath 1.3.0 documentation: Precision and representation issues](https://mpmath.org/doc/current/technical.html)
8. [mpmath documentation: Setting up mpmath (Using gmpy2)](https://mpmath.readthedocs.io/en/latest/setup.html)
9. [mpmath/mpmath Releases (GitHub)](https://github.com/mpmath/mpmath/releases)
10. [mpmath 1.4.1 documentation: Basic usage](https://mpmath.readthedocs.io/en/1.4.1/basics.html)
11. [mpmath 1.4.1 documentation: Contexts](https://mpmath.readthedocs.io/en/stable/contexts.html)
12. [mpmath 1.3.0 documentation: Contexts](https://mpmath.org/doc/current/contexts.html)
13. [mpmath 1.3.0 documentation: Mathematical functions](https://www.mpmath.org/doc/current/functions/index.html)
14. [mpmath documentation: Numerical calculus](https://mpmath.readthedocs.io/en/latest/calculus/index.html)
15. [mpmath documentation: Ordinary differential equations](https://mpmath.readthedocs.io/en/latest/calculus/odes.html)
16. [mpmath documentation (PDF): Numerical integration, algorithms](https://mpmath.org/doc/current/mpmath.pdf)
17. [The MPFR Library 2.3.0: Timings, comparison of multiple-precision floating-point software](https://www.mpfr.org/mpfr-2.3.0/timings.html)
18. [mpmath/mpmath on GitHub (Known problems)](https://github.com/mpmath/mpmath)

---
*Initially written Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Arbitrary-precision arithmetic

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/mpmath

English original: https://www.edgepedia.org/mpmath
