fabs is one of a family of type-specific names from C. The C++ standard libraries provide these names, fabsf for float, fabs for double and fabsl for long double, for compatibility with C, so that barring other issues C code that uses these names can be compiled as C++. However, the C++ library provides corresponding overloads of the single name abs, which one should use in C++. There is an issue with abs, namely that the overloads for integral argument types reside in (or for the name placed in the std namespace, in ). So, for using abs one should better include both and . And since in C++17 and later the header provides some functions that are not provided by , if one writes a wrapper to include standard library numerical stuff headers then it should best include also . Answer from alfps on reddit.com
🌐
Reddit
reddit.com › r/cpp_questions › difference between abs() and fabs(), and can i replace with each other in the program?? and abs() fn is kinda not working.....
r/cpp_questions on Reddit: Difference between abs() and fabs(), and can i replace with each other in the program?? And abs() fn is kinda not working.....
November 26, 2019 - fabs is one of a family of type-specific names from C. The C++ standard libraries provide these names, fabsf for float, fabs for double and fabsl for long double, for compatibility with C, so that barring other issues C code that uses these ...
Top answer
1 of 3
49

In C++, std::abs is overloaded for both signed integer and floating point types. std::fabs only deals with floating point types (pre C++11). Note that the std:: is important; the C function ::abs that is commonly available for legacy reasons will only handle int!

The problem with

float f2= fabs(-9);

is not that there is no conversion from int (the type of -9) to double, but that the compiler does not know which conversion to pick (int -> float, double, long double) since there is a std::fabs for each of those three. Your workaround explicitly tells the compiler to use the int -> double conversion, so the ambiguity goes away.

C++11 solves this by adding double fabs( Integral arg ); which will return the abs of any integer type converted to double. Apparently, this overload is also available in C++98 mode with libstdc++ and libc++.

In general, just use std::abs, it will do the right thing. (Interesting pitfall pointed out by @Shafik Yaghmour. Unsigned integer types do funny things in C++.)

2 of 3
19

With C++ 11, using abs() alone is very dangerous:

#include <iostream>
#include <cmath>

int main() {
    std::cout << abs(-2.5) << std::endl;
    return 0;
}

This program outputs 2 as a result. (See it live)

Always use std::abs():

#include <iostream>
#include <cmath>

int main() {
    std::cout << std::abs(-2.5) << std::endl;
    return 0;
}

This program outputs 2.5.

You can avoid the unexpected result with using namespace std; but I would adwise against it, because it is considered bad practice in general, and because you have to search for the using directive to know if abs() means the int overload or the double overload.

Discussions

What is the difference between abs() and fabs() functions?
What is the difference between abs() and fabs() functions in C language? More on youth4work.com
🌐 youth4work.com
5
0
fabs versus abs? - C++ Forum
I would like to know why there's 3 math functions to calculate the absolute value of a number: fabs(), labs() and abs(), this last one is for integral types. I think this is a completely stupid thing. It doesn't matter what kind of number we have, the steps to achieve the absolute value are ... More on cplusplus.com
🌐 cplusplus.com
June 22, 2014
c++ - When do I use fabs and when is it sufficient to use std::abs? - Stack Overflow
I assume that abs and fabs are behaving different when using math.h. But when I use just cmath and std::abs, do I have to use std::fabs or fabs? Or isn't this defined? More on stackoverflow.com
🌐 stackoverflow.com
Fabs and abs
in cint it seems that abs and fabs are different function root [6] abs(1.2234234) (const int)1 root [7] fabs(1.2234234) (const double)1.22342339999999994e+00 but this is not the behaviour of c++ codepad.org/gM7JzQ2L More on root-forum.cern.ch
🌐 root-forum.cern.ch
0
0
July 10, 2012
🌐
Quora
quora.com › What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
In C++, wzxhzdk:6 provides int ... for integral ... abs() and fabs() both return the absolute value of a number, but they differ in types, headers, return types, overloads, and portability....
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
March 14, 2025 - 1-4) Computes the absolute value of the floating-point value num. The library provides overloads of std::abs and std::fabs for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)
🌐
Programiz
programiz.com › c-programming › library-function › math.h › fabs
C fabs() - C Standard Library
Become a certified C programmer. Try Programiz PRO! ... The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double).
🌐
Youth4work
youth4work.com › talent › c language › forum › what is the difference between abs() and fabs() functions?
What is the difference between abs() and fabs() functions?
You can easily figure out the difference. ... Both functions are to retrieve absolute value. abs() is for integer values and fabs() is for floating type numbers.
🌐
Cplusplus
cplusplus.com › forum › beginner › 136274
fabs versus abs? - C++ Forum
June 22, 2014 - C++ provides overload of abs() for all arithmetic types. fabs() and labs() are provided for backward compatibility with C, which does not have function overloading and had to have different fucntions for different types.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fabs.htm
C library - fabs() function
Here, we calculate the approximate absolute value of long double numbers. The result of approximate number can be identify using the formula tanh = (1.0 + tanh(x / 2.0)) * (1.0 - tanh(x / 2.0). #include <stdio.h> #include <math.h> long double my_exp(long double x) { return (1.0 + tanh(x / 2.0)) * (1.0 - tanh(x / 2.0)); } int main() { long double a = -6.546; long double b = 5.980; double res; res = fabs(a); printf("The absolute value of %.3Lf is %.3lf\n", a, res); res = fabs(b); printf("The absolute value of %.3Lf is %.3lf\n", b, res); return 0; }
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
/* Example using fabs by TechOnTheNet.com */ #include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ double value; double result; /* Assign the value we will find the fabs of */ value = -2.1; /* Calculate the absolute value of value */ result = fabs(value); /* Display the result of the calculation */ printf("The Absolute Value of %f is %f\n", value, result); return 0; } When compiled and run, this application will output:
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › fabs-fabsf-fabsl
fabs, fabsf, fabsl | Microsoft Learn
July 28, 2025 - The fabs functions return the absolute value of the argument x. There's no error return. C++ allows overloading, so you can call overloads of fabs if you include the <cmath> header.
🌐
Scaler
scaler.com › home › topics › fabs() function in c
fabs() Function in C - Scaler Topics
November 7, 2023 - fabs in C function takes a number as input and returns the magnitude of that number, i.e., ignore the sign of the number and return its absolute value.
🌐
OpenGenus
iq.opengenus.org › fabs-and-abs-in-cpp
Fabs and abs in C++
January 12, 2023 - Mathematically, it is represented as: fabs(num) = |num| Abs in C++ used to compute the absolute value of a number. The function takes a single argument, which is the number for which the absolute value is to be calculated, and it returns the absolute value of that number as its result.
🌐
Codecademy
codecademy.com › docs › c++ › math functions › fabs()
C++ (C Plus Plus) | Math Functions | fabs() | Codecademy
April 13, 2025 - fabs() is designed for floating-point types (float, double, long double) while abs() is typically used with integer types. Using abs() with floating-point values may cause implicit type conversion and potential precision loss.
🌐
YouTube
youtube.com › watch
Absolute Value Functions abs() And fabs() | C Programming Tutorial - YouTube
How to find the absolute value of a number using the functions abs() and fabs() in C. Source code: https://github.com/portfoliocourses/c-example-code/blob/m...
Published   November 4, 2022
🌐
Cplusplus
cplusplus.com › reference › cmath › fabs
fabs
double fabs (double x); float fabs (float x);long double fabs (long double x); double fabs (T x); // additional overloads for integral types · Compute absolute value · Returns the absolute value of x: |x|. Header <tgmath.h> provides a type-generic macro version of this function.
🌐
Sololearn
sololearn.com › en › Discuss › 2784928 › fabs-and-abs
fabs and abs | Sololearn: Learn to code for FREE!
May 15, 2021 - f in fabs stands for float The difference is that math.fabs(number) will always return a floating point number even if the argument is integer, whereas abs() will return a floating point or an integer depending upon the argument. src: geeksforgeeks ...
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - The fabs() function returns the absolute value of its floating-point argument x; if x is greater than or equal to 0, the return value is equal to x. If x is less than 0, the function returns -x. float x = 4.0F * atanf( 1.0F ); long double y ...
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
CERN
root-forum.cern.ch › t › fabs-and-abs › 14556
Fabs and abs - ROOT - ROOT Forum
July 10, 2012 - in cint it seems that abs and fabs are different function root [6] abs(1.2234234) (const int)1 root [7] fabs(1.2234234) (const double)1.22342339999999994e+00 but this is not the behaviour of c++ codepad.org/gM7JzQ2L