Detailed explanation of complex number operations in C language

烟雨青岚
Release: 2020-06-19 13:30:07
Original
6160 people have browsed it

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

C language introduced plural types in ISO C99. It is defined in complex.h. We can use complex , __complex__ , or _ComplexI type 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. complex It 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_I and are defined in the complex.h header file I to create a plural number.

Macro: const float complex _Complex_I;
Macro: const float complex  I;
Copy after login

These two macros represent complex numbers (0 1i). We can use this unit complex number to create any complex number.

#include <stdio.h>
#include <complex.h>

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;
}
Copy after login
a=3.000000+4.000000i
b=4.000000+5.000000i
c=5.000000+6.000000i
the arg of a is 176
Copy after login

Basic operation functions for complex numbers

Define some functions for basic operations on complex numbers in the

complex.h header file.

FunctionFunction##crealcimagconjcargcproj
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 <stdio.h>
#include <complex.h>

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;
}
Copy after login
d=a+b=7.000000+9.000000i
f=a*b=-8.000000+31.000000i
g=a/b=0.780488+0.024390i
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!