Name

fabs

Synopsis

Obtains the absolute value of a number

#include <math.h>
doublefabs( double x );
float fabsf( float x );
long double fabsl( long double x );

The fabs() function returns the absolute value of its floating-point argument x; if x is greater than or equal to 0, the return value is equal to x. If x is less than 0, the function returns -x.

Example

float x = 4.0F * atanf( 1.0F );
long double y = 4.0L * atanl( 1.0L );

if ( x == y )
  printf( "x and y are exactly equal.\n" );
else if (fabs( x − y ) < 0.0001 * fabsl( y ) )
  printf( "x and y are approximately equal:\n"
          "x is %.8f; y is %.8Lf.\n", x, y );

This code produces the following output:

x and y are approximately equal:
x is 3.14159274; y is 3.14159265.

See Also

The absolute value functions for integer types: abs(), labs(), llabs(), and imaxabs(); the absolute value functions for complex numbers: cabs(), cabsf(), cabsl(); the C99 functions fdim() and copysign(); the functions fmax() and fmin()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.