# Python数学库

Canonical: https://www.mirstar.net/2025/10/09/Python-2-math/
Author: onlymatt
Published: 2025-10-09T11:32:58.000Z
Updated: 2026-05-17T11:52:03.044Z
Categories: Python
Tags: Python

# Python数学库
Python提供了两个主要的数学库：`math`和`cmath`。`math`库用于处理实数的数学运算，而`cmath`库用于处理复数的数学运算。
## math库
`math`库提供了许多常用的数学函数和常量。以下是一些常用的函数和常量：
- `math.sqrt(x)`：计算平方根。
- `math.sin(x)`：计算正弦值，x为弧度。  
- `math.cos(x)`：计算余弦值，x为弧度。
- `math.tan(x)`：计算正切值，x为弧度。
- `math.log(x, base)`：计算以base为底的对数，默认底为e。
- `math.exp(x)`：计算e的x次幂。
- `math.pi`：圆周率π的值。
- `math.e`：自然常数e的值。
### 示例代码
```python
import math
# 计算平方根
print(math.sqrt(16))  # 输出: 4.0
# 计算正弦值
print(math.sin(math.pi / 2))  # 输出: 1.0
# 计算以10为底的对数        
print(math.log(100, 10))  # 输出: 2.0
# 计算e的2次幂
print(math.exp(2))  # 输出: 7.38905609893065
# 输出常量π和e
print(math.pi)  # 输出: 3.141592653589793
print(math.e)   # 输出: 2.718281828459045
```
## cmath库
`cmath`库用于处理复数的数学运算。它提供了类似于`math`库的函数，但适用于复数。以下是一些常用的函数：
- `cmath.sqrt(x)`：计算复数的平方根。
- `cmath.sin(x)`：计算复数的正弦值。
- `cmath.cos(x)`：计算复数的余弦值。
- `cmath.tan(x)`：计算复数的正切值。
- `cmath.log(x, base)`：计算复数的对数，默认底为e。
- `cmath.exp(x)`：计算e的复数次幂。
### 示例代码
```python
import cmath
# 计算复数的平方根
print(cmath.sqrt(-16))  # 输出: 4j
# 计算复数的正弦值
print(cmath.sin(1 + 2j))  # 输出: (1.403    1192506220405+0.489056259041293j)
# 计算复数的对数
print(cmath.log(1 + 2j))  # 输出: (0.8047189562170503+1.1071487177940904j)
# 计算e的复数次幂
print(cmath.exp(1 + 2j))  # 输出: (-1.1312043837568135+2.4717266720048188j)
```
以上就是Python中`math`和`cmath`库的基本使用方法。通过这些函数和常量，可以方便地进行各种数学运算。

# 函数表格


