C 库 - ctanh() 函数

❮ C 标准库 - <complex.h>


C 复数 库中的 ctanh() 函数用于计算 z(复数)的双曲正切。双曲正切函数的性质与常规正切函数相似,只是它与双曲线相关,而不是圆。

双曲正切 (tanh) z(复数)定义为: tanh(z)= sinh(z)/cosh(z)
此函数取决于 z(复数)的类型。如果 z 是"float"类型,我们使用 ctanhf() 来计算 tanh;对于 long double 类型,使用 ctanhl();对于 double 类型,使用 ctanh()

语法

以下是 ctanh() 函数的 C 库语法 -

double complex ctanh( double complex z );

参数

此函数接受单个参数 -

  • Z - 它表示一个复数,我们需要计算其 tanh 函数值。

返回值

此函数返回 z(复数)的复数双曲正切值。

示例 1

以下是一个基本的 C 程序,用于演示如何对复数使用 ctanh() 函数。

#include <stdio.h>
#include <complex.h>

int main() {
    double complex z = 4.0 + 5.0 * I;
    
    // 计算双曲正切
    double complex res = ctanh(z);
    printf("复数 tanh: %.2f%+.2fi", creal(res), cimag(res));
    
    return 0;
}

输出

以下是输出 -

复数 tanh: 1.00-0.00i

示例 2

我们来看另一个示例,使用 ctanh() 函数计算实数的双曲正切。

#include <stdio.h>
#include <math.h>
#include <complex.h>
 
int main(void)
{
   // 实线
   double complex z = ctanh(1);
   printf("tanh(1+0i) = %.2f+%.2fi ", creal(z), cimag(z));
}

输出

以下是输出 -

tanh(1+0i) = 0.76+0.00i

示例 3

以下程序计算复数的双曲正切 (tanh) 和虚线正切 (tan),然后比较结果是否相同。

#include <complex.h>
#include <stdio.h>
#include <math.h>

int main() {
    double complex z = 0.0 + 1.0*I;
    
    double complex tanh = ctanh(z);
    double complex tan = ctan(z);
    
    printf("ctanh(%.1fi) = %.2f + %.2fi", cimag(z), creal(tanh), cimag(tanh));
    printf("ctan(%.1fi) = %.2f + %.2fi", cimag(z), creal(tan), cimag(tan));
    
    if (cabs(tanh - tan) < 1e-10) {
        printf("双曲正切值与虚线正切值大致相同。");
    } else {
        printf("双曲正切值与虚线正切值不同。");
    }
    return 0;
}

输出

以下是输出 -

ctanh(1.0i) = 0.00 + 1.56i
ctan(1.0i) = 0.00 + 0.76i
双曲正切值与虚线正切值不同。

❮ C 标准库 - <complex.h>