C Math fabs() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 Syntax

Standard C declaration:

C
double fabs(double x);

Related variants

C
float fabsf(float x);           /* <math.h> */
long double fabsl(long double x); /* <math.h> */

Parameters

  • x — any floating-point value (double).

Return Value

  • Non-negative magnitude of x, returned as double.
  • fabs(-12.34) returns 12.34; fabs(0.0) returns 0.0.
  • fabs(-0.0) returns positive zero. fabs(NaN) returns NaN.

Headers and linking

  • #include <math.h>
  • Compile: gcc fabs.c -std=c11 -o fabs -lm

⚡ Quick Reference

CallInputResult
fabs(-12.34)Negative12.34
fabs(3.5)Positive3.5
fabs(0.0)Zero0.0
fabs(a - b)DifferenceDistance
abs(-5)int (stdlib)5 (not for double)
Double
fabs(x)

math.h

Float
fabsf(x)

float abs

Integer
abs(x)

stdlib.h

2D distance
hypot(dx, dy)

Pythagoras

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Absolute value is one of the most common math helpers in real programs.

📚 Getting Started

Remove the sign from a negative floating-point number.

Example 1 — Absolute Value of a Negative Number

Apply fabs to -12.34.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double num = -12.34;
    double absolute = fabs(num);

    printf("Input:  %f\n", num);
    printf("|x| =   %f\n", absolute);

    return 0;
}

How It Works

fabs flips the sign of negative values and leaves non-negative values unchanged.

Example 2 — Positive Input Stays Unchanged

Absolute value of a number already positive.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double values[] = { 5.0, -5.0, 0.0 };
    size_t i;

    for (i = 0; i < 3; i++) {
        printf("fabs(%5.1f) = %5.1f\n", values[i], fabs(values[i]));
    }

    return 0;
}

How It Works

Positive and zero inputs pass through unchanged. Only negatives get negated.

📈 Practical Patterns

Distance, tolerance checks, and type safety.

Example 3 — Distance Between Two Points on a Line

Use fabs(a - b) for one-dimensional distance.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double a = 10.5;
    double b = 3.2;
    double distance = fabs(a - b);

    printf("Point A: %.1f\n", a);
    printf("Point B: %.1f\n", b);
    printf("Distance: %.1f\n", distance);

    return 0;
}

How It Works

Order does not matter: fabs(a - b) equals fabs(b - a). For 2D distance, use hypot(dx, dy) instead.

Example 4 — Floating-Point Comparison with Tolerance

Check if two doubles are “close enough” using fabs.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double expected = 0.1 + 0.2;
    double actual = 0.3;
    double epsilon = 1e-9;
    int close;

    close = fabs(expected - actual) < epsilon;

    printf("expected = %.17f\n", expected);
    printf("actual   = %.17f\n", actual);
    printf("Close?    %s\n", close ? "yes" : "no");

    return 0;
}

How It Works

Floating-point arithmetic is inexact. Never use == for doubles—compare fabs(a - b) < epsilon instead.

Example 5 — fabs() vs abs() — Why Type Matters

Using the wrong function truncates and loses the decimal part.

C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double x = -12.34;

    printf("x = %.2f\n", x);
    printf("fabs(x) = %.2f  (correct for double)\n", fabs(x));
    printf("abs(x)  = %d     (wrong: truncates to int first)\n", abs((int)x));

    return 0;
}

How It Works

abs() from stdlib.h is for integers. For any floating-point type, use fabs, fabsf, or fabsl.

🚀 Common Use Cases

  • Distancefabs(a - b) on a line; hypot in 2D.
  • Error measurementfabs(measured - expected).
  • Floating-point equality — tolerance checks with epsilon.
  • Graphics — magnitude of velocity, force, or offset components.
  • Signal processing — envelope detection, amplitude without phase sign.

🧠 How fabs() Works

1

Receive double x

Any floating-point value including NaN and infinity.

Input
2

Clear sign bit

Implementation clears the sign or compares against zero.

Compute
3

Return magnitude

Non-negative double with same size as input.

Output
=

Absolute value

Distance from zero—always ≥ 0 (except NaN).

📝 Notes

  • Declared in <math.h>, not <stdlib.h>.
  • For int, use abs(); for long, labs(); for long long, llabs().
  • fabs(-0.0) returns +0.0.
  • fabs(NaN) returns NaN; fabs(±INFINITY) returns INFINITY.
  • Link with -lm on GCC/Clang Unix-like builds.
  • Macro fabs may exist in some headers—including math.h gives the function.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Use fabs for double magnitudes
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about fabs()

Floating-point absolute value in C, explained simply.

5
Core concepts
📚 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.

Explore C Math Functions

Continue with floor() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful