The abs function in C++ is used to find the absolute value of a complex number. The absolute value of a complex number (also known as modulus) is the distance of that number from the origin in the complex plane. This can be found using the formula −
For complex number a+bi:
mod|a+bi| = √(a2+b2)
The abs() function returns the result of the above calculation in C++. It is defined in the complex library that is needed to be included.
#include <iostream> #include <complex> using namespace std; int main () { float a= 13.0 , b = 5.0; complex<double> complexnumber (a, b); cout << "The absolute value of " << a<<"+"<<b<<"i" << " is: "; cout << abs(complexnumber) << endl; return 0; }
The absolute value of 13+5i is: 13.9284