C Standard Library<complex.h>

<complex.h>is a header file in the C standard library used to support complex number operations.

<complex.h>Introduced in the C99 standard, it provides a set of types, macros, and functions for defining and manipulating complex numbers.

1、Complex types

<complex.h>The following complex number types are defined:

  • float complexSingle-precision complex number.

  • double complexDouble-precision complex number.

  • long double complex: long double complex number.

These types are actually macros defined in the C standard library, which expand respectively to:_Complex float、_Complex doubleand_Complex long double。


2、Complex constants

<complex.h>The following macros are defined to represent the imaginary unit:i:

  • IRepresents the imaginary uniti, i.e.sqrt(-1)。

Example

double complex z = 3.0 + 4.0 * I; // Represents the complex number 3 + 4i

3、Complex number operation functions

<complex.h>It provides a set of functions for operating on complex numbers, including arithmetic operations, trigonometric functions, exponential functions, and logarithmic functions, etc. The following are some commonly used functions:

Arithmetic operations

Function Description
creal(z) Returns the complex numberzthe real part
cimag(z) Returns the complex numberzthe imaginary part
cabs(z) Returns the complex numberzThe modulus (absolute value) of
carg(z) Returns the complex numberzThe argument (phase) of
conj(z) Returns the complex numberzthe complex conjugate
cproj(z) Returns the complex numberzthe projection

Trigonometric functions

Function Description
csin(z) Returns the complex numberzsine value of
ccos(z) Returns the complex numberzcosine value of
ctan(z) Returns the complex numberztangent value of

Exponential and logarithmic functions

Function Description
cexp(z) Returns the complex numberzexponential value of
clog(z) Returns the complex numberz's natural logarithm

power function

Function Description
cpow(z1, z2) Returns the complex numberz1ofz2power

square root function

Function Description
csqrt(z) Returns the complex numberz's square root

4、Example

The following is a use<complex.h>An example of ..., showing how to define and manipulate complex numbers:

Example

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

int main() {
    // Define complex numbers
    double complex z1 = 3.0 + 4.0 * I;
    double complex z2 = 1.0 - 2.0 * I;

    // Arithmetic operations
    double complex sum = z1 + z2;
    double complex product = z1 * z2;

    // Output the results
    printf("z1 = %.2f + %.2fi\n", creal(z1), cimag(z1));
    printf("z2 = %.2f + %.2fi\n", creal(z2), cimag(z2));
    printf("Sum = %.2f + %.2fi\n", creal(sum), cimag(sum));
    printf("Product = %.2f + %.2fi\n", creal(product), cimag(product));

    // Modulus and argument
    printf("|z1| = %.2f\n", cabs(z1));
    printf("Phase of z1 = %.2f radians\n", carg(z1));

    // Exponential function
    double complex exp_z1 = cexp(z1);
    printf("exp(z1) = %.2f + %.2fi\n", creal(exp_z1), cimag(exp_z1));

    return 0;
}

Output result:

z1 = 3.00 + 4.00i
z2 = 1.00 + -2.00i
Sum = 4.00 + 2.00i
Product = 11.00 + -2.00i
|z1| = 5.00
Phase of z1 = 0.93 radians
exp(z1) = -13.13 + -15.20i

5、Notes

  • <complex.h>It is only available in C99 and later versions.

  • Using complex number types and functions requires including<complex.h>Header file.

  • The performance of complex number operations may be lower than real number operations, especially when involving a large amount of computation.


6、Mathematical background of complex number operations

The general form of a complex number isa + bi, where:

  • ais the real part,bis the imaginary part.

  • iis the imaginary unit, satisfyingi² = -1。

The modulus (absolute value) of a complex number is:

|z| = sqrt(a² + b²)

The argument (phase) of a complex number is:

arg(z) = atan2(b, a)

The exponential form of a complex number is:

exp(z) = exp(a) * (cos(b) + i * sin(b))
other extensions