Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cmath (math.h)
functions:
abs
acos
asin
atan
atan2
ceil
cos
cosh
exp
fabs
floor
fmod
frexp
ldexp
log
log10
modf
pow
sin
sinh
sqrt
tan
tanh
macro constants:
HUGE_VAL


abs

function
<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/ ).

In C++, this function is overloaded in <cstdlib>, <complex> and <valarray> (see cstdlib abs, complex abs and 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