The C <complex.h> header provides many functions to perform operations on complex number.The concept of complex number in C programming is same as the complex numbers in mathematics.Some of the common operations we performed on complex number in Mathematics like finding the subtractions or addition or even computing the trigonometric value of the complex values can be also carried out in C using the functions provided by <complex.h> header.
In this post we will see how to declare a complex number in C and also see the functions provided by the <complex.h> in detail.A more vivid discussion of each function is given in separate post,you can visit each link related to the function to read about the function.
Declaring complex number
To declare a complex number in C we usually use the format:
data_type complex variable_name;
data_type _Complex variable_name;
Replace the ‘data_type‘ with the data type of your choice :float or double or long double.
In Mathematics we usually declare a complex number in the format a+ib,the ‘i’ is the iota.In C while declaring the initializer of a complex variable we will use ‘I'(capital ‘i’) instead of ‘i’ to signify the iota of a complex number.An example of complete complex number declaration in C is shown below.
float _Complex c2=0.89 +I*11.23 ; //a float type complex number with 0.89 as real part and 11.23 as imaginary part
float _Complex cc; //an uninitialized complex number
double complex c3=0.6 + 12 ; //work fine but real part is 12.6 and imaginary part is 0
double complex c4=123 ; //work fine real part is 123 and imaginary part is 0
double complex c5=I*123 ; //work fine real part is 0.00 and imaginary part is 123
As you can the initializer may have or may not have the letter ‘I’.If the letter ‘I’ is include the the value that accompany it is the imaginary part,if ‘I’ is absent the Imaginary part is taken as 0.00 (zero).
The next section shows all the functions found in the <complex.h> header file.
Functions of <complex.h> header
|cacos ,cacosf ,cacosl||compute the complex arc cosine of the complex number|
|casin, casinf ,casinl||compute the complex arc sine of the complex number|
|catan, catanf ,catanl||compute the complex arc tangent of the complex number|
|ccos, ccosf , ccosl||compute the complex cosine of the complex number|
|csin, csinf , csinl||compute the complex sine of the complex number|
|ctan, ctanf , ctanl||compute the complex tangent of the complex number|
|cacosh, cacoshf , cacoshl||compute the complex arc hyperbolic cosine of the complex number|
|casinh , casinhf , casinhl||compute the complex arc hyperbolic sine of the complex number|
|catanh , catanhf , catanhl||compute the complex arc hyperbolic tangent of the complex number|
|ccosh, ccoshf , ccoshl||compute the complex hyperbolic cosine of the complex number|
|csinh, csinhf , csinhl||compute the complex hyperbolic sine of the complex number|
|ctanh, ctanhf , ctanhl||compute the complex hyperbolic tangent of the complex number|
|cexp, cexpf , cexpl||compute the base-e exponential of the complex number|
|clog, clogf , clogl||compute the natural logarithm of the complex number|
|cabs ,cabsf , cabsl||compute the absolute of the complex number|
|cpow, cpowf , cpowl||compute the base-e exponential of the complex number|
|csqrt , csqrtf , csqrtl||compute the complex power value|
|carg, cargf , cargl||compute the phase angle of the complex number|
|cimag , cimagf , cimagl||compute the imaginary part of the complex number|
|conj , conjf , conjl||compute the conjecture of the complex number|
|cproj , cprojf , cprojl||compute the projection of the complex number on Riemann sphere|
|creal , crealf , creall||compute the real part of the complex number|
A code example using complex number in C is given below.
csum=c1+c2; ///csum stores the sum of c1 and c2 complex numbers
printf( “\nReal part of csum=%lf” , creal(csum) ); //creal compute the real part of any complex number
printf( “\nImaginary part of csum=%lf” , cimag(csum) ); //cimag compute the imaginary part of any complex number
cproduct=c1*c2 ; //cproduct stores the product of c1 and c2
printf( “\n\nReal part of cproduct=%lf” , creal(cproduct) );
printf( “\nImaginary part of cproduct=%lf” , cimag(cproduct) );
float complex cf1=11.23 +I*21 , cf2=9.4 +I*619 ,
chypertan= ctanhf( cf1 ) , //ctanhf compute the hyperbolic tangent of complex number
cExpo=cexpf( cf2 ); //cexpf compute the exponential of any complex number
printf( “\n\nReal part of chypertan=%f” , crealf( chypertan ) );
printf( “\nImaginary part of chypertan is=%f” , cimagf( chypertan ) );
printf( “\n\nReal part of cproduct=%f” , crealf( cExpo ) );
printf( “\nImaginary part of cproduct is=%f” , cimagf( cExpo ) );
Output in Code::Blocks,
Real part of csum=134.560000
Imaginary part of csum=108.230000
Real part of cproduct=467.646300
Imaginary part of cproduct is=12336.452700
Real part of chypertan=1.000000
Imaginary part of chypertan is=-0.000000
Real part of cproduct=-12020.210938
Imaginary part of cproduct is=-1281.941650
The video below explain how to declare complex number in Visual Studio in C programming.