C++ Complex::norm() function
The C++ std::complex::norm() function is used to return the squared magnitude of the complex number, calculated as the sum of the squares of its real and imaginary parts. For example, a=x+yi, where x and y are real and imaginary numbers, then the norm is calculated as a2+b2.
Syntax
Following is the syntax for std::complex::norm() function.
norm (const complex<T>& x); double norm (ArithmeticType x);
Parameters
- x − It indicates the complex value.
Return Value
It returns the norm value of the complex number x.
Exceptions
none
Example 1
In the following example, we are going to consider the basic usage of the norm() function.
#include <iostream>
#include <complex>
int main() {
std::complex < double > a(1.1, 1.3);
double x = std::norm(a);
std::cout << "Result : " << x << std::endl;
return 0;
}
Output
Output of the above code is as follows −
Result : 2.9
Example 2
Consider the following example, where we are going to use the norm() with the negative values.
#include <iostream>
#include <complex>
int main() {
std::complex < double > x(-2.1, -3.2);
double y = std::norm(x);
std::cout << "Result : " << y << std::endl;
return 0;
}
Output
Following is the output of the above code −
Result : 14.65
Example 3
In the following example, we are going to use the norm() on the real part.
#include <iostream>
#include <complex>
int main() {
std::complex < double > a(2.3, 0.0);
double b = std::norm(a);
std::cout << "Result : " << b << std::endl;
return 0;
}
Output
If we run the above code it will generate the following output −
Result : 5.29