Latest web development tutorials

Python cos() 函數

Python 數字 Python數字


描述

cos()返回x的弧度的餘弦值。


語法

以下是cos() 方法的語法:

import math

math.cos(x)

注意: cos()是不能直接訪問的,需要導入math模塊,然後通過math靜態對象調用該方法。


參數

  • x -- 一個數值。

返回值

返回x的弧度的餘弦值,-1 到1 之間。


實例

以下展示了使用cos() 方法的實例:

#!/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

Python 數字 Python數字