math.tanh()方法是数学模块的一种库方法,用于获取以弧度为单位的给定数字的双曲正切,它接受一个数字并返回双曲正切。
注意: math.tanh()方法仅接受数字,如果我们提供除数字以外的其他任何内容,它将返回错误TypeError – “ TypeError:需要浮点数”。
它的语法 math.tanh() 方法:
math.tanh(x)
Parameter(s): x –是要计算其双曲正切的数字。
返回值: float-它返回一个浮点值,该值是数字x的双曲正切值。
示例
Input: x = 1.5 # 函数调用 print(math.tanh(x)) Output: 0.9051482536448664
# Python代码演示示例 # math.tanh() method # 导入数学模块 import math # 数 x = -10 print("math.tanh(",x,"): ", math.tanh(x)) x = 0 print("math.tanh(",x,"): ", math.tanh(x)) x = 1.5 print("math.tanh(",x,"): ", math.tanh(x)) x = 5 print("math.tanh(",x,"): ", math.tanh(x)) x = 15.45 print("math.tanh(",x,"): ", math.tanh(x))
输出结果
math.tanh( -10 ): -0.9999999958776927 math.tanh( 0 ): 0.0 math.tanh( 1.5 ): 0.9051482536448664 math.tanh( 5 ): 0.9999092042625951 math.tanh( 15.45 ): 0.999999999999924
TypeError示例
# Python代码演示示例 # math.tanh() method with exception # 导入数学模块 import math # 数 x = "2.5" print("math.tanh(",x,"): ", math.tanh(x))
输出结果
Traceback (most recent call last): File "/home/main.py", line 9, in <module> print("math.tanh(",x,"): ", math.tanh(x)) TypeError: a float is required