Name

carg

Synopsis

Calculates the argument of a complex number

#include <complex.h>
doublecarg( double complex z );
float cargf( float complex z );
long double cargl( long double complex z );

The carg() function determines the argument of a complex number, or the angle it forms with the origin and the positive part of the real axis. A complex number is defined in polar coordinates by its argument and modulus (or radius), which is the same as the absolute value of the complex number, given by cabs(). The return value of carg() is in radians, and within the range [-π, π]. For a complex number z = x + y × i, where x and y are real numbers, carg(z) is equal to atan2( y, x).

Example

/* Convert a complex number from Cartesian to polar coordinates. */
double complex z = -4.4 + 3.3 * I;
double radius = cabs( z );
double argument =carg( z );

double x = creal( z );
double y = cimag( z );

printf( "Cartesian (x, y): (%4.1f, %4.1f)\n", x, y );
printf( "Polar (r, theta): (%4.1f, %4.1f)\n", radius, argument );

This code produces the following output:

Cartesian (x, y): (-4.4,  3.3)
Polar (r, theta): ( 5.5,  2.5)

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.