tanh python
Python math.tanh()方法 (Python math.tanh() method)
math.tanh() method is a library method of math module, it is used to get the hyperbolic tangent of given number in radians, it accepts a number and returns hyperbolic tangent.
math.tanh()方法是数学模块的库方法,用于获取以弧度为单位的给定数字的双曲正切值,它接受一个数字并返回双曲正切值。
Note: math.tanh() method accepts only numbers, if we provide anything else except the number, it returns error TypeError – "TypeError: a float is required".
注意: math.tanh()方法仅接受数字,如果我们提供除数字以外的任何其他内容,它将返回错误TypeError – “ TypeError:需要浮点数” 。
Syntax of math.tanh() method:
math.tanh()方法的语法:
math.tanh(x)
Parameter(s): x – is the number whose hyperbolic tangent to be calculated.
参数: x –是要计算其双曲正切值的数字。
Return value: float – it returns a float value that is the hyperbolic tangent value of the number x.
返回值: float-返回一个浮点值,该值是数字x的双曲正切值。
Example:
例:
Input:
x = 1.5
# function call
print(math.tanh(x))
Output:
0.9051482536448664
Python代码演示math.tanh()方法的示例 (Python code to demonstrate example of math.tanh() method)
# Python code to demonstrate example of
# math.tanh() method
# importing math module
import math
# number
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))
Output
输出量
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 example
TypeError示例
# Python code to demonstrate example of
# math.tanh() method with exception
# importing math module
import math
# number
x = "2.5"
print("math.tanh(",x,"): ", math.tanh(x))
Output
输出量
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
翻译自: https://www.includehelp.com/python/math-tanh-method-with-example.aspx
tanh python