## math库函数表格
| 函数/常量         | 描述                             | 示例                      | 输出                     |
|------------------|--------------------------------|-------------------------|------------------------|
| `math.sqrt(x)`   | 计算平方根                        | `math.sqrt(16)`          | `4.0`                  |
| `math.sin(x)`    | 计算正弦值，x为弧度               | `math.sin(math.pi / 2)` | `1.0`                  |
| `math.cos(x)`    | 计算余弦值，x为弧度               | `math.cos(0)`            | `1.0`                  |
| `math.tan(x)`    | 计算正切值，x为弧度               | `math.tan(math.pi / 4)` | `1.0`                  |
| `math.asin(x)`   | 计算反正弦值，返回值为弧度          | `math.asin(1)`           | `1.5707963267948966`   |
| `math.acos(x)`   | 计算反余弦值，返回值为弧度          | `math.acos(1)`           | `0.0`                  |
| `math.atan(x)`   | 计算反正切值，返回值为弧度          | `math.atan(1)`           | `0.7853981633974483`  |
| `math.atan2(y, x)` | 计算点(y, x)的反正切值，返回值为弧度 | `math.atan2(1, 1)`      | `0.7853981633974483`  |
| `math.sinh(x)`  | 计算双曲正弦值                     | `math.sinh(1)`           | `1.1752011936438014`   |
| `math.cosh(x)`  | 计算双曲余弦值                     | | `math.cosh(1)`           | `1.5430806348152437`   |
| `math.tanh(x)`  | 计算双曲正切值                     | | `math.tanh(1)`           | `0.7615941559557649`
| `math.log(x, base)` | 计算以base为底的对数，默认底为e      | `math.log(100, 10)`     | `2.0`                  |
| `math.exp(x)`    | 计算e的x次幂                    | `math.exp(2)`            | `7.389    05609893065` |
| `math.pi`       | 圆周率π的值                       | `math.pi`               | `3.141592653589793`     |
| `math.e`        | 自然常数e的值                      | `math.e`                | `2.718281828459045`    |
| `math.abs(x)`   | 计算绝对值                        | `math.abs(-5)`           | `5`                    |
| `math.factorial(x)` | 计算阶乘                          | `math.factorial(5)`     | `120`                  |
| `math.pow(x, y)` | 计算x的y次幂                     | `math.pow(2, 3)`        | `8.0`                  |
| `math.ceil(x)`  | 向上取整                          | `math.ceil(4.2)`        | `5`                    |
| `math.floor(x)` | 向下取整                          | `math.floor(4.8)`       | `4`                    |
| `math.gcd(x, y)` | 计算最大公约数                     | `math.gcd(48, 18)`      | `6`                    |
| `math.lcm(x, y)` | 计算最小公倍数                     | `math.lcm(4, 5)`        | `20`                   |
| `math.degrees(x)` | 弧度转换为角度                     | `math.degrees(math.pi)` | `180.0`                |
| `math.radians(x)` | 角度转换为弧度                     | `math.radians(180)`      | `3.141592653589793`     |
| `math.hypot(x, y)` | 计算直角三角形的斜边长度               | `math.hypot(3, 4)`      | `5.0`                  |
| `math.isqrt(x)` | 计算整数平方根                      | `math.isqrt(16)`        | `4`                    |
| `math.copysign(x, y)` | 将y的符号赋给x                   | `math.copysign(3, -1)`  | `-3.0`                 |
| `math.fmod(x, y)` | 计算x除以y的余数                   | `math.fmod(5, 2)`       | `1.0`                  |
| `math.trunc(x)` | 截断小数部分，返回整数部分              | `math.trunc(4.7)`       | `4`                    |
| `math.modf(x)` | 返回x的小数部分和整数部分               | `math.modf(4.7)`        | `(0.6999999999999993, 4.0)` |
| `math.round(x, n)` | 将x四舍五入到n位小数               | `math.round(4.675, 2)`  | `4.67`                 |
| `math.nan` | 表示“不是一个数字”（NaN）            | `math.nan`              | `nan`                  |
| `math.inf` | 表示正无穷大                        | `math.inf`              | `inf`                  |
| `math.isfinite(x)` | 检查x是否为有限数                   | `math.isfinite(5)`      | `True`                 |
| `math.isinf(x)` | 检查x是否为正无穷大或负无穷大          | `math.isinf(math.inf)`  | `True`                 |
| `math.isnan(x)` | 检查x是否为NaN                     | `math.isnan(math.nan)`  | `True`                 |
| `math.nextafter(x, y)` | 返回x向y方向的下一个浮点数         | `math.nextafter(1.0, 2.0)` | `1.0000000000000002`   |
| `math.ulp(x)` | 返回x的单位在最后位置（ULP）         | `math.ulp(1.0)`         | `2.220446049250313e-16` |
| `math.frexp(x)` | 将x分解为尾数和指数               | `math.frexp(8)`         | `(0.5, 4)`             |
| `math.ldexp(m, e)` | 计算m * (2**e)               | `math.ldexp(0.5, 4)`    | `8.0`                  |
| `math.gamma(x)` | 计算伽马函数                      | `math.gamma(5)`         | `24.0`                 |



## cmath库函数表格
| 函数/常量         | 描述                             | 示例                      | 输出                     |
|------------------|--------------------------------|-------------------------|------------------------|
| `cmath.sqrt(x)`  | 计算复数的平方根                   | `cmath.sqrt(-16)`        | `4j`                   |
| `cmath.sin(x)`   | 计算复数的正弦值                   | `cmath.sin(1 + 2j)`     | `(1.4031192506220405+0.489056259041293j)` |
| `cmath.cos(x)`   | 计算复数的余弦值                   | | `cmath.cos(1 + 2j)`     | `(2.032723007019351+3.0518977991518j)` |
| `cmath.tan(x)`   | 计算复数的正切值                   | `cmath.tan(1 + 2j)`     | `(-0.0037640256415042486-1.0032386273536096j)` |
| `cmath.log(x, base)` | 计算复数的对数，默认底为e          | `cmath.log(1 + 2j)`     | `(0.8047189562170503+1.1071487177940904j)` |
| `cmath.exp(x)`   | 计算e的复数次幂                  | `cmath.exp(1 + 2j)`     | `(-1.1312043837568135+2.4717266720048188j)` |     
| `cmath.pi`      | 复数圆周率π的值                    | `cmath.pi`              | `(3.141592653589793+0j)` |
| `cmath.e`       | 复数自然常数e的值                   | `cmath.e`               | `(2.718281828459045+0j)` |
