首先在输入代码前先导入math函数库:import math
>>>print(math.e)
2.718281828459045
>>> print(math.pi)
3.141592653589793
>>> print(math.degrees(math.pi))
180.0
>>>print(math.radians(45))
0.7853981633974483
>>> print(math.exp(2))
7.38905609893065
>>>print(math.expm1(2))
6.38905609893065
math.log(x[, base]) | 返回x的以base为底的对数,base默认为e |
---|
>>> print(math.log(math.e))
1.0
>>> print(math.log(2, 10))
0.30102999566398114
math.log10(x) | 返回x的以10为底的对数 |
---|
>>> print(math.log10(2))
0.30102999566398114
math.log1p(x) | 返回1+x的自然对数(以e为底) |
---|
>>> print(math.log1p(math.e-1))
1.0
>>> print(math.pow(5,3))
125.0
>>> print(math.sqrt(3))
1.7320508075688772
>>> print(math.ceil(5.2))
6.0
>>> print(math.floor(5.8))
5.0
>>> print(math.trunc(5.8))
5
>>> print(math.modf(5.2))
(0.20000000000000018, 5.0)
>>> print(math.abs(-5))
5.0
>>> print(math.fmod(5,2))
1.0
math.fsum([x, y, …]) | 返回无损精度的和 |
---|
>>> print(0.1+0.2+0.3)
0.6000000000000001
>>> print(math.fsum([0.1, 0.2, 0.3]))
0.6
>>> print(math.factorial(5))
120
math.isinf(x) | 若x为无穷大,返回True;否则,返回False |
---|
>>> print(math.isinf(1.0e+308))
False
>>> print(math.isinf(1.0e+309))
True
math.isnan(x) | 若x不是数字,返回True;否则,返回False |
---|
>>> print(math.isnan(1.2e3))
False
math.hypot(x, y) | 返回以x和y为直角边的斜边长 |
---|
>>>print(math.hypot(3,4))
5.0
math.copysign(x, y) | 若y<0返回-1乘以x的绝对值;否则返回x的绝对值 |
---|
>>> print(math.copysign(5.2, -1))
-5.2
math.ldexp(m, i) | th.ldexp(m, i) 返回m乘以2的i次方 |
---|
>>> print(math.ldexp(0.75, 2))
3.0
>>>print(math.sin(45))
0.8509035245341184
>>> print(math.asin(0.5))
0.5235987755982989
>>> print(math.cos(math.radians(45)))
0.7071067811865476
>>> print(math.acos(math.sqrt(2)/2))
0.7853981633974483
>>> print(math.tan(math.radians(60)))
1.7320508075688767
>>> print(math.atan(1.7320508075688767))
1.0471975511965976
math.atan2(x, y) | 返回x/y的反三角正切值 |
---|
>>> print(math.atan2(2,1))
1.1071487177940904
以下函数不常见用的也比较少,这里就不用代码一一举例,类似上面的三角函数就可以。
math.sinh(x) 返回x的双曲正弦函数
math.asinh(x) 返回x的反双曲正弦函数
math.cosh(x) 返回x的双曲余弦函数
math.acosh(x) 返回x的反双曲余弦函数
math.tanh(x) 返回x的双曲正切函数
math.atanh(x) 返回x的反双曲正切函数
math.erf(x) 返回x的误差函数
math.erfc(x) 返回x的余误差函数
math.gamma(x)返回x的伽玛函数
math.lgamma(x)返回x的绝对值的自然对数的伽玛函数