Previous Page
Next Page

cabs

Obtains the absolute value of a complex number

#include <complex.h>
double cabs ( double complex z  );
float cabsf ( float complex z  );
long double cabsl ( long double complex z  );

For a complex number z = x + y x i, where x and y are real numbers, cabs(z) is equal to the square root of x2 + y2, or hypot(x,y). The result is a non-negative real number.

Example

The absolute value of a complex number is its absolute distance from the origin in the complex planein other words, a positive real number, as this example demonstrates:

double complex z[4];
z[0] = 3.0 + 4.0 * I;
z[1] = conj( z[0] );
z[2] =  z[0] * I;
z[3] = -( z[0] );

for (int i = 0; i < 4 ; i++ )
  {
    double a = creal(z[i]);
    double b = cimag(z[i]);
    printf ( "The absolute value of (%4.2f %+4.2f x I) is ", a, b );

    double absolute_z = cabs(z[i]);
    printf ( "%4.2f.\n", absolute_z );
  }

The output of the sample code is as follows:

The absolute value of (3.00 +4.00 x I) is 5.00.
The absolute value of (3.00 -4.00 x I) is 5.00.
The absolute value of (-4.00 +3.00 x I) is 5.00.
The absolute value of (-3.00 -4.00 x I) is 5.00.

See Also

cimag( ), creal( ), carg( ), conj( ), cproj( )


Previous Page
Next Page