Python - AI 助手

Python cmath.tanh() 函数



Python 的 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)

示例 5

在下例中,如果给定的值不是数字,则此函数返回TypeError。

import cmath
x = "Welcome to Tutorialspoint"
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
python_modules.htm
广告
© . All rights reserved.