目 录CONTENT

文章目录

Pyhton-math库常用函数

~梓
2025-04-06 / 0 评论 / 0 点赞 / 10 阅读 / 0 字
温馨提示:
部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
import math

# 常量
print("圆周率 pi:", math.pi)  # 输出: 圆周率 pi: 3.141592653589793
print("自然常数 e:", math.e)  # 输出: 自然常数 e: 2.718281828459045
print("圆常数 tau:", math.tau)  # 输出: 圆常数 tau: 6.283185307179586
print("正无穷大:", math.inf)  # 输出: 正无穷大: inf
print("非数字:", math.nan)  # 输出: 非数字: nan

# 数值表示函数
print("\n数值表示函数:")
print("向上取整 math.ceil(3.2):", math.ceil(3.2))  # 输出: 向上取整 math.ceil(3.2): 4
print("向下取整 math.floor(3.8):", math.floor(3.8))  # 输出: 向下取整 math.floor(3.8): 3
print("截断小数 math.trunc(3.9):", math.trunc(3.9))  # 输出: 截断小数 math.trunc(3.9): 3
print("绝对值 math.fabs(-3.5):", math.fabs(-3.5))  # 输出: 绝对值 math.fabs(-3.5): 3.5
print("取模 math.fmod(5, 2):", math.fmod(5, 2))  # 输出: 取模 math.fmod(5, 2): 1.0
print("分离整数和小数 math.modf(3.7):", math.modf(3.7))  # 输出: 分离整数和小数 math.modf(3.7): (0.7, 3.0)

# 幂对数函数
print("\n幂对数函数:")
print("幂运算 math.pow(2, 3):", math.pow(2, 3))  # 输出: 幂运算 math.pow(2, 3): 8.0
print("平方根 math.sqrt(9):", math.sqrt(9))  # 输出: 平方根 math.sqrt(9): 3.0
print("指数运算 math.exp(1):", math.exp(1))  # 输出: 指数运算 math.exp(1): 2.718281828459045
print("自然对数 math.log(math.e):", math.log(math.e))  # 输出: 自然对数 math.log(math.e): 1.0
print("以10为底的对数 math.log10(100):", math.log10(100))  # 输出: 以10为底的对数 math.log10(100): 2.0
print("以2为底的对数 math.log2(8):", math.log2(8))  # 输出: 以2为底的对数 math.log2(8): 3.0

# 三角函数
print("\n三角函数:")
print("正弦值 math.sin(math.pi / 2):", math.sin(math.pi / 2))  # 输出: 正弦值 math.sin(math.pi / 2): 1.0
print("余弦值 math.cos(math.pi):", math.cos(math.pi))  # 输出: 余弦值 math.cos(math.pi): -1.0
print("正切值 math.tan(math.pi / 4):", math.tan(math.pi / 4))  # 输出: 正切值 math.tan(math.pi / 4): 1.0
print("反正弦值 math.asin(1):", math.asin(1))  # 输出: 反正弦值 math.asin(1): 1.5707963267948966
print("反余弦值 math.acos(0):", math.acos(0))  # 输出: 反余弦值 math.acos(0): 1.5707963267948966
print("反正切值 math.atan(1):", math.atan(1))  # 输出: 反正切值 math.atan(1): 0.7853981633974483
print("atan2 math.atan2(1, 1):", math.atan2(1, 1))  # 输出: atan2 math.atan2(1, 1): 0.7853981633974483

# 角度和弧度转换函数
print("\n角度和弧度转换函数:")
print("弧度转角度 math.degrees(math.pi / 2):", math.degrees(math.pi / 2))  # 输出: 弧度转角度 math.degrees(math.pi / 2): 90.0
print("角度转弧度 math.radians(180):", math.radians(180))  # 输出: 角度转弧度 math.radians(180): 3.141592653589793

# 双曲函数
print("\n双曲函数:")
print("双曲正弦值 math.sinh(0):", math.sinh(0))  # 输出: 双曲正弦值 math.sinh(0): 0.0
print("双曲余弦值 math.cosh(0):", math.cosh(0))  # 输出: 双曲余弦值 math.cosh(0): 1.0
print("双曲正切值 math.tanh(0):", math.tanh(0))  # 输出: 双曲正切值 math.tanh(0): 0.0
print("反双曲正弦值 math.asinh(0):", math.asinh(0))  # 输出: 反双曲正弦值 math.asinh(0): 0.0
print("反双曲余弦值 math.acosh(1):", math.acosh(1))  # 输出: 反双曲余弦值 math.acosh(1): 0.0
print("反双曲正切值 math.atanh(0):", math.atanh(0))  # 输出: 反双曲正切值 math.atanh(0): 0.0

# 特殊函数
print("\n特殊函数:")
print("阶乘 math.factorial(5):", math.factorial(5))  # 输出: 阶乘 math.factorial(5): 120
print("最大公约数 math.gcd(12, 18):", math.gcd(12, 18))  # 输出: 最大公约数 math.gcd(12, 18): 6
print("比较接近 math.isclose(1.000000001, 1):", math.isclose(1.000000001, 1))  # 输出: 比较接近 math.isclose(1.000000001, 1): True
print("是否有限数 math.isfinite(3.5):", math.isfinite(3.5))  # 输出: 是否有限数 math.isfinite(3.5): True
print("是否无穷大 math.isinf(math.inf):", math.isinf(math.inf))  # 输出: 是否无穷大 math.isinf(math.inf): True
print("是否非数字 math.isnan(math.nan):", math.isnan(math.nan))  # 输出: 是否非数字 math.isnan(math.nan): True

# Python 3.8 及之后新增的函数
print("\nPython 3.8 及之后新增的函数:")
# math.prod() 计算可迭代对象中元素的乘积
numbers = [2, 3, 4]
print("math.prod() 计算列表元素的乘积:", math.prod(numbers))  # 输出: math.prod() 计算列表元素的乘积: 24
# math.isqrt() 返回非负整数的整数平方根
print("math.isqrt() 计算整数平方根:", math.isqrt(16))  # 输出: math.isqrt() 计算整数平方根: 4
  
0

评论区