Home  >  Article  >  Backend Development  >  What you should know about complex number operations in C language when learning programming

What you should know about complex number operations in C language when learning programming

烟雨青岚
烟雨青岚forward
2020-06-17 12:01:214086browse

What you should know about complex number operations in C language when learning programming

What you should know about complex number operations in C language when learning programming

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 symbols to represent it. There are three complex number types in C language, namely float complex, double complex, and 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 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 I are defined in the complex.h header file to create a complex 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 <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;
}
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

Defines some functions for basic operations on complex numbers in the complex.h header file.

Function function

creal Get the real part of the complex number

cimag Get the imaginary part of the complex number

conj Get the conjugate of the complex number

carg Gets the angle between the straight line on the complex plane that passes through the origin and the point represented by the complex number on the complex plane and the real axis

cproj Returns the projection of the complex number on the Riemann sphere

Numerical operations on complex numbers

Complex number types can also be directly used to perform numerical operations like ordinary numerical types, ~int, double, long~, using numerical operation symbols.

#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;
}
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, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/duandianR/article/details/70846638

Recommended tutorial: "C Language"

The above is the detailed content of What you should know about complex number operations in C language when learning programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete