Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python cmath.tanh() Function



The Pythoncmath.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 astanh(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, andcosh(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 thecmath.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 thecmath.tanh() function.

import cmathx = 3+2jresult = 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 thecmath.tanh() function, then this function returns numbers in the range -1 and 1 −

import cmathfrom fractions import Fractionx = 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 thecmath.tanh() function −

import cmathx = -0.7result = 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 themath.tanh() function. This loop iterates through each value in the list.

import cmathvalues = [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 cmathx = "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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp