The fabs() function returns the absolute value of a floating-point number—its distance from zero, without the sign. The name stands for floating-point absolute. Use it for distances, errors, and any time only magnitude matters.
01
|x|
Magnitude.
02
math.h
Link -lm.
03
double
Float abs.
04
vs abs()
For int.
05
Distance
|a − b|.
06
Variants
fabsf/fabsl.
Fundamentals
Definition and Usage
The absolute value of a number is its magnitude without a sign. On the number line, fabs(-12.34) and fabs(12.34) both equal 12.34—the distance from zero.
fabs works on double values. For int, use abs() from <stdlib.h>. For float and long double, use fabsf() and fabsl().
💡
Beginner Tip
Never call abs() on a double—it truncates to int first. For -12.34, use fabs(-12.34) to get 12.34, not abs(-12.34) which gives 12.
Macro fabs may exist in some headers—including math.h gives the function.
Performance
⚡ Optimization
fabs() is typically a single instruction or inlined bit operation on modern CPUs. It is faster and clearer than manual if (x < 0) x = -x. Do not micro-optimize further unless profiling shows it in a critical hot path.
Wrap Up
Conclusion
fabs() gives the magnitude of a double, stripping the sign. Use it for distances, errors, and tolerant comparisons—and reach for abs() only when working with integers.
Continue with floor() for rounding, or review abs() for integer absolute values.
Match type: fabsf for float, fabsl for long double
Compare floats with fabs(a - b) < epsilon
Include <math.h> and link -lm
Use fabs(a - b) for 1D distance
❌ Don’t
Call abs() on floating-point values
Compare doubles with == without tolerance
Assume fabs is in stdlib.h
Negate manually when fabs is clearer
Forget NaN: fabs(NaN) is still NaN
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about fabs()
Floating-point absolute value in C, explained simply.
5
Core concepts
🔢01
|x|
Magnitude.
Basics
📚02
math.h
Not stdlib.
Header
📈03
vs abs()
int only.
Types
📄04
Distance
|a − b|.
Pattern
🔄05
epsilon
Float compare.
Tip
❓ Frequently Asked Questions
fabs() returns the absolute value (non-negative magnitude) of a double. For example, fabs(-12.34) is 12.34 and fabs(3.5) is still 3.5. The name means floating-point absolute.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double fabs(double x). Integer absolute value uses abs() from <stdlib.h> instead.
fabs() works on double (and has fabsf/fabsl variants for float/long double) via math.h. abs() works on int via stdlib.h. Never use abs() on floating-point values—it truncates and can give wrong answers.
fabsf(x) takes a float and returns float. fabsl(x) takes a long double and returns long double. Use the variant that matches your variable type for consistency and to avoid unnecessary conversions.
fabs(NaN) returns NaN. fabs(INFINITY) and fabs(-INFINITY) both return positive INFINITY. fabs(-0.0) returns positive 0.0.
Use fabs() whenever you need magnitude without sign—distances, errors, tolerances. It is clearer than writing if (x < 0) x = -x; and handles edge cases like -0.0 correctly.
Did you know?
On many CPUs, fabs() is implemented by clearing a single sign bit in the IEEE-754 representation—no multiplication or branching needed. That is why it is both fast and the preferred way to get magnitude.