C++C++17Introduction to C++Learn C++

What You Need To Use Complex Numbers in C++ Software

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 i2 = −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++.

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,

We can use auto to define a complex variable

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

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,

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,

Output of this example will be as follows,

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

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

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,

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,

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,


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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?