C Programming/complex.h/carg

From Wikibooks, open books for an open world
Jump to: navigation, search

carg is a standard library function which calculates the argument (phase angle) of a complex number. It takes a single parameter and returns the phase angle of that value (converted if necessary to a complex number) within the range [π, −π].

Description[edit]

This function takes a single parameter of type complex and returns a result of type double.

A complex number can be represented:

  • in rectangular co-ordinates as
  • in polar co-ordinates as

where:

  • A = creal(Z) is the real part
  • B = cimag(Z) is the imaginary part
  • r = cabs(z) is the radius
  • a = carg(z) is the phase (angle or the argument)

Hence this function returns phase of complex number Z used in polar co-ordinates. The return value is in radians, in the range [π, -π]. When we use this function, we must include the <complex.h> header file.

Synopsis[edit]

#include <complex.h>
double carg(double complex z);
float cargf(float complex z);
long double cargl(long double complex z);[1]

Example 1[edit]

/* conversion of a real number from its Cartesian to polar form */

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

int main() {
double complex z = -4.4 + 3.3 * I;
double x = creal(z);
double y = cimag(z);

double radius = cabs(z);
double argument = carg(z);

printf("cartesian(x, y): (%4.1f, %4.1f)\n", x, y);
printf("polar(r, theta): (%4.1f, %4.1f)\n", radius, argument);
return 0;
}

Output[edit]

Cartesian(x, y): (-4.4, 3.3)
polar(r, theta): (5.5, 2.5)[2]

References[edit]