![4edfe299641a15e5f4bdafff60da057d.png](https://i-blog.csdnimg.cn/blog_migrate/120b044932cbfb3fda07183f8445a247.png)
![276796b3fa8d443b2bea014a2c889733.png](https://i-blog.csdnimg.cn/blog_migrate/f60d20c38d21c9b9fac979b1d21305a2.png)
math包定义了两个常数,很多数字运算中,我们都会用到一些特别的常量,例如 圆周率π (pi)和自然常数e:
math.e # 自然常数e
math.pi # 圆周率pi
# 导入函数;
>>> import math
>>> math.e
2.718281828459045
>>> math.pi
3.141592653589793
1Math包的各种运算符:
# 向上取整;
>>> math.ceil(1.01)
2
# 向下取整;
>>> math.floor(1.01)
1
# 指数运算;
>>> math.pow(2,3)
8.0
# 对数,默认以e为基数;
>>> math.log(10)
2.302585092994046
>>> math.log(100, 10)
2.0
# 开根号;
>>> math.sqrt(10)
3.1622776601683795
# 绝对值;
>>> math.fabs(-1.1)
1.1
2角度转换
# 把x从弧度转换成角度(Degrees(x));
>>> math.degrees(math.pi)
180.0
>>> math.degrees(math.pi/4)
45.0
>>> math.degrees(math.pi/2)
90.0
# 将x角度转换为弧度(randian(x))
>>> math.radians(3)
0.05235987755982989
# 求x的正弦值(sin(x));
>>> math.sin(math.pi/4)
0.7071067811865476
>>> math.sin(math.pi/2)
1.0
3三角函数
# 求x 的余弦值(cos(x));
>>> math.cos(math.pi/2)
6.123233995736766e-17
>>> math.cos(math.pi/4)
0.7071067811865476
# 求x 的正切值;
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257
以上是math函数一些简单的使用方法。当然,它还有很多其他的用法,如:
4双曲线函数
# 返回 x 的反双曲余弦值;
>>> math.acosh(x)
# 返回 x 的反双曲正弦值;
>>> math.asinh(x)
# 返回 x 的反双曲正切值;
>>> math.atanh(x)
# 返回 x 的双曲余弦值;
>>> math.cosh(x)
# 返回 x 的双曲正弦值;
>>> math.sinh(x)
# 返回 x 的双曲正切值;
>>> math.tanh(x)
除了以上这些,还有特殊函数(math.erf(x),math.erfc(x),math.gamma(x)...)等更多的函数,包括上面已介绍的,它们也还包含其它的使用方法。
感兴趣的可以参考官网进行学习:
https://docs.python.org/zh-cn/3/library/math.html#math.acosh