std::norm(std::complex)

From cppreference.com
< cpp‎ | numeric‎ | complex
 
 
 
 
Defined in header <complex>
(1)
template< class T >
T norm( const std::complex<T>& z );
(until C++20)
template< class T >
constexpr T norm( const std::complex<T>& z );
(since C++20)
(2)
float norm( float z );

template< class DoubleOrInteger >
double norm( DoubleOrInteger z );

long double norm( long double z );
(since C++11)
(until C++20)
constexpr float norm( float z );

template< class DoubleOrInteger >
constexpr double norm( DoubleOrInteger z );

constexpr long double norm( long double z );
(since C++20)
1) Returns the squared magnitude of the complex number z.
2) Additional overloads are provided for float, double, long double, and all integer types, which are treated as complex numbers with zero imaginary component.
(since C++11)

Parameters

z - complex value

Return value

the squared magnitude of z

Notes

The norm calculated by this function is also known as field norm or absolute square.

The Euclidean norm of a complex number is provided by std::abs, which is more costly to compute. In some situations, it may be replaced by std::norm, for example, if abs(z1) > abs(z2) then norm(z1) > norm(z2).

Example

#include <cassert>
#include <complex>
#include <iostream>
 
int main()
{
    constexpr std::complex<double> z{3, 4};
    static_assert(std::norm(z) == (z.real() * z.real() + z.imag() * z.imag()));
    static_assert(std::norm(z) == (z * std::conj(z)));
           assert(std::norm(z) == (std::abs(z) * std::abs(z)));
    std::cout << "std::norm(" << z << ") = " << std::norm(z) << '\n';
}

Output:

std::norm((3,4)) = 25

See also

returns the magnitude of a complex number
(function template)
returns the complex conjugate
(function template)
constructs a complex number from magnitude and phase angle
(function template)