math是python的内置模块,主要提供了常用的数学函数。该模块默认数据类型为浮点型,不支持复数(复数计算可以使用cmath).
那么math提供了哪些函数呢 ? 下面总结一下math中的所有函数已经在python3.9中的更新。
一 数论函数类
math.ceil(x) 向上取整, 返回大于等于x的最小整数
import math
math.ceil(2.5) # ceiling 天花板,上限
3.0
math.floor(x) 向下取整,返回小于等于x的最大整数
math.floor(2.5)
2.0
math.fabs(x) 绝对值
math.fabs(-1)
1.0
math.factorial(n) 阶乘
math.factorial(3) # factorial 阶乘
6.0
math.comb(n,k) 计算组合数, 从n个不相同的样本中选取k个的取法个数 ; 在python 3.8中新增。
math.comb(4,2) # combination 组合
>>>6.0
math.perm(n, k) 排列数函数, 在python3.8更新, 计算排列数也可以使用A(n,k) = C(n,k)* k! ,
math.perm(4,2) # permutation 排列
12.0
math.comb(4,2)* math.factorial(2)
12.0
math.copysign(x,y) , sign即符号,返回x乘以y的符号
math.copysign(2,-1)
-2.0
math.fmod(x,y) ,返回x除以y的余数
math.fmod(5,3)
2.0
math.frexp(x) 把浮点数 x 分解成尾数和指数,。返回(m,e)
math.frexp(8)
(0.5, 4) # 0.5* 2**4 = 8
math.ldexp(m, e), 返回的值, 是math.frexp()的逆函数
math.ldexp(0.5, 4)
8.0
math.fsum() 返回可迭代序列的累加值
math.fsum([1,2,3])
6.0
math.prod() 计算可迭代序列的乘积,只支持数值型数据
math.prod([1,5,6]) # product 积
30.0
math.gcd(x,y) , 返回x, y的最大公约数(greatest common divisor)
math.gcd(4,8)
4.0
math.lcm(x,y), 返回x,y 的最小公倍数(least common multiple) , python3.9 中更新,在3.9版本之前math.gcd()与math.lcm只能计算两个数,3.9可以计算任意个数的gcd或lcm。
math.lcm(4,8)
8.0
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) , 判断a,b是否接近, rel_rol为相对误差, abs_tol为绝对误差。若给定一个误差,|a-b|小于等于该误差返回True, 否则返回False ;若给定两个误差, 小于等于任意一个误差返回True,否则False.
math.isclose(1,2,abs_tol=1.1)
True
math.isclose(1,2,rel_tol=0.5)
True
math.isclose(1,2,abs_tol=0.1,rel_tol=0.4)
False
math.infinite(x), 如果x既不是无穷大也不是NaN,则返回True
math.isinfinite(100)
True
math.infinite(math.nan)
False
math.isinf(x),如果x是正无穷大或负无穷大,则返回True,否则返回False
math.isinf(math.inf)
True
math.isinf(math.nan)
math.isnan(),如果x是NaN(不是数字),返回True,否则返回False。
math.isnan(math.inf)
False
math.isqrt(n), n平方根的向下取整, 等价于 math.floor(math.sqrt(n) )
math.isqrt(6)
2.0
math.modf(x) ,返回x的小数部分和整数部分, 这两个结果都带有x的符号并且是浮点数。
math.modf(-1.5)
(-0.5, -1.0)
math.trunc(x) 返回x的整数部分
math.trunc(-1)
-1
math.nextafter(x,y), 返回x向y的下一个浮点值。
- math.nextafter(x, math.inf) 上升,趋向正无穷
- math.nextafter(x, -math.inf)递减,趋近于负无穷
- math.nextafter(x, 0.0) 趋向于0
- math.nextafter(x, math.copysign(math.inf, x))从0开始
math.nextafter(4, 5)
4.000000000000001
math.ulp() ,返回最低精度单位大小 ;
- 如果x是NaN(不是数字),则返回x
- 如果x是负数,返回值与math.ulp(-x)相同。
- 如果x是正无穷,返回x
- 如果x等于0,返回最小的正非规范化可表示浮点数(小于最小的正规范化浮点数,sys.float_info.min)。
- 如果x等于最大的正可表示浮点数,则返回x的最小有效位的值,以便小于x的第一个浮点数是x - ulp(x)。
- 否则(x为正有限数),返回x的最低精度单位,使第一个大于x的浮点数为x + ulp(x)。
math.remainder(x,y) # 不明白这个函数用法,清楚的请告诉我
二 基本初等函数
语法 | 函数 |
math.sqrt(x) | x的平方根 |
math.exp(x) | 以e为底的指数函数 |
math.pow(x, y) | |
math.expm1(x) | 等于math.exp(x)-1 |
math.log(x[, base]) | 若一个参数 log(x),以e为底的对数函数 |
math.log1p(x) | |
math.log2(x) | |
math.log10(x) | |
math.sin(x) | 正弦函数 |
math.cos(x) | 余弦函数 |
math.tan(x) | 正切函数 |
math.asin(x) | 反正弦函数 |
math.acos(x) | 反余弦函数 |
math.atan(x) | 反正切函数 |
math.sinh(x) | 双曲正弦函数 |
math.cosh(x) | 双曲余弦函数 |
math.tanh(x) | 双曲正切函数 |
math.asinh(x) | 反双曲正弦函数 |
math.acosh(x) | 反双曲余弦函数 |
math.atanh(x) | 反双曲正切函数 |
math.atan2(y, x) | 返回从原点(0,0)到(x,y)点的线段与x轴正方向之间的平面角度(弧度值) |
math.dist(p, q) | 返回两点p和q之间的欧几里得距离,每个点都给出一个序列(或可迭代)。这两个点必须有相同的维数。 |
math.hypot(*coordinates) | 返回欧几里得范数, 版本3.8中增加了对n维点的支持。以前只支持二维情况。 |
math.atan2(1,1)==math.pi/4
True
math.dist([1,1],[0,1])
1.0
math.hypot(3,4)
5.0
三 角度转换
math.degrees(x),把角x从弧度转换成角度。
math.radians(x),把角x从角度转换成弧度。
math.degrees(math.pi)
180.0
math.radians(90)
1.5707963267948966 # pi/2
四 特殊函数
math.erf(x) 误差函数, 计算
**math.rerf(x)**互补误差函数。计算
math.gamma(x) 伽马函数
**math.lgamma(x)**返回x的绝对值的自然对数的伽玛函数
五 常量
语法 | 常量 |
math.pi | π = 3.141592… |
math.e | e = 2.718281 |
math.tau | τ = 6.283185… |
math.inf | 正无穷大 |
math.nan | “not a number” (NaN) |
这里对math模块的所有函数做一总结,以便查阅。