Skip to content

Latest commit

 

History

History
266 lines (181 loc) · 3.97 KB

File metadata and controls

266 lines (181 loc) · 3.97 KB

complex.h - Complex Arithmetic

Category: 📂 std/ (C Standard Library)
Header: <complex.h>
Scope: C99

The complex.h header declares functions and macros for performing arithmetic on complex numbers.

Facilities Overview

Facility Category Key Symbols Description
Arithmetic creal, cimag, cabs, carg, conj Extracting parts and modifying complex values.
Functions cexp, clog, csqrt, csin, ccos Complex versions of standard math functions.
Types & Macros double complex, I Native complex types and imaginary unit.

Types & Variables

complex

Native keyword for complex types.

I

#define I _Complex_I

The imaginary unit i.


Functions

creal

double creal(double complex z)

Extracts the real component.

cimag

double cimag(double complex z)

Extracts the imaginary component.

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

int main(void) {
    double complex z = 3.0 + 4.0 * I;
    printf("Imaginary: %.1f\n", cimag(z)); // 4.0
    return 0;
}

cabs

double cabs(double complex z)
Example
#include <complex.h>
#include <stdio.h>

int main(void) {
    double complex z = 3.0 + 4.0 * I;
    printf("Magnitude: %.1f\n", cabs(z));
    return 0;
}

carg

double carg(double complex z)

Computes the argument (phase angle) of z.

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

int main(void) {
    double complex z = 1.0 + 1.0 * I;
    printf("Argument: %.3f\n", carg(z)); // ≈ 0.785 (π/4)
    return 0;
}

conj

double complex conj(double complex z)

Computes the complex conjugate of z.

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

int main(void) {
    double complex z = 3.0 + 4.0 * I;
    double complex c = conj(z);
    printf("Conjugate: %.1f + %.1fi\n", creal(c), cimag(c)); // 3.0 - 4.0i
    return 0;
}

cexp

double complex cexp(double complex z)

Computes the complex exponential of z.

Example
#include <complex.h>

int main(void) {
    double complex z = 1.0 + 0.0 * I;
    double complex e = cexp(z); // ≈ 2.718 + 0i
    return 0;
}

clog

double complex clog(double complex z)

Computes the complex natural logarithm of z.

Example
#include <complex.h>

int main(void) {
    double complex z = 1.0 + 0.0 * I;
    double complex l = clog(z); // ≈ 0 + 0i
    return 0;
}

csqrt

double complex csqrt(double complex z)

Computes the complex square root of z.

Example
#include <complex.h>

int main(void) {
    double complex z = -1.0 + 0.0 * I;
    double complex s = csqrt(z); // ≈ 0 + 1i
    return 0;
}

csin

double complex csin(double complex z)

Computes the complex sine of z.

Example
#include <complex.h>

int main(void) {
    double complex z = 0.0 + 0.0 * I;
    double complex s = csin(z); // 0 + 0i
    return 0;
}

ccos

double complex ccos(double complex z)

Computes the complex cosine of z.

Example
#include <complex.h>

int main(void) {
    double complex z = 0.0 + 0.0 * I;
    double complex c = ccos(z); // 1 + 0i
    return 0;
}

ctan

double complex ctan(double complex z)

Computes the complex tangent of z.

Example
#include <complex.h>

int main(void) {
    double complex z = 0.0 + 0.0 * I;
    double complex t = ctan(z); // 0 + 0i
    return 0;
}