What is Complex Number ? What does real and imaginary number means in complex numbers? Do you want to use Complex numbers in your C++ software? How we can operate with complex numbers.

In mathematics, a **Complex Number** is a number that is composed with real numbers and imaginary with ‘i’ symbol, in the form *a* + *bi*. Here *a* is called the **real part** and *b* is called the **imaginary part**. Complex numbers come from satisfying the equation *i*^{2} = −1. Because there is no real number to satisfy this equation, but we can use complex numbers with this *i* (imaginary number by René Descartes) to solve our these kind of problems.

In C++ there is a **complex library** that implements the complex class to contain complex numbers in cartesian form and many methods and properties to operate with in complex forms. In this post we will give some of basics to use complex numbers in C++.

Table of Contents

## What is the definition of a complex number in a C++ software app?

We can define a complex number with real and imaginary parts as given example below,

1 2 3 |
std::complex<double> c1(6.0, 3.0); // c1 is a complex number 6+3i |

We can use **auto** to define a complex variable

1 2 3 |
auto c3 = std::complex<double>(1.2, 3.4); // we can use auto to define a new complex variable |

We can also define a complex number in lines as below,

1 2 3 4 5 6 |
std::complex<double> c; c = {1,2}; //setting a new value to a complex variable c = std::complex<double>(1.2, 3.4); //setting a new value to a complex variable |

## How to display a complex number and its Real and Imaginary parts

We can display real and imaginary parts of a complex number as given example below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <complex> int main() { std::complex<double> c1(6.0, 3.0); // c1 is a complex number 6+3i std::cout << "c1: " << c1 << '\n'; std::cout << "Real number of c1: " << c1.real() << '\n'; std::cout << "Imaginary number of c1: " << c1.imag() << '\n'; std::cout << "Complex number c1: " << c1.real() << " + " << c1.imag() << "i\n"; getchar(); return 0; } |

1 2 3 4 5 6 7 8 9 10 |
c1: (6,3) Complex number c1: 6 + 3i Real number of c1: 6 Imaginary number of c1: 3 |

## How to use addition, subtraction, multiplication, and division operators with complex numbers in C++

We can use + – / * operators as we use in real numbers. When you operate with complex numbers real part and imaginary parts are handled as two different variables. See this example below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <complex> int main() { std::complex<double> c; std::complex<double> c1(6.0, 3.0); std::complex<double> c2(8.0, 1.0); std::cout << "c1: " << c1 << '\n'; std::cout << "c2: " << c1 << '\n'; std::cout << "Real Number of c1: " << c1.real() << '\n'; std::cout << "Imaginary Number of c1: " << c1.imag() << '\n'; std::cout << "Complex Number c1: " << c1.real() << " + " << c1.imag() << "i\n"; std::cout << "Complex Number c2: " << c2.real() << " + " << c2.imag() << "i\n\n"; c = c1 + c2; std::cout << "Complex Addition (c1+c2): " << c.real() << " + " << c.imag() << "i\n"; c = c2 - c1; std::cout << "Complex Substraction (c2-c1): " << c.real() << " + " << c.imag() << "i\n\n"; c = c1*2.0; std::cout << "Complex Multipication (c1*2): " << c.real() << " + " << c.imag() << "i\n"; c = c1*c2; std::cout << "Complex Multipication (c1*c2): " << c.real() << " + " << c.imag() << "i\n"; c = c2/2.0; std::cout << "Complex Division (c2/2): " << c.real() << " + " << c.imag() << "i\n"; c = c2/c1; std::cout << "Complex Division (c2/c1): " << c.real() << " + " << c.imag() << "i\n"; getchar(); return 0; } |

Output of this example will be as follows,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
c1: (6,3) c2: (6,3) Real Number of c1: 6 Imaginary Number of c1: 3 Complex Number c1: 6 + 3i Complex Number c2: 8 + 1i Complex Addition (c1+c2): 14 + 4i Complex Substraction (c2-c1): 2 + -2i Complex Multipication (c1*2): 12 + 6i Complex Multipication (c1*c2): 45 + 30i Complex Division (c2/2): 4 + 0.5i Complex Division (c2/c1): 1.13333 + -0.4i |

In C++, we can also use other functions with complex numbers,

## Using functions with complex numbers

#### Square Root Function

In C++, **sqrt()** function returns the square root of complex number using the principal branch, whose cuts are along the negative real axis

#### Power Function

In C++, pow() function powers the complex number.

Here is an example about Square Root **sqrt()** and Power **pow()** functions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <complex> int main() { std::complex<double> c; std::complex<double> c1(6.0, 3.0); c = sqrt(c1); std::cout << "Squareroot of c1: " << c.real() << " + " << c.imag() << "i\n"; c = pow(c1, 2); std::cout << "Power 2 of c1: " << c.real() << " + " << c.imag() << "i\n"; getchar(); return 0; } |

#### Absolute Function

In C++, **abs() function** returns the absolute of the complex number.

#### Argument Function

In C++, **arg() function** returns the argument of the complex number.

Here is an example about **abs() **and **arg()** functions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <complex> int main() { std::complex<double> c1(6.0, 3.0); std::cout << "Complex Number c1: " << c1.real() << " + " << c1.imag() << "i\n"; std::cout << "Absolute of c1: " << abs(c1) << '\n'; std::cout << "Argumant of c1: " << arg(c1) << '\n'; getchar(); return 0; } |

#### Norm Function

In C++, **norm()** function is used to find the norm of the complex number. If c = a + bi is a complex number with real part a and imaginary part b, the complex conjugate of c is defined as c'(c bar) = a – bi, and the absolute value, also called the norm, of c is defined as,

1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <complex> int main() { std::complex<double> c1(6.0, 3.0); std::cout << "Norm of c1: " << norm(c1) << "\n\n"; getchar(); return 0; } |

#### Polar Function

In C++, **polar()** function constructs a complex number from magnitude and phase angle. Note that real part is equal to magnitude*cosine(phase angle) and imaginary part is equal to magnitude*sine(phase angle);

#### Conjugate Function

In C++, **conj()** function returns the conjugate of the complex number c. The conjugate of a complex number c = a + bi is c = a – bi, in other terms imaginary part multiplied by -1.

Here is a example for the **Polar and Conjugate** functions,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <complex> int main() { std::complex<double> c1(6.0, 3.0); c = std::conj(c1); std::cout << "Conjugate of c1: " << c.real() << " + " << c.imag() << "i\n"; c = std::proj(c1); std::cout << "Projection of c1: " << c.real() << " + " << c.imag() << "i\n"; getchar(); return 0; } |

#### Projection Function

In C++, **proj()** function returns the projection of c complex number in to the Riemann sphere. The projection of c is c, except for complex infinities, that are mapped to the complex value with a real component of infinity. Imaginary part can be 0.0 or -0.0, depending on the sign of the imaginary component of complex number c. See this example below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <complex> int main() { std::complex<double> c; std::complex<double> c1(6.0, 3.0); c = std::proj(c1); std::cout << "Projection of c1: " << c.real() << " + " << c.imag() << "i\n"; c = std::complex<double> (INFINITY, -2); std::cout << "Projection of " << c << " is " << std::proj(c) << '\n'; c = std::complex<double> (1, -INFINITY); std::cout << "Projection of " << c << " is " << std::proj(c) << '\n'; getchar(); return 0; } |

**Do you want to try out these examples? Why not download a free trial of C++ Builder today?**