Python cmath 模块的 cmath.tanh() 函数返回给定复数的双曲正切。
此函数用于求双曲正切。如果给定的值不是数字,则此函数返回 TypeError。
双曲正切表示为 tanh(x)。此函数返回范围 -1 和 1 中的实际值。
切线双曲函数的数学表示为 -
tanh(x) = sinh(x)/cosh(x)
其中,sinh(x) 是双曲正弦函数,cosh(x) 是双曲余弦函数。此函数相对于原点对称,即 tanh(-x) = -tanh(-x)。
语法
以下是 cmath.tanh() 函数的基本语法 -
cmath.tanh(x)
参数
此函数接受我们需要找到双曲正切作为参数的所有实数。
返回值
此函数返回范围 (-1,1) 中给定复数的双曲正切值。
示例 1
在下面的示例中,我们使用 cmath.tanh() 函数计算复数的双曲正切。
import cmath
x = 3+2j
result = cmath.tanh(x)
print(result)
输出
以下是上述代码的输出 -
(1.00323862735361-0.003764025641504249j)
示例 2
当我们将分数值传递给 cmath.tanh() 函数时,此函数返回 -1 和 1 − 范围内的数字
import cmath
from fractions import Fraction
x = Fraction(7, -9)
result = cmath.tanh(x)
print(result)
输出
获得的输出如下 -
(-0.6514293576428292+0j)
示例 3
在下面的示例中,我们使用 cmath.tanh() 函数检索负数的双曲正切 -
import cmath
x = -0.7
result = cmath.tanh(x)
print(result)
输出
我们将得到以下输出 -
(-0.6043677771171636+0j)
示例 4
在下面的示例中,我们将创建一个循环,以使用 math.tanh() 函数创建双曲正切值。此循环遍历列表中的每个值。
import cmath
values = [2.0, 4.0, 6.0]
for x in values:
result = cmath.tanh(x)
print("tanh({}) = {}".format(x, result))
输出
我们将得到如下所示的输出 -
tanh(2.0) = (0.9640275800758169+0j)
tanh(4.0) = (0.999329299739067+0j)
tanh(6.0) = (0.9999877116507956+0j)
tanh(4.0) = (0.999329299739067+0j)
tanh(6.0) = (0.9999877116507956+0j)
示例 5
在下面的示例中,如果给定的值不是数字,则此函数返回 TypeError。
import cmath
x = "Welcome to qikepu"
result = cmath.tanh(x)
print(result)
输出
我们将得到如下所示的输出 -
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
File "/home/cg/root/36330/main.py", line 3, in
result = cmath.tanh(x)
TypeError: must be real number, not str