Defined in header <complex>
template< class T > 
T abs( const complex<T>& z );

Returns the magnitude of the complex number z.


z - complex value

Return value

If no errors occur, returns the absolute value (also known as norm, modulus, or magnitude) of z.

Errors and special cases are handled as if the function is implemented as std::hypot(std::real(z), std::imag(z)).


#include <iostream>
#include <complex>
int main()
    std::complex<double> z(1, 1);
    std::cout << z << " cartesian is rho = " << std::abs(z)
              << " theta = " << std::arg(z) << " polar\n";


(1,1) cartesian is rho = 1.41421 theta = 0.785398 polar

See also

returns the phase angle
(function template)
constructs a complex number from magnitude and phase angle
(function template)
computes absolute value of an integral value (|x|)
absolute value of a floating point value (|x|)
computes square root of the sum of the squares of two given numbers (x2
applies the function std::abs to each element of valarray
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.