function

abs

<cmath>
     double abs (      double x );
      float abs (       float x );
long double abs ( long double x );
Compute absolute value
Returns the absolute value of x ( /x/ ).

These convenience abs overloads are exclusive of C++. In C, abs is only declared in <cstdlib> (and only operates on integral values).

This function is also overloaded in <complex> for complex numbers (see complex abs), and in <valarray> for valarrays (see valarray abs).

Parameters

x
Floating point value.

Return Value

The absolute value of x.

Portability

These overloads of abs are only available in C++.
In C, only the cstdlib version of exists (see cstdlib abs) - fabs can be used instead.

Example

1
2
3
4
5
6
7
8
9
10
11
// cmath's abs example
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
  cout << "The absolute value of 3.1416 is " << abs (3.1416) << endl;
  cout << "The absolute value of -10.6 is " << abs (-10.6) << endl;
  return 0;
}


Output:

The absolute value of 3.1416 is 3.1416
The absolute value of -10.6 is 10.6

See also