C 库 - conj() 函数

❮ C 标准库 - <complex.h>


C complex 库中的 conj() 函数用于通过反转虚部的符号来计算 z(复数)的复共轭。复数的共轭是指实部相等,虚部大小相等但符号相反的数。

此函数取决于 z(复数)的类型。如果 z 是"float"类型或浮点虚数,我们可以使用 conjf() 计算共轭。对于 long double 类型,使用 conjl();对于 double 类型,使用 conj()

语法

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

double complex conj( double complex z );

参数

此函数接受单个参数 -

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

返回值

此函数返回 z(复数)的复共轭值。

示例 1

以下是一个简单的 C 程序,演示如何使用 conj() 计算 z 的共轭值。

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

int main() {
    double real = 3.0;
    double imag = 4.0;
    
    // 使用 CMPLX 函数创建复数
    double complex z = CMPLX(real, imag);
    
    printf("复数为:%.2f + %.2fi", creal(z), cimag(z));
    
    // 计算共轭
    double complex vocation = conj(z);
    
    printf("复数的共轭为:%.2f + %.2fi", creal(vocation), cimag(vocation));
    
    return 0;
}

输出

以下是输出 -

复数为:3.00 + 4.00i
复数的共轭为:3.00 + -4.00i

示例 2

我们来看另一个示例,使用 conj() 函数计算两个复数和的共轭。

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

int main() {
    双精度复数 z1 = 1.0 + 3.0 * I;
    双精度复数 z2 = 1.0 - 4.0 * I;
    
    printf("复数:Z1 = %.2f + %.2fi Z2 = %.2f %+.2fi", creal(z1), cimag(z1), creal(z2), cimag(z2));
    
    双精度复数 z = z1 + z2;
    printf("Z:Z1 + Z2 = %.2f %+.2fi", creal(z), cimag(z));
    
    双精度复数 conju= conj(z);
    printf("Z 的共轭 = %.2f %+.2fi", creal(conju), cimag(conju));
    
    return 0;
}

输出

以下是输出 -

复数:Z1 = 1.00 + 3.00i Z2 = 1.00 -4.00i
Z:Z1 + Z2 = 2.00 -1.00i
Z 的共轭 = 2.00 +1.00i

示例 3

以下示例计算长整型复数的共轭。

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

int main(void) {
    long double complex z = 3.0 + -4.0*I;
    
    // 计算共轭值
    long double conj = conjl(z);
    
    printf("复数:%.2Lf + %.2Lfi", creall(z), cimagl(z));
    printf("z 的共轭值:%.2Lf + %.2Lfi", creall(conj), cimagl(conj));
    return 0;
}

输出

以下是输出 -

复数:3.00 + -4.00i
z 的共轭值:3.00 + 0.00i

❮ C 标准库 - <complex.h>