Name
cabs
Synopsis
Obtains the absolute value of a complex number
#include <complex.h> doublecabs
( double complexz
); floatcabsf
( float complexz
); long doublecabsl
( long double complexz
);
For a complex number z
=
x
+ y
×
i, where
x
and y
are
real numbers, cabs(
z
)
is equal to the square root of
x
2 +
y
2, 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 plane—in 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 × 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 × I) is 5.00. The absolute value of (3.00 -4.00 × I) is 5.00. The absolute value of (-4.00 +3.00 × I) is 5.00. The absolute value of (-3.00 -4.00 × I) is 5.00.
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.