absolute value of a complex number

View versions (3)

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
Problem

This program returns the absolute value of the complex number z=0.5+2i.

Workings

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

using std::complex;

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

Solution

Output:

|0.5 + 2 i| = 2.0616

The function is actually an alias for hypot(a, b) = \sqrt{a^2 + b^2}.