floating-point absolute value function

You're viewing an older version of this page (#5716). View the current version.

View versions (4)

Interface

#include <math.h>

double fabs ( double x )
long double fabsl ( long double x )
float fabsf ( float x )

Description

The fabs functions compute the absolute value of a floating-point number x, which is given by:

$$\mathrm{fabs}(x) :=\begin{cases} x, & x \geq 0 \\ -x, & x < 0 \end{cases}$$
(1)
Example 1
Workings
#include <stdio.h>
#include <math.h>
int main(void)
{
  float number = -1234.0;
  printf("number: %.2f   absolute value: %.2f\n", number, fabs(number));
  return 0;
}
Solution

Output:

number: -1234.00   absolute value: 1234.00

Special Values

fabs ( &plusmn;0 ) returns 0.

fabs ( &plusmn;&infin; ) returns +&infin;.

See Also

abs, ceil, floor

Standards

The fabs functions conform to ISO/IEC 9899:1999(E).