absolute value of a complex number

You're viewing an older version of this page (#3449). View the current version.

View versions (3)

NAME

abs

INTERFACE

#include <complex>

double abs(complex<T> z );

DESCRIPTION

The abs function returns the absolute value of the complex number z = a + {\rm i} b. The result is the real number denoted by |z| and defined through:

|z| = \sqrt{a^2+b^2}
(1)

Example 1

#include <stdio.h>
#include <complex>

using std::complex;
 
int main()
{
  complex<double> z;
  z=0.5+2i;
  printf("|0.5 + 2 i| = %.4lf\n", abs(z));
  return 0;
}

Output:

|0.5 + 2 i| = 2.0616

The function is actually an alias for hypot(a, b) = sqrt(a*a + b*b).