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

Copyfloat 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

Copyfloat 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:

Copy#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():

Copy#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.

🌐
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 ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 136274
fabs versus abs? - C++ Forum
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.
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
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)
🌐
Quora
quora.com › What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
The only difference between both of them is, abs() is used to calculate the absolute value for integer type numbers whereas fabs() are used for floating type numbers. abs() function is use un...
🌐
Codecademy
codecademy.com › docs › c++ › math functions › fabs()
C++ (C Plus Plus) | Math Functions | fabs() | Codecademy
April 13, 2025 - In this example, fabs() is used ... floating-point precision issues: ... fabs() is designed for floating-point types (float, double, long double) while abs() is typically used with integer types....
🌐
Sololearn
sololearn.com › en › Discuss › 2784928 › fabs-and-abs
fabs and abs | Sololearn: Learn to code for FREE!
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 ...
🌐
CodeSpeedy
codespeedy.com › home › difference between fabs and abs function in c++
Difference between fabs and abs function in C++ - CodeSpeedy
September 14, 2022 - The difference between the fabs() and abs() function is that fabs() function is used for floating values and abs() function is used for integer values. abs() function returns the absolute value for the integer.
🌐
Krayonnz
krayonnz.com › user › doubts › detail › 61894a1591cca20041786b9f › difference-between-abs-and-fabs-functions-in-C
Difference between abs() and fabs() functions in C ?
The fastest growing social learning network of students. Participate in quizzes and competitions to win cash & exciting prizes. Host your college competitions & quizzes. Buy & Sell verified notes. Ask & answer doubts.
Find elsewhere
🌐
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.
🌐
Sololearn
sololearn.com › en › Discuss › 1799301 › qus10what-is-the-difference-between-abs-and-fabs-functions
Qus10:-What is the difference between abs() and fabs() functions? | Sololearn: Learn to code for FREE!
Hi if you have a float number, you have to use fabs() if you have an integer number, you have to use abs() exemple with fabs() : fabs(239.14) return 239.14 fabs(-239.14) return 239.14 exemple with abs() : abs(239) return 239 abs(-239) return 239
🌐
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
🌐
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.
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - Parameter: It will take a single parameter which is to be converted to an absolute value. Return Value: While passing values to fabs(), we can convert the number explicitly to double.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › fabs-fabsf-fabsl
fabs, fabsf, fabsl | Microsoft Learn
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.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1811170-abs-function-equivalent-in-c
abs function equivalent in c++ - MATLAB Answers - MATLAB Central
June 23, 2022 - It was common for fabs to be implemented as a macro, but that led to arguments, especially as ieee 754 double precision defines an fabs hardware operation that does not require any branching. ... https://www.mathworks.com/matlabcentral/answers/1811170-abs-function-equivalent-in-c#comment_2378990
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
The fabs function returns the absolute value of a floating-point number represented by x. In the C Language, the required header for the fabs function is: