Python 的math 模块

2023年7月1日11:06:51

一、math库简介
math库是python提供的内置数学类函数库
math库不支持复数类型,仅支持整数和浮点数运算
math库一共提供了4个数字常数和44个函数,其中44个函数共分为4类,包括16个数值表示函数,8个幂对数函数,16个三角对数函数和4个高等特殊函数
二、数字常数
import math
print(math.pi)
# 圆周率,值为3.141592653589793
print(math.e)
# 自然对数,值为2.718281828459045
print(math.inf)
# 正无穷大,负无穷大为-math.inf
print(math.nan)
# 非浮点数标记,NAN(Not a Number)
三、数值表示函数
import math
print(math.fmod(5,2))           # 返回x与y的模 math.fmod(x,y)
print(math.fsum([5,2,2,2.3]))   # 浮点数精确求和 math.fsum([x,y,...])
print(math.fabs(-999))          # 返回x的绝对值 math.fabs(x)
print(math.ceil(2.3) )          # 向上取整,返回不小于x的最小整 math.ceil(x)
print(math.floor(2.3))          #向下取整,返回不大于x的最大整数 math.floor(x)
print(math.factorial(5))        # 返回x的阶乘,如果x是小数或负数,返回ValueError math.factorial(x)
print(math.gcd(9, 3))           # 返回a与b的最大公约数 math.gcd(a, b)
print(math.frexp(40))            # 返回(m,e),当x=0时,返回(0.0,0) math.frexp(x)
print(math.ldexp(5, 3))         # 返回x*(2的i次方)运算值,math.frexp(x)函数的反运算 math.ldexp(x, i)
print(math.modf(5.5))            # 返回x的小数和整数部分  math.modf(x)
print(math.trunc(-90.3))         # 返回x的整数部分 math.trunc(x)
print(math.copysign(90,-6))      # 用数值y的正负号替换数值x的正负号 默认返回小数点后一位 math.copysign(x,y)
print(math.isclose(20, 20))      # 比较a和b的相似性,返回True或False math.isclose(a, b)
print(math.isfinite(math.inf))   #如果 number 是 NaN(非数字),或者是正、负无穷大的数,则返回 false,否则返回True math.isfinite(x)
print(math.isinf(math.inf))             # 当x为正数或者负数无穷大, 返回True, 否则返回False math.isinf(x)
print(math.isnan(math.nan))# 当x是NaN, 返回True, 否则返回False math.isnan(x)


四、幂对数函数
import math
print(math.pow(2, 3))
# 返回x的y次幂math.pow(x, y)

print(math.exp(2))
# 返回e的x次幂, e是自然对数 math.exp(x)

print('we',math.expm1(2))
# 返回e的x次幂减1 math.expm1(x)

print(math.sqrt(2))
# 返回x的平方根 math.sqrt(x)

print(math.log(10,10))
# 返回x的对数值,只输入x时,返回自然对数,即lnx

print(math.log1p(math.e-1))
# 返回1+x的自然对数值 math.log1p(x)

print(math.log2(4))
# 返回x的2的对数值 math.log2(x)

print(math.log10(100))
# 返回x的10的对数值 math.log10(x)

五、三角运算函数
import math
print(math.degrees(1))
# 角度x的弧度值转角度值 math.degrees(x)

print(math.radians(math.pi))
# 角度x的角度值转弧度值 math.radians(x)

print(math.hypot(1,1))
# 返回(x,y)坐标点到原点(0,0)的距离 math.hypot(x,y)

print(math.sin(0))
# 返回x的正弦函数值,x是弧度值 math.sin(x)

print(math.cos(math.pi))
# 返回x的余弦函数值,x是弧度值 math.cos(x)

print(math.tan(math.pi))
# 返回x的正切函数值,x是弧度值 math.tan(x)

print(math.asin(1))
# 返回x的反正弦函数值,x是弧度值 math.asin(x)

print(math.acos(1))
# 返回x的反余弦函数值,x是弧度值 math.acos(x)

print(math.atan(1))
# 返回x的反正切函数值,x是弧度值 math.atan(x)

print(math.atan2(1,2))
# 返回y/x的反正切函数值,x是弧度值 math.atan2(y, x)

print(math.sinh(math.pi))
# 返回x的双曲正弦函数值,x是弧度值 math.sinh(x)

print(math.cosh(math.pi))
# 返回x的双曲余弦函数值,x是弧度值 math.cosh(x)

print(math.tanh(math.pi))
# 返回x的双曲正切函数值,x是弧度值 math.tanh(x)

print(math.atanh(0.999))
# 返回x的反双曲正切函数值,x是弧度值 math.atanh(x)

print(math.asinh(0.99))
# 返回x的反双曲正弦函数值,x是弧度值 math.asinh(x)

print(math.acosh(1))
# 返回x的反双曲余弦函数值,x是弧度值 math.acosh(x) x -- 必需,一个数字,大于等于 1


六、高等特殊函数
import math
print(math.erf(2))
# 高斯误差函数,应用于概率论,统计学领域
print(math.erfc(2))
# 余数高斯误差函数,math.erfc(x) = 1-math.erf(x)
print(math.gamma(2))
# 伽玛函数,也叫欧拉第二积分函数
print(math.lgamma(2))
# 伽玛函数的自然对数

  • 作者:玖哥说
  • 原文链接:https://blog.csdn.net/j22588/article/details/127110767
    更新时间:2023年7月1日11:06:51 ,共 2686 字。