The Python math.tanh() method returns the hyperbolic tangent of a given number.
The hyperbolic tangent method, denoted as tanh(x), is a mathematical method that calculates the value of the tangent of a complex number or a real number x. It returns real values that range between -1 and 1.
Mathematically, the hyperbolic tangent method is defined as −
tanh(x) = sinh(x)/ cosh(x)
Where, sinh(x) is the hyperbolic sine method and cosh(x) is the hyperbolic cosine method. This method is odd, meaning that tanh(-x) = -tanh(x).
SyntaxFollowing is the basic syntax of the Python math.tanh() method −
math.tanh(x)Parameters
This method accepts a number (all real numbers) for which you want to find the hyperbolic tangent as a parameter.
Return ValueThe method returns the hyperbolic tangent of the given number in the range of (-1, 1).
Example 1In the following example, we calculate the hyperbolic tangent of a positive number using the math.tanh() method −
import math x = 2.0 result = math.tanh(x) print(result)Output
The output obtained is as follows −
0.9640275800758169Example 2
If we pass a fraction value to the math.tanh() method, it returns a number between -1 and 1 (exclusive) −
import math from fractions import Fraction x = Fraction(5, -9) result = math.tanh(x) print(result)Output
Following is the output of the above code −
-0.5046723977218567Example 3
In here, we are retrieving the hyperbolic tangent of a negative number using the math.tanh() method −
import math x = -0.5 result = math.tanh(x) print(result)Output
We get the output as shown below −
-0.46211715726000974Example 4
In this example, we use a loop to calculate the hyperbolic tangent of multiple values using the math.tanh() method. The loop iterates through each value in the values list, calculates the hyperbolic tangent, and prints the result for each value −
import math values = [1.0, 2.0, 3.0] for x in values: result = math.tanh(x) print("tanh({}) = {}".format(x, result))Output
The result produced is as shown below −
tanh(1.0) = 0.7615941559557649 tanh(2.0) = 0.9640275800758169 tanh(3.0) = 0.9950547536867305
python_maths.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4