math.cos() 回傳 x(弧度)的餘弦值,結果介於 -1 到 1 之間。本文說明語法、參數、回傳值與範例。
import math
math.cos(x)
回傳餘弦值(浮點數),範圍 -1 到 1。
#!/usr/bin/python
import math
print "cos(3) : ", math.cos(3)
print "cos(-3) : ", math.cos(-3)
print "cos(0) : ", math.cos(0)
print "cos(math.pi) : ", math.cos(math.pi)
print "cos(2*math.pi) : ", math.cos(2*math.pi)
cos(3) : -0.9899924966
cos(-3) : -0.9899924966
cos(0) : 1.0
cos(math.pi) : -1.0
cos(2*math.pi) : 1.0
提示: 若角度為度數,需先用 math.radians() 轉換為弧度。