C++ Standard Library C++ STL Library

C++ <complex> - norm() Function



The C++ <complex> norm() function returns the squared magnitude of a complex number. It is defined as the sum of the squares of real and imaginary parts (without the imaginary unit) of the complex number.

The additional overloads is added in C++11 to provide the arguments of any fundamental arithmetic type. In this case, the function assumes the value has a zero imaginary component, and thus simply returns z converted to the proper type.

Syntax

template<class T> T norm (const complex<T>& z);
template<class T> T norm (const complex<T>& z); 

//additional overloads
double norm (ArithmeticType z);

Parameters

z Specify a complex value.

Return Value

Returns squared magnitude of the complex number.

Example:

The example below shows the usage of <complex> norm() function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z1 (3, 4);
  complex<double> z2 (3, 0);
  complex<double> z3 (0, 4);

  //calculating squared magnitude
  cout<<"norm(z1): "<<norm(z1)<<"\n";
  cout<<"norm(z2): "<<norm(z2)<<"\n";
  cout<<"norm(z3): "<<norm(z3)<<"\n";

  return 0;
}

The output of the above code will be:

norm(z1): 25
norm(z2): 9
norm(z3): 16

❮ C++ <complex> Library

5