C complex.h cabs,cabsf and cabsl


The C <complex.h> cabs , cabsf and cabsl functions compute the absolute value of the complex number.The declaration of the function is shown below.

1 double cabs(double complex z);
2 float cabsf(float complex z);
3 long double cabsl(long double complex z);

All the three functions compute the same value,the only difference between them is in their return type:

i)The ‘cabs’ return the absolute value of the complex number as double complex type.
ii)The ‘cabsf’ return the absolute value of the complex number as float complex type and
iii)The ‘cabsl’ return the absolute value of the complex number as long double complex type.

Some points to note:

i)The cabs , cabsf and cabsl compute the square root of the sum of the square of the real part and imaginary part,

C complex cabs , cabsf and cabsl

abs=cabs=cabsf=cabsl

The magnitude of a complex number is also known as modulus of the complex number.

double cabs(double complex z);

Parameters:
z -The complex number whose magnitude is to be computed.

Return type
double -The absolute value of the complex number.

Code example

double complex c1=90.56 + I*23.5 ;

double mag=cabs( c1 );

printf(“Magnitude of c1=%lf” , mag);

Output,

Magnitude of c1=93.559412



float cabsf(float complex z);

The cabsf returned the magnitude as float type.

Parameters:
z -The complex number whose magnitude is to be computed.

Return type
float -The absolute value of the complex number.

Code example

double complex c1=123450.56 + I*20923933.5 ;

double mag=cabsf( c1 ); //may result in loss of precision

printf(“Magnitude of c1=%lf” , mag);

Output,

Magnitude of c1=20924297.000000

The cabsf will convert the return type to float type but the type is actually a double type,converting from double to float may result in loss of precision.


long double cabsl(long double complex z);

The cabsl return the magnitude as long double type.

Parameters:
z -The complex number whose magnitude is to be computed.

Return type
long double -The absolute value of the complex number.

Code example

long double complex c1=56 + I*55 ;

long double mag=cabsl( c1 );precision

printf(“Magnitude of c1=%Lf” , mag);

Output,

Magnitude of c1=78.492035