Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
complex
abs
arg
complex
conj
cos
cosh
exp
imag
log
log10
norm
polar
pow
real
sin
sinh
sqrt
tan
tanh


abs

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

Return absolute value of complex

Returns the absolute value of the complex number x.

The absolute value of a complex number is its magnitude (or modulus), defined as the theoretical distance between the coordinates (real,imag) of x and (0,0) (apply pythagorean theorem).

This function overloads cstdlib's abs and cmath's abs functions.

Parameters

x
Complex value.

Return value

Absolute value of x.
T is x's complex template type (i.e., its value type).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// abs complex example
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
  complex<double> mycomplex (3.0,4.0);
  cout << "The absolute value of " << mycomplex << " is " << abs(mycomplex) << endl;
  return 0;
} 


Output:

The absolute value of (3,4) is 5

See also