Python math.tan()方法
Pythonmath.tan()
方法是计算一个数字在弧度上的正切的有效方法。输入的参数必须是弧度。
Pythonmath.tan()
方法的语法
math.tan(x)
参数
x |
任何要操作的正或负弧度值。 |
返回值
该方法返回一个浮点数,代表x
的正切值(弧度)。
例1:在Python中使用math.tan()
方法
import math
x=90
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
x=0
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
x=-34.5
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
x=math.pi
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
输出:
The tangent of 90 is -1.995200412208242.
The tangent of 0 is 0.0.
The tangent of -34.5 is 0.057582706804865574.
The tangent of 3.141592653589793 is -1.2246467991473532e-16.
注意,参数可以是整数或浮点数。数值可以是正的,也可以是负的。
例 2: 使用math.tan()
方法得到度数的正切值
import math
print("The tangent of 30 degrees is ", math.tan(math.radians(30)))
输出:
The tangent of 30 degrees is 0.5773502691896257
我们在与几何学有关的数学计算中使用这些方法,在天文计算中也有一定的应用。
例3:使用math.tan()
方法时的错误
import math
x=math.inf
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
x='h'
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
输出:
Traceback (most recent call last):
File "main.py", line 5, in <module>
value=math.tan(x)
ValueError: math domain error
Traceback (most recent call last):
File "main.py", line 9, in <module>
value=math.tan(x)
TypeError: must be real number, not str
注意,如果参数值不是一个数字,可能会出现TypeError
异常。
例4:用math.tan()
方法使用三角函数
import math
x=1/3
value=math.tan(x)
print(f"The tangent of {x} is {value}.")
a=math.sin(x)
b=math.cos(x)
value= a/b
print(f"The tangent of {x} using trigonometric functions is {value}.")
输出:
The tangent of 0.3333333333333333 is 0.34625354951057546.
The tangent of 0.3333333333333333 using trigonometric functions is 0.34625354951057546.
注意tan(θ)
等于sin(θ)
除以cosine(θ)
。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。