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