C++ Standard Library C++ STL Library

C++ <complex> - abs() Function



The C++ <complex> abs() function returns the absolute value of a complex number. The absolute value of a complex number is its magnitude (or modulus), defined as the distance between the Cartesian coordinates (0,0) and (real, imag) of z and (0,0).

This function is overloaded in <cstdlib> abs() function for integral types, in <cmath> abs() function for floating-point types, and in <valarray> abs() function for valarrays.

Syntax

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

Parameters

z Specify the complex number.

Return Value

Returns the absolute value of z.

Example:

The example below shows the usage of abs() function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z (8.0, 6.0);

  //display the absolute part of 
  //the complex number
  cout<<"Absolute value of z: "<<
      abs(z)<<"\n";

  return 0;
}

The output of the above code will be:

Absolute value of z: 10

❮ C++ <complex> Library