Most of the C Programs deals with complex number operations and manipulations by using complex.h header file. This header file was added in C99 Standard.
C++ standard library has a header, which implements complex numbers as a template class, complex<T>, which is different from <complex.h> in C.
Macros associated with <complex.h>
Some of the macros of <complex.h> are shown below. The values in the left side describe the Macros in complex.h and the right side describes the expansion of those macros with the keywords (_Imaginary, _Complex) added in C99 standard.
Below program helps to create complex numbers.
Example 1:
C
C
Examples 3: Program to find Conjugate of a complex number.
C
C
C
| Macro Name | Expands To |
|---|---|
| complex | _Complex |
| imaginary | _Imaginary |
| _Complex_I | (const float _Complex) i |
| _Imaginary_I | (const float _Imaginary) i |
| I | _Imaginary_I(_Complex_I if _Imaginary_I is absent) |
// C program to show the working
// of complex.h library
#include <complex.h>
#include <stdio.h>
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf(
"z = %.1f% + .1fi\n",
creal(z), cimag(z));
}
Output:
Explanation:
z = 1.3+4.9i
- cmplx() function creates complex number objects by taking real part and imaginary parts as parameters. This function returns the object of complex numbers.
- creal() function returns the real part of a complex number
- cimag() function returns the imaginary part of a complex number
- If our real and imaginary parts are of type float we use cmplxf() function to generate complex numbers and to get real and imaginary parts we use crealf(), cimagf() functions.
- If our real and imaginary parts are of type long double we use cmplxl() function to generate complex numbers and to get real and imaginary parts we use creall(), cimagl() functions.
// C program to create a complex
// number using macro I
#include <complex.h>
#include <stdio.h>
int main(void)
{
double complex
z
= 3.2 + 4.1 * I;
// Creates complex number
// with 3.2 and 4.1 as
// real and imaginary parts
printf(
"z = %.1f% + .1fi\n",
creal(z), cimag(z));
}
Output:
Functions associated with <complex.h>
The <complex.h> header file also provides some inbuilt functions to work with the complex number. Here the word "arg" stands for complex number object.
z = 3.2+4.1i
| Function | Description |
|---|---|
| float cabsf(float complex arg) double cabs(double complex arg) long double cabsl(long double complex arg) |
It returns the absolute value of complex argument |
| float complex cacosf(float complex arg) double complex cacos(double complex arg) long double complex cacosl(long double complex arg) |
It returns the complex arc cosine values of complex argument |
| float complex cacoshf(float complex arg) double complex cacosh(double complex arg) long double complex cacoshl(long double complex arg) |
It returns the complex arc hyperbolic cosine values of complex argument |
| float cargf(float complex arg) double carg(double complex arg) long double cargl(long double complex arg) |
It returns the phase angle of complex argument (in radians). |
| float complex casinf(float complex arg) double complex casin(double complex arg) long double complex casinl(long double complex arg) |
It returns the complex arc sine values of complex argument |
| float complex casinhf(float complex arg) double complex casin(double complex arg) long double complex casinl(long double complex arg) |
It returns the complex arc hyperbolic sine values of complex argument |
| float complex catanf(float complex arg) double complex catan(double complex arg) long double complex catanl(long double complex arg) |
It returns the complex arc tangent values of complex argument |
| float complex catanhf(float complex arg) double complex catan(double complex arg) long double complex catanl(long double complex arg) |
It returns the complex arc hyperbolic tangent values of complex argument |
| float complex ccosf(float complex arg) double complex ccos(double complex arg) long double complex ccosl(long double complex arg) |
It returns the complex cosine values of complex argument |
| float complex cexpf(float complex arg) double complex cexp(double complex arg) long double complex cexpl(long double complex arg) |
It returns the complex value earg where e is the natural logarithm base |
| float crealf(float complex arg) double creal(double complex arg) long double creall(long double complex arg) |
It returns the real part of the complex argument |
| float cimagf(float complex arg) double cimag(double complex arg) long double cimagl(long double complex arg) |
It returns the imaginary part of the complex argument |
| float complex clogf(float complex arg) double complex clog(double complex arg) long double complex cimagl(long double complex arg) |
It returns the imaginary part of the complex argument |
| float complex conjf(float complex arg) double complex conj(double complex arg) long double complex conjl(long double complex arg) |
It returns the conjugate of complex argument |
| float complex cpowf(float complex a, long double complex b) | It returns the complex value of ab |
| float complex csqrtf(float complex arg) double complex csqrt(double complex arg) long double complex csqrtl(long double complex arg) |
It returns the complex square root of argument |
#include <complex.h>
#include <stdio.h>
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
double complex
conj_f
= conjf(z);
printf("z = %.1f% + .1fi\n",
creal(conj_f),
cimag(conj_f));
}
Output:
Examples 4: Program to find the absolute value of a complex number.
z = 1.3-4.9i
#include <complex.h>
#include <stdio.h>
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf("Absolute value = %.1f",
cabsf(z));
}
Output:
Examples 4: Program to find the phase angle of a complex number.
Absolute value = 5.1
#include <complex.h>
#include <stdio.h>
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf(
"Phase Angle = %.1f radians\n",
cargf(z));
}
Output:
Phase Angle = 1.3 radians