今天要为大家介绍Python中的——Math库
平时我们都会利用Math库中的函数来解决问题,但是很少有人能了解到所有的函数,今天就让我给大家一一介绍一下。
首先,在使用Math库之前,我们要用import导入该库
方法一:import math
函数采用math.<a>()
形式调用
例如:
方法二:from math import *(好用但是有风险)
函数采用<a>()
形式调用
例如:
Math库共有39个函数:
4个常量函数、16个数值表示函数、8个幂/对数函数、8个三角函数、6个双曲函数、2个角度转换函数和4个特殊函数。
1、常量函数(4个)
函数名 | 说明 | 实例 |
---|---|---|
math.pi | 圆周率pi 3.141592653589793 | >>> math.pi 3.141592653589793 |
math.e | 自然常数e 2.718281828459045 | >>> math.e 2.718281828459045 |
math.inf | 正无穷大 负无穷大为-math.inf | >>> math.inf inf |
math.nan | NaN(Not a Number) 非浮点数标记 | >>> math.nan nan |
math.tau | 常数τ = 6.283185 |
2、数值表示函数(16个)
函数名 | 说明 | 实例 |
---|---|---|
math.ceil(x) | 向上取整,返回不小于x的最小整数 | >>> ceil(4.2) 5 |
math.floor(x) | 向下取整,返回不大于x的最大整数 | >>> floor(4.2) 4 |
math.fabs(x) | 返回x的绝对值 | >>> math.fabs(-0.03) 0.03 |
math.fmod(x,y) | 返回x%y(取余) 其值是一个浮点数 | >>> math.fmod(20,3) 2.0 |
math.trunc(x) | 返回x的整数部分 | >>> math.trunc(4.8) 4 |
math.modf(x) | 返回x的小数和整数 | >>> math.modf(4.8) (0.7999999999999998, 4.0) |
math.gcd(a,b) | 返回a与b的最大公约数 | >>> math.gcd(5,30) 5 |
math.factorial(x) | 返回x的阶乘 如果x是小数或复数,返回ValueError | >>>math.factorial(4) 24 |
math.fsum([x,y,…]) | 浮点数的精确求和 f指float | >>> 0.1+0.2+0.3 0.6000000000000001 >>> math.fsum([0.1, 0.2, 0.3]) 0.6 |
math.frexp(x) | 返回(m,a)。其中x = m * 2^a(m乘以2的a次方)。当x=0时,返回(0.0,0) | >>> math.frexp(3) (0.75, 2) |
math.ldexp(m, a) | 返回m乘以2的a次方 也是math.frepx(x)的反运算 | >>> math.ldexp(0.75, 2) 3.0 |
math.copysign(x, y) | 若y<0,返回-1乘以x的绝对值; 否则,返回x的绝对值 | >>> math.copysign(5.2, -1) -5.2 |
math.isclose(a,b) | 比较a和b的相似性, 返回True或False | |
math.isfinite(x) | 若x既不是无穷大也不是NaN, 则返回True,否则返回False | >>> math.isfinite(3.0) True |
math.isinf(x) | 若x为正负无穷大,返回True; 否则返回False | >>> math.isinf(1.0e+308) False >>> math.isinf(1.0e+309) True |
math.isnan(x) | 若x不是数,返回True, 否则返回False | >>> math.isnan(1.2e3) False |
3、幂/对数函数(8个)
函数名 | 说明 | 实例 |
---|---|---|
math.sqrt(x) | 返回x的平方根 | >>> math.sqrt(4) 2.0 |
math.pow(x,y) | 返回x的y次方 | >>> math.pow(5,3) 125.0 |
math.exp(x) | 返回e的x次方 | >>> math.exp(2) 7.38905609893065 |
math.expm1(x) | 返回e的x次方减1 | >>> math.expm1(2) 6.38905609893065 |
math.log(x[, base]) | 返回x的以base为底的对数 base默认为e | >>> math.log(math.e) 1.0 >>> math.log(2, 10) 0.30102999566398114 |
math.log1p(x) | 返回x+1的自然对数,即ln(x+1) | >>> math.log1p(math.e-1) 1.0 |
math.log2(x) | 返回x的2对数值 | >>> math.log2(2) 1.0 |
math.long10(x) | 返回x的10对数值 | >>> math.log10(10) 1.0 |
4、三角函数(8个)
函数名 | 说明 | 实例 |
---|---|---|
math.hypot(x,y) | 返回从原点到点(x,y)的矢量长度 | >>>math.hypot(3,4) 5.0 |
math.sin(x) | 返回x(弧度)的三角正弦值 | >>>math.sin(math.radians(30)) 0.49999999999999994 |
math.cos(x) | 返回x的余弦值,x为弧度值 | >>>math.cos(math.radians(45)) 0.7071067811865476 |
math.tan(x) | 返回x的正切值,x为弧度值 | >>>math.tan(math.radians(60)) 1.7320508075688767 |
math.asin(x) | 返回x的反正弦值,x为弧度值 | >>> math.asin(0.5) 0.5235987755982989 |
math.acos(x) | 返回x的反余弦值,x为弧度值 | >>> math.acos(math.sqrt(2)/2) 0.7853981633974483 |
math.atan(x) | 返回x的反正切值,x为弧度值 | >>>math.atan(1.7320508075688767) 1.0471975511965976 |
math.atan2(y,x) | 返回y/x的反正切函数值,x是弧度值 | >>> math.atan2(2,1) 1.1071487177940904 |
5、角度转换(2个)
函数名 | 说明 | 实例 |
---|---|---|
math.degrees(x) | 弧度转度 | >>> math.degrees(math.pi) 180.0 |
math.radians(x) | 度转弧度 | >>> math.radians(45) 0.7853981633974483 |
6、双曲函数(6个)
函数名 | 说明 |
---|---|
math.sinh(x) | 返回x的双曲正弦函数 |
math.asinh(x) | 返回x的反双曲正弦函数 |
math.cosh(x) | 返回x的双曲余弦函数 |
math.acosh(x) | 返回x的反双曲余弦函数 |
math.tanh(x) | 返回x的双曲正切函数 |
math.atanh(x) | 返回x的反双曲正切函数 |
7、特殊函数(4个)
函数名 | 说明 |
---|---|
math.erf(x) | 返回x的误差函数 |
math.erfc(x) | 返回x的余误差函数 |
math.gamma(x) | 返回x的伽玛函数 |
math.lgamma(x) | 返回x的绝对值的自然对数的伽玛函数 |