Python math.cos()方法 (Python math.cos() method)
math.cos() method is a library method of math module, it is used to get the cosine of the number radians, it accepts a number returns the cosine of the given number radians.
math.cos()方法是数学模块的库方法,用于获取弧度数的余弦,它接受一个数字,返回给定弧度的余弦。
Note: math.cos() method accepts the only number, if we provide anything else except the number, it returns error TypeError - "TypeError: a float is required".
注意: math.cos()方法接受唯一的数字,如果我们提供除数字以外的其他任何内容,它将返回错误TypeError- “ TypeError:需要浮点数” 。
Syntax of math.cos() method:
math.cos()方法的语法:
math.cos(x)
Parameter(s): x – is the number whose cosine to be calculated.
参数: x –是要计算其余弦的数字。
Return value: float – it returns a float value that is the cosine value of the number x radians.
返回值: float-它返回一个浮点值,该值是数字x弧度的余弦值。
Example:
例:
Input:
a = 0.278
# function call
print(math.cos(a))
Output:
0.9616062271291891
Python代码演示math.cos()方法的示例 (Python code to demonstrate example of math.cos() method)
# python code to demonstrate example of
# math.cos() method
# importing math module
import math
# number
a = -1
print("cos(",a,") is = ", math.cos(a))
a = 0
print("cos(",a,") is = ", math.cos(a))
a = 0.278
print("cos(",a,") is = ", math.cos(a))
a = 1
print("cos(",a,") is = ", math.cos(a))
Output
输出量
cos( -1 ) is = 0.5403023058681398
cos( 0 ) is = 1.0
cos( 0.278 ) is = 0.9616062271291891
cos( 1 ) is = 0.5403023058681398
TypeError example
TypeError示例
# python code to demonstrate example of
# math.cos() method with an exception
# importing math module
import math
# number
a = "2"
print("cos(",a,") is = ", math.cos(a))
Output
输出量
Traceback (most recent call last):
File "/home/main.py", line 9, in <module>
print("acos(",a,") is = ", math.acos(a))
TypeError: a float is required
翻译自: https://www.includehelp.com/python/math-cos-method-with-example.aspx