The C++ std::complex::abs() function is used to get the magnitude of a complex number. It returns a floating point number representing the distance of the complex number from the origin in the complex plane.
This is calculated as the square root of the sum of the squares of the real and imaginary parts.
SyntaxFollowing is the syntax for std::complex::abs() function.
abs (const complex<T>& x);Parameters
It returns the absolute value of the complex number x.
Exceptionsnone
Example 1In the following example, we are going to consider the basic usage of the abs() function.
#include <iostream> #include <complex> #include <cmath> int main() { std::complex < double > a(1.1, 1.2); double x = std::abs(a); std::cout << "Result : " << x << std::endl; return 0; }Output
Output of the above code is as follows −
Result : 1.62788Example 2
Consider the following example, where we are going to take the negative value and applying the abs().
#include <iostream> #include <complex> #include <cmath> int main() { std::complex < double > x(-2.1, -3.1); double a = std::abs(x); std::cout << "Result : " << a << std::endl; return 0; }Output
If we run the above code it will generate the following output −
Result : 3.74433Example 3
Let's look at the following example, where we are going to the consider the real part and applying the abs().
#include <iostream> #include <complex> #include <cmath> int main() { std::complex < double > y(1.0, 0.0); double b = std::abs(y); std::cout << "Result : " << b << std::endl; return 0; }Output
Following is the output of the above code −
Result : 1
complex.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4