fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128
Header: <math.h>
1-6) Computes the absolute value of a floating-point value arg. The functions with decimal floating-point parameters are declared if and only if the implementation predefines STDC_IEC_60559_DFP (i.e. the implementation supports decimal floating-point numbers). (since C23)
# Declarations
float fabsf( float arg );
(since C99)
double fabs( double arg );
long double fabsl( long double arg );
(since C99)
_Decimal32 fabsd32( _Decimal32 arg );
(since C23)
_Decimal64 fabsd64( _Decimal64 arg );
(since C23)
_Decimal128 fabsd128( _Decimal128 arg );
(since C23)
#define fabs( arith )
(since C99)
# Parameters
arg: floating-point valuearith: floating-point or integer value
# Return value
If successful, returns the absolute value of arg ((\small |arg| )|arg|). The value returned is exact and does not depend on any rounding modes.
# Example
#include <math.h>
#include <stdio.h>
#define PI 3.14159
// This numerical integration assumes all area is positive.
double integrate(double f(double),
double a, double b, // assume a < b
unsigned steps) // assume steps > 0
{
const double dx = (b - a) / steps;
double sum = 0.0;
for (double x = a; x < b; x += dx)
sum += fabs(f(x));
return dx * sum;
}
int main(void)
{
printf("fabs(+3) = %f\n", fabs(+3.0));
printf("fabs(-3) = %f\n", fabs(-3.0));
// special values
printf("fabs(-0) = %f\n", fabs(-0.0));
printf("fabs(-Inf) = %f\n", fabs(-INFINITY));
printf("Area under sin(x) in [-PI, PI] = %f\n", integrate(sin, -PI, PI, 5101));
}