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++.)

Answer from Baum mit Augen on Stack Overflow
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.

🌐
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)
🌐
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.
🌐
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 ...
🌐
BIKEPACKING.com
bikepacking.com › home › all-posts › have you seen the new ron’s bikes fab’s abs?
Have You Seen the New Ron's Bikes Fab's Abs? - BIKEPACKING.com
July 5, 2022 - Connecticut-based Ron’s Bikes, owned by Ronnie Romance and Arya Namz, just added a new product to their lineup of handlebar and saddle bags. The new Fab’s Abs is a pared-down version of their Fabio’s Fanny, based on an expandable roll-top and adjustable bungee closure.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
August 9, 2021 - The difference is that math.fabs(number) will always return a floating-point number even if the argument is an integer, whereas abs() will return a floating-point or an integer depending upon the argument.
Find elsewhere
🌐
Quora
quora.com › What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
Fast site, predictable costs — no surprises. ... abs() and fabs() both return the absolute value of a number, but they differ in types, headers, return types, overloads, and portability.
🌐
SysTutorials
systutorials.com › docs › linux › man › 3-std::abs(float),std::fabs,std::fabsf,std::fabsl
std::abs(float),std::fabs,std::fabsf,std::fabsl: std::abs(float),std::fabs,std::fabsf,std::fabsl - Linux Manuals (3)
Defined in header <cmath> Defined in header <cstdlib> (since C++17) float abs( float arg ); (1) double abs( double arg ); (2) long double abs( long double arg ); (3) Defined in header <cmath> float fabs ( float arg ); float fabsf( float arg ); (since C++11) double fabs ( double arg ); (4) (5) long double fabs ( long double arg ); long double fabsl( long double arg ); (6) (since C++11) double fabs ( IntegralType arg ); (7) (since C++11)
🌐
SACO Evaluator
saco-evaluator.org.za › docs › cppreference › en › cpp › numeric › math › fabs.html
std::abs(float), std::fabs - cppreference.com
#include <iostream> #include <cmath> int main() { std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\n' << "abs(-3.0) = " << std::abs(-3.0) << '\n'; // special values std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\n' << "abs(-Inf) = " << std::abs(-INFINITY) << '\n'; }
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › fabs.3.html
fabs(3) - Linux manual page
These functions return the absolute value of x. If x is a NaN, a NaN is returned. If x is -0, +0 is returned.
🌐
Coin-or
coin-or.org › CppAD › Doc › abs.htm
AD Absolute Value Functions: abs, fabs
abs function is the sign function; i.e., @[@ {\rm abs}^{(1)} ( x ) = {\rm sign} (x ) = \left\{ \begin{array}{rl} +1 & {\rm if} \; x > 0 \\ 0 & {\rm if} \; x = 0 \\ -1 & {\rm if} \; x < 0 \end{array} \right. @]@ The result for ... == 0 used to be a directional derivative.
🌐
TutorialsPoint
tutorialspoint.com › python-fabs-vs-abs
Python - fabs() vs abs()
July 22, 2020 - The Python math.fabs() method is used to calculate the float absolute value of a number. The result of this method is never negative; even if the number is a negative value, the method will return the negation of it.
🌐
Apple Developer
developer.apple.com › forums › thread › 90710
What does the function "fabs(_:)" … | Apple Developer Forums
This is a common gotcha in C programming, because if "myValue" is a variable of type "double" (say), and you write "double result = abs (myValue)", it looks like you are taking the absolute value, but the C compiler [correctly and automatically] converts the double to an int by truncating it, computes the absolute value of the truncated integer, converts it back to a double, and this isn't the answer you wanted.