C++ complex abs function


The C++ <complex> ‘abs’ function compute the absolute value of the complex number.The declaration of the ‘abs’ function is shown below.

template<class T> T abs(const complex<T>& x);

Parameters:
x -The complex number whose magnitude is to be computed.

Return type
T -The absolute value of ‘x’.

Some points to note:

i)The ‘abs’ compute the square root of the sum of the square of the real part and imaginary part,

C++ complex abs function

The magnitude of a complex number is also known as modulus of the complex number.

Code example

complex< double > c1(3 , 4) , c1( 2.34 , 5.67)

cout<< abs( c1 ) << endl ;
<< abs( c2 );

Output,

5
6.13388