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?

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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?