Python 中的 math
库提供了一些数学函数,可以计算常用的数学运算,如三角函数、对数、幂等。使用方法:
- 导入库:
import math
- 计算平方根:
math.sqrt(x)
,参数为正数x
。 - 计算指数:
math.exp(x)
,参数为实数x
。 - 计算以 e 为底的对数:
math.log(x[, base])
,参数为正数x
,可选参数为底数base
(默认为 e)。 - 计算三角函数:
math.sin(x)
、math.cos(x)
、math.tan(x)
,参数为弧度x
。
示例:
import math
# 计算平方根
result = math.sqrt(16)
print