function
<cmath> <ctgmath>

fabs

double fabs (double x);
     double fabs  (double x);
      float fabsf (float x);
long double fabsl (long double x);
     double fabs (double x);
      float fabs (float x);
long double fabs (long double x);
     double fabs (double x);
      float fabs (float x);
long double fabs (long double x);
     double fabs (T x);           // additional overloads for integral types
Compute absolute value
Returns the absolute value of x: |x|.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double (defined for T being any integral type).

Parameters

x
Value whose absolute value is returned.

Return Value

The absolute value of x.

Example

1
2
3
4
5
6
7
8
9
10
/* fabs example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fabs */

int main ()
{
  printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) );
  printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) );
  return 0;
}


Output:

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

See also