C++ std::fabs() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::fabs() returns the absolute value of a decimal number. The name means "floating-point absolute value". It makes any negative double positive and leaves a positive one alone.

It is the same idea as std::abs(), but its name makes it clear that you are working with decimals. It was in C before C++ got the overloaded abs(), so you will see it in a lot of older code.

std::fabs() is declared in <cmath> and is part of the group Absolute value and sign. These functions work with the sign of a number. abs() and fabs() remove a minus sign, and copysign() and signbit() copy or read it.

Syntax

Include <cmath> and call std::fabs. Start with these lines.

C++
double a = std::fabs(-4.5);    // double version
float  b = std::fabs(-4.5f);   // float version
CallWhat it does
std::fabs(-4.5)Passes a double and returns a double. Here the result is 4.5.
std::fabs(-4.5f)Passes a float, so the float overload runs and returns a float.
std::fabs(-4)An integer is converted to double. Returns 4 as a double.

Write std::fabs so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       fabs(float x);
double      fabs(double x);
long double fabs(long double x);
double      fabs(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. A float, double, or long double. An integer is converted to double.

Return Value

Return ValueDescription
Absolute valueThe magnitude of x with a positive sign.
+0.0Returned for both 0.0 and -0.0.
inf / NaNfabs(-inf) is inf. fabs(NaN) is NaN.

C++ Examples

Example 1 — Absolute Value of Several Decimals

Loop over a few numbers, including negative zero, and print the original next to its fabs() result.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {-3.5, 0.0, 2.25, -0.0};

    for (double v : values) {
        std::cout << "fabs(" << v << ") = " << std::fabs(v) << '\n';
    }
    return 0;
}

How It Works

  1. The range-based for sends each value in the array to std::fabs(), one at a time.
  2. A negative value such as -3.5 loses its sign. A positive value such as 2.25 stays the same.
  3. The last value is negative zero. It prints as -0, but fabs() returns plain 0, so the sign is cleared even for zero.

Example 2 — Total and Average Forecast Error

Add up how far each forecast was from the real value. Errors in both directions must count as positive, so use fabs().

C++
#include <cmath>
#include <iostream>

int main() {
    double forecast[] = {10.5, 12.0, 9.5};
    double actual[]   = {11.0, 10.5, 9.5};
    double total = 0.0;

    for (int i = 0; i < 3; ++i) {
        total += std::fabs(forecast[i] - actual[i]);
    }
    std::cout << "Total error: " << total << '\n';
    std::cout << "Average error: " << total / 3 << '\n';
    return 0;
}

How It Works

  1. The three differences are -0.5, 1.5, and 0. Without fabs() the first would cancel part of the second.
  2. fabs() makes every difference positive, so 0.5 + 1.5 + 0 gives a total of 2.
  3. Dividing by 3 gives the average, 0.666667. This is called the mean absolute error.

Example 3 — Infinity and NaN

Check what fabs() does with the special values that a double can hold.

C++
#include <cmath>
#include <iostream>
#include <limits>

int main() {
    double inf = std::numeric_limits<double>::infinity();
    double bad = std::nan("");

    std::cout << "fabs(-inf) = " << std::fabs(-inf) << '\n';
    std::cout << "fabs(NaN) is NaN: " << std::isnan(std::fabs(bad)) << '\n';
    std::cout << "fabs(-1e308 * 10) = " << std::fabs(-1e308 * 10) << '\n';
    return 0;
}

How It Works

  1. std::numeric_limits<double>::infinity() is a real value, and fabs(-inf) turns minus infinity into plain inf.
  2. std::nan("") creates a NaN. fabs() keeps it as NaN, so std::isnan() prints 1.
  3. -1e308 * 10 overflows to -inf before fabs() is called, so the final answer is inf.

Frequently Asked Questions

std::fabs() returns the absolute value of a floating-point number. It removes the minus sign and keeps the size, so -4.5 becomes 4.5.
The absolute value as float, double, or long double. For -0.0 the result is +0.0, for -inf it is inf, and for NaN it is NaN.
For decimals, yes. std::fabs(x) and std::abs(x) give the same result for a double. fabs() only works with floating-point values, while abs() also has integer versions.
No. It returns a new value. Write x = std::fabs(x); if you want to replace the variable.
Include <cmath> and write std::fabs(). That always works and picks the C++ overloads. The global name ::fabs often works too, but the standard does not promise it, and it may only take a double.

Next: fdim()

Keep going through the <cmath> functions. Positive difference of two numbers (never below 0).

fdim() tutorial →

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.

7 people found this page helpful