Python math 模块

Python math.cos() 函数用于计算余弦,参数是弧度。

语法

math.cos(x)

参数

x:弧度值。

返回值

返回 x 的余弦值(float)。

示例

import math

print(math.cos(0))              # 1.0
print(math.cos(math.pi))        # -1.0,180度的余弦
print(math.cos(math.radians(60)))  # 0.5,60度

输出:

1.0
-1.0
0.5000000000000001

注意事项

cos 参数为弧度。float 精度会产生微小误差,比较时用近似。