Home>Article>Backend Development> Detailed explanation of complex number operations in C language
#Detailed explanation of complex number operations in C language
Note: The complex type was introduced from the c99 standard, but the specific implementation is not part of the standard and is implemented by each compiler. The standards may be different. For specific information, please check the implementation standards of the relevant compilers.This article is for the gcc compiler.
Complex numbers are very important in mathematical operations. When writing numerical operations or algorithms, we will use the concept of complex numbers. So, how are complex numbers represented in C/C language? We will introduce them one by one next.Complex numbers in C language
In mathematics, a complex number can be defined in the form of (z=a bi). TheClanguage introduced plural types in
ISO C99. It is defined in
complex.h. We can use
complex,
__complex__, or
_ComplexItype notation to represent it. There are three complex number types in C language, namely
float complex,
double complex,
long double complex. The difference between them is that the data types representing the real part and imaginary step of complex numbers are different.
complexIt is actually an array. There are two elements in the array, one representing the real part of the complex number and one representing the imaginary part of the complex number.
Define a complex number
Two macros_Complex_Iand
are defined in thecomplex.h
header file Ito create a plural number.
Macro: const float complex _Complex_I; Macro: const float complex I;These two macros represent complex numbers (0 1i). We can use this unit complex number to create any complex number.
#include#include int main(int argc, char *argv[]) { complex double a = 3.0 + 4.0 * _Complex_I; __complex__ double b = 4.0 + 5.0 * _Complex_I; _Complex double c = 5.0 + 6.0 * _Complex_I; printf("a=%f+%fi\n", creal(a),cimag(a)); printf("b=%f+%fi\n", creal(b), cimag(b)); printf("c=%f+%fi\n", creal(c), cimag(c)); printf("the arg of a is %d", carg(a)); return 0; }
a=3.000000+4.000000i b=4.000000+5.000000i c=5.000000+6.000000i the arg of a is 176
Basic operation functions for complex numbers
Define some functions for basic operations on complex numbers in thecomplex.hheader file.
Function | |
---|---|
Get the plural number The real part | |
Get the imaginary part of the complex number | |
Get the common part of the complex number Yoke | |
Get the angle between the straight line on the complex plane passing through the origin and the point represented by the complex number in the complex plane and the real axis | |
Returns the projection of a complex number on the Riemann sphere |
Complex number types can also directly use numerical operation symbols to perform numerical operations like ordinary numerical types, ~int, double, long~.
#include#include int main(int argc, char *argv[]) { complex double a = 3.0 + 4.0 * _Complex_I; __complex__ double b = 4.0 + 5.0 * _Complex_I; _Complex double c = 5.0 + 6.0 * _Complex_I; complex double d =a + b; complex double f = a *b ; complex double g = a/b; printf ("d=a+b=%f+%fi\n",creal(d),cimag(d)); printf ("f=a*b=%f+%fi\n",creal(f),cimag(f)); printf("g=a/b=%f+%fi\n",creal(g),cimag(g)); return 0; }
d=a+b=7.000000+9.000000i f=a*b=-8.000000+31.000000i g=a/b=0.780488+0.024390i
Thank you everyone for reading, have you learned it?
This article is reproduced from: https://blog.csdn.net/duandianR/article/details/70846638
Recommended tutorial: "
C LanguageThe above is the detailed content of Detailed explanation of complex number operations in C language. For more information, please follow other related articles on the PHP Chinese website!