Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python cmath.tanh() Function



The Python cmath.tanh() function returns the hyperbolic tangent of a given complex number.

This function is used to find the hyperbolic tangent. If the given value is not a number, then this function returns a TypeError.

The hyperbolic tangent is denoted as tanh(x). This function returns the real values from the ranges -1 and 1.

The mathematical representation of the tangent hyperbolic function is −

tanh(x) = sinh(x)/cosh(x)

Here, sinh(x) is the hyperbolic sine function, and cosh(x) is the hyperbolic cosine function. This function is symmetric with respect to the origin, i.e tanh(-x) = -tanh(-x).

Syntax

Following is the basic syntax of the cmath.tanh() function −

cmath.tanh(x)

Parameters

This function accepts all real numbers for which we need to find the hyperbolic tangent as a parameter.

Return Value

This function returns the hyperbolic tangent of a given complex number in the range (-1,1).

Example 1

In the below example, we are calculating the hyperbolic tangent of a complex number using the cmath.tanh() function.

import cmath
x = 3+2j
result = cmath.tanh(x)
print(result)

Output

Following is the output of the above code −

(1.00323862735361-0.003764025641504249j)

Example 2

When we pass fraction value to the cmath.tanh() function, then this function returns numbers in the range -1 and 1 −

import cmath
from fractions import Fraction
x = Fraction(7, -9)
result = cmath.tanh(x)
print(result)

Output

Output obtained is as follows −

(-0.6514293576428292+0j)

Example 3

In the below example, we are retrieving the hyperbolic tangent of a negative number using the cmath.tanh() function −

import cmath
x = -0.7
result = cmath.tanh(x)
print(result)

Output

We will get the following output −

(-0.6043677771171636+0j)

Example 4

In the following example, we are creating a loop to create a hyperbolic tangent values using the math.tanh() function. This loop iterates through each value in the list.

import cmath
values = [2.0, 4.0, 6.0]
for x in values:
   result = cmath.tanh(x)
   print("tanh({}) = {}".format(x, result))

Output

We will get the output as shown below −

tanh(2.0) = (0.9640275800758169+0j)
tanh(4.0) = (0.999329299739067+0j)
tanh(6.0) = (0.9999877116507956+0j)

Example 5

In the below example, if the given value is not a number then this function returns TypeError.

import cmath
x = "Welcome to Tutorialspoint"
result = cmath.tanh(x)
print(result)

Output

We will get the output as shown below −

Traceback (most recent call last):
  File "/home/cg/root/36330/main.py", line 3, in 
    result = cmath.tanh(x)
TypeError: must be real number, not str
python_modules.htm
Advertisements