CKTIS / C
fabs — C23 mathematical function
`fabs(x)` from `<math.h>` returns the absolute value of a `double`; `fabsf` and `fabsl` serve `float` and `long double`. Unlike integer `abs`, it has no unrepresentable positive counterpart problem for the most negative integer.
Version: C23
Syntax
double fabs(double x);fabs(x) from <math.h> returns the absolute value of a double; fabsf and fabsl serve float and long double. Unlike integer abs, it has no unrepresentable positive counterpart problem for the most negative integer.
The example evaluates fabs(-3.5), tests the sign of fabs(-0.0), and compares fabs(-2.0) with 2.0. Output 3.5 0 1 demonstrates a positive magnitude, removal of the zero sign, and an ordinary predicate use.
No domain or range error is reported for a finite argument, although the result still has the precision of the selected floating type. Do not use abs for a double, because that function takes int. Under Annex F, fabs(±0) is +0, fabs(±∞) is +∞, and a NaN remains a NaN.
Examples
#include <math.h>
#include <stdio.h>
int main(void) {
printf("%.1f %d %d\n", fabs(-3.5), signbit(fabs(-0.0)) != 0, fabs(-2.0) == 2.0);
return 0;
}
Result
3.5 0 1
The example evaluates `fabs(-3.5)`, tests the sign of `fabs(-0.0)`, and compares `fabs(-2.0)` with `2.0`. Output `3.5 0 1` demonstrates a positive magnitude, removal of the zero sign, and an ordinary predicate use.