You should be very familiar with the primitive numeric datatypes such as int
and
double
. These are obviously very useful for doing calculations. Unfortunately as electronic
engineers, we often have to use complex numbers. Fortunately, the C++ standard library contains a complex number class.
It can be used by including the following header.
#include <complex>
This simple example below demonstrates how to create a complex number object and use some of the class methods to get the real, imaginary, absolute and phase of the number.
#include <complex> #include <iostream> int main() { std::complex<double> v(2.0, -1.0); std::cout << "Real part = " << v.real() << std::endl; std::cout << "Imaginary part = " << v.imag() << std::endl; std::cout << "Absolute value = " << std::abs(v) << std::endl; std::cout << "Phase angle = " << std::arg(v) << std::endl; }
This would produce the following output.
Real part = 2 Imaginary part = -1 Absolute value = 2.23607 Phase angle = -0.463648