Home>Article>Backend Development> Detailed explanation of complex number operations in C language

Detailed explanation of complex number operations in C language

烟雨青岚
烟雨青岚 Original
2020-06-19 13:30:07 6099browse

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). The

Clanguage introduced plural types inISO C99. It is defined incomplex.h. We can usecomplex,__complex__, or_ComplexItype notation to represent it. There are three complex number types in C language, namelyfloat 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_Iandare defined in thecomplex.hheader 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 the

complex.hheader file.

Function Function ##creal cimag conj carg cproj
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
Numerical operations on complex numbers

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 Language

"

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn