C complex.h cimag,cimagf and cimagl


The C <complex.h> cimag , cimagf and cimagl function compute the imaginary part of the complex number as a real value.The declaration of the cimag function is shown below.

1 double cimag(double complex z);
2 float cimagf(float complex z);
3 long double cimagl(long double complex z);

All the three functions are same,they return the imaginary part of the complex number as real value.The only difference between them is in their return type:

i)The first function return the imaginary part as real value as double type
ii)The second function return the imaginary part as real value as float type and
iii)The third function return the imaginary value as real value as long double type.

double cimag(double complex z);

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

Return type
double-The imaginary part is returned as real value as double type.

Code example

double complex c1=777.889 +I*11.23 ;
printf( “Imaginary part of c1=%lf” , cimag(c1) );

Output,

Imaginary part of c1=11.230000





float cimagf(float complex z);

In this function the ‘f’ appended at the end signify the return type of the function and it stands for ‘float’ type.

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

Return type
double-The imaginary part is returned as real value as float type.

Code example

double complex c1=12.34 + I*78.90 ;

printf( “Imaginary part of c1=%f” , cimagf( c1 ) ); /*Work fine but may result in loss of precision */
printf( “\nImaginary part of c1=%lf” , cimagl( c1 ) );

float complex c2=11.3 + I*01.003 ;
printf( “\nImaginary part of c2=%lf” , cimagf( c2 ) );

Output,

Imaginary value of c1=78.900000
Imaginary value of c1=78.900000
Imaginary part of c2=1.003000

In the first output the number returned is float type but the actual type of the c1 complex number is double type,converting from double to float type may result in loss of precision.


long double cimagl(long double complex z);

In this function the character ‘l’ is appended at the end.It stands for long double hence this function return type is long double type.

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

Return type
long double-The imaginary part is returned as real value as long double type.

Code example

long double complex c1=12.34 + I*78.90 ;

printf( “Real part of c1=%f” , cimagf( c1 ) ); /*Work fine but may result in loss of precision */
printf( “\nReal part of c1=%lf” , cimag( c1 ) ); /*Work fine but may result in loss of precision */
printf( “\nReal part of c1=%Lf” , cimagl( c1 ) );

Output,

Real part of c1=12.340000
Real part of c1=12.340000
Real part of c1=12.340000


Assigning the cimag returned value to complex number

If the returned value of cimag or cimagf or cimagl is assign to the complex number the real value of the complex number on the left hand side is replaced with the imaginary value returned by the cimag function.And the imaginary value of the complex number on the left hand side is assigned the value 0.

Code example

double complex c1=12.34 + I*78.90 ,
c2=999.545 +I*002.34 ;

printf(“Imaginary value of c1 =%lf” , cimag( c1 ) );

c1=cimag( c2 );

printf(“\nReal value of c1 after assignment =%lf” , creal( c1 ) );
printf(“\nImaginary value of c1 after assignment =%lf” , cimag( c1 ) );

Output,

Imaginary value of c1 =78.900000
Real value of c1 after assignment =2.340000
Imaginary value of c1 after assignment =0.000000

The real value of c1 is assigned the imaginary value of c2 and the imaginary value of c1 is assigned the value 0.

On how to define complex number in Visual Studio please visit the link https://www.youtube.com/watch?v=lZxh_4Kwv5c.