# C complex.h creal,crealf and creall

The C <complex.h> creal , crealf and creall function compute the real part of the complex number.The declaration of the creal function is shown below.

1 double creal(double complex z);
2 float crealf(float complex z);
3 long double creall(long double complex z);

All the three functions are same.They return the real part of the complex number.The only difference is in the return type of the function:

i)The first function return a real value of double type
ii)The second function return a real value of float type and
iii)The third function return a real value of long double type.

#### double creal(double complex z);

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

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

Code example

double complex c1=12.34 + I*78.90 ;

printf( “Real part of c1=%e” , creal( c1 ) );

Output,

Real part of c1=1.234000e+001

#### float crealf(float complex z);

In this function the ‘f’ appended at the end signify the return type of the function,which is the float type.

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

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

Code example

double complex c1=12.34 + I*78.90 ;

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

Output,

Real part of c1=1.234000e+001
Real part of c1=23.45

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 creall(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 real part is to be computed.

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

Code example

long double complex c1=12.34 + I*78.90 ;

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

Output,

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

The real part of c1 complex number is long double type so converting it to float or double type may result in loss of precision.

#### Assigning the creal returned value to complex number

In assigning the returned value of creal or crealf or creall function to the complex number,the real value of the complex number is changed.

Code example

double complex c1=12.34 + I*78.90 ,
c2=888.900 + I*111.555 ;

pritnf( “Real value of c1=%lf” , creal(c1) );

c1=creal( c2 );

pritnf( “Real value of c1=%lf” , creal(c1) );

Output,

Real value of c1=12.340000
Real value of c1=888.900000

You can see that the real value of c1 is changed or is assigned the real value of c2 in calling the creal( c2 ) function.

*Side Note

In Visual Studio the correct way to define a complex number is shown below.

Code example

_Dcomplex c1=12.34 + I*78.90 ; /*a double type complex number */

_Fcomplex c2=12.34 + I*78.90 ; /*A float type complex number */

_Lcomplex c3=12.34 + I*78.90 ; /*A long double type complex number */

printf( “Real part of c1=%f” , creal( c1 ) );
printf( “\nReal part of c2=%lf” , crealf( c2 ) );
printf( “\nReal part of c3=%Lf” , creall( c3 ) );

Output,

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