math库引用方式
1.import math
对math库中函数采用math.<函数名>()形式使用
import math
a=math.ceil(10.2) #向上取整
print(a)
2.from math import <函数名>
对 math 库中函数可以直接采用 <函数名>() 形式使用
from math import floor
a=floor(10.2) #向下取整
print(a)
math库数字常数
常数 | 描述 |
---|---|
math.pi | 圆周率,值为 3.141 592 653 589 793 |
math.e | 自然对数,值为 2.718 281 828 459 045 |
math.inf | 正无穷大,负无穷大为 - math.inf |
math.nan | 非浮点数标记,NaN (Not a Number) |
math库数值表示函数
函数 描述
math.fabs(x) 返回 x 的绝对值
math.fmod(x,y) 返回 x 与 y 的摸
math.fsum([x,y,…]) 浮点数精确求和
math.ceil(x) 向上取整,返回不小于 x 的最小整数
math.floor(x) 向下取整,返回不大于 x 的最大整数
math.factorial(x) 返回 x 的阶乘,如果 x 是小数或负数,返回 ValueError
math.gcd(a,b) 返回 a 与 b 的最大公约数
math.frexp(x) 表示 x = m*2e,返回(m,e),当 x =0,返回(0.0,0)
math.ldexp(x,i) 返回 x*2i 运算值,math.frexp(x) 函数的反运算
math.modf(x) 返回 x 的小数和整数部分
math.trunc(x) 返回 x 的整数部分
math.copysign(x,y) 用数值 y 的正负号替换数值 x 的正负号
math.isclose(a,b) 比较 a 和 b 的相似性,返回 True 或 False
math.isfinite(x) 当 x 不是无穷大或 NaN,返回 True;否则,返回 False
math.isinf(x) 当 x 为正负无穷大,返回 True;否则,返回 False
math.isnan(x) 当 x 是 NaN,返回 True;否则返回 False
math库的幂对数函数
函数 数学表示 描述
math.pow(x,y) xy 返回 x 的 y 次幂
math.exp(x) ex 返回 e 的 x 次幂,e 是自然对数
math.expml(x) ex-1 返回 e 的 x 次幂减 1
math.sqrt(x) √x 返回 x 的平方根
math.log(x[,base]) logbasex 返回 x 的对数值,只输入 x 时,返回自然对数,即 ln x
math.log1p(x) ln(1+x) 返回 1+x 的自然对数值
math.log2(x) log2x 返回 x 的 2 对数值
math.log10(x) log10x 返回 x 的 10 对数值
math库的三角运算函数
函数 数学表示 描述
math.degrees(x) 无 角度 x 的弧度值转角度值
math.radians(x) 无 角度 x 的角度值转弧度值
math.hypot(x,y) √x2+y2 返回 (x,y) 坐标到原点的距离
math.sin(x) sin x 返回 x 的正弦函数值,x 是弧度值
math.cos(x) cos x 返回 x 的余弦函数值,x 是弧度值
math.tan(x) tan x 返回 x 的正切函数值,x 是弧度值
math.asin(x) arcsin x 返回 x 的反正弦函数值,x 是弧度值
math.acos(x) arccos x 返回 x 的反余弦函数值,x 是弧度值
math.atan(x) arctan x 返回 x 的反正切函数值,x 是弧度值
math.atan2(x,y) arctan y/x 返回 y/x 的反正切函数值,x 是弧度值
math.sinh(x) sinh x 返回 x 的双曲正弦函数值
math.cosh(x) cosh x 返回 x 的双曲余弦函数值
math.tanh(x) tanh x 返回 x 的双曲正切函数值
math.asinh(x) atcsinh x 返回 x 的反双曲正弦函数值
math.acosh(x) arccosh x 返回 x 的反双曲余弦函数值
math.atanh(x) arctanh x 返回 x 的反双曲正切函数值