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 ...
๐ŸŒ
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.
Discussions

c++ - What's the difference between abs and fabs? - Stack Overflow
I checked the difference between abs and fabs on python here As I understand there are some difference regarding the speed and the passed types, but my question related to native c++ on V.S. Rega... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
python - abs() vs fabs() speed difference and advantage of fabs() - Stack Overflow
I ran some simple tests on abs() and fabs() functions and I don't understand what are the advantages of using fabs(), if it is: 1) slower 2) works only on floats 3) will throw an exception if us... More on stackoverflow.com
๐ŸŒ stackoverflow.com
abs() vs fabs()
Hi, I noticed that the C++ compiler (pgCC 10.3-0 64-bit target on x86-64 Linux -tp shanghai-64) behaves somewhat unexpectedly when trying to compile #include int main() { double a = 5.0; std::abs(a); return 0; } The error message "test-fabs.cpp", line 6: error: more than one instance of overloaded ... More on forums.developer.nvidia.com
๐ŸŒ forums.developer.nvidia.com
0
0
December 25, 2010
Top answer
1 of 5
139

math.fabs() converts its argument to float if it can (if it can't, it throws an exception). It then takes the absolute value, and returns the result as a float.

In addition to floats, abs() also works with integers and complex numbers. Its return type depends on the type of its argument.

In [7]: type(abs(-2))
Out[7]: int

In [8]: type(abs(-2.0))
Out[8]: float

In [9]: type(abs(3+4j))
Out[9]: float

In [10]: type(math.fabs(-2))
Out[10]: float

In [11]: type(math.fabs(-2.0))
Out[11]: float

In [12]: type(math.fabs(3+4j))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/npe/<ipython-input-12-8368761369da> in <module>()
----> 1 type(math.fabs(3+4j))

TypeError: can't convert complex to float
2 of 5
10

Edit: as @aix suggested, a better (more fair) way to compare the speed difference:

In [1]: %timeit abs(5)
10000000 loops, best of 3: 86.5 ns per loop

In [2]: from math import fabs

In [3]: %timeit fabs(5)
10000000 loops, best of 3: 115 ns per loop

In [4]: %timeit abs(-5)
10000000 loops, best of 3: 88.3 ns per loop

In [5]: %timeit fabs(-5)
10000000 loops, best of 3: 114 ns per loop

In [6]: %timeit abs(5.0)
10000000 loops, best of 3: 92.5 ns per loop

In [7]: %timeit fabs(5.0)
10000000 loops, best of 3: 93.2 ns per loop

In [8]: %timeit abs(-5.0)
10000000 loops, best of 3: 91.8 ns per loop

In [9]: %timeit fabs(-5.0)
10000000 loops, best of 3: 91 ns per loop

So it seems abs() only has slight speed advantage over fabs() for integers. For floats, abs() and fabs() demonstrate similar speed.


In addition to what @aix has said, one more thing to consider is the speed difference:

In [1]: %timeit abs(-5)
10000000 loops, best of 3: 102 ns per loop

In [2]: import math

In [3]: %timeit math.fabs(-5)
10000000 loops, best of 3: 194 ns per loop

So abs() is faster than math.fabs().

๐ŸŒ
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.
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)
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ c++ โ€บ math functions โ€บ fabs()
C++ (C Plus Plus) | Math Functions | fabs() | Codecademy
April 13, 2025 - Using abs() with floating-point values may cause implicit type conversion and potential precision loss. No, fabs() doesnโ€™t work with complex numbers.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-fabs-vs-abs
Python - fabs() vs abs()
July 22, 2020 - Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex numbers as its arguments.
๐ŸŒ
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.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ abs-vs-fabs-methods.aspx
Difference between abs() and fabs() methods in Python
In python, abs() method and fabs() method both are used to find the absolute value of a number.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
NVIDIA Developer Forums
forums.developer.nvidia.com โ€บ accelerated computing โ€บ hpc compilers โ€บ legacy pgi compilers
abs() vs fabs() - Legacy PGI Compilers - NVIDIA Developer Forums
December 25, 2010 - Hi, I noticed that the C++ compiler (pgCC 10.3-0 64-bit target on x86-64 Linux -tp shanghai-64) behaves somewhat unexpectedly when trying to compile #include int main() { double a = 5.0; std::abs(a); return 0; } The error message "test-fabs.cpp", line 6: error: more than one instance of overloaded function "std::abs" matches the argument list: function "abs(int)" function "std::abs(long double)" argument types are: (double) ...
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ fabs-and-abs-in-cpp
Fabs and abs in C++
January 12, 2023 - For fabs, the function is applied to a floating-point number and it's just a matter of extracting the bits of the number and returning the same number with the sign bit set to positive. For abs, the function is applied to an integer, and it's just a matter of checking the sign of the number and returning the same number with the sign bit set to positive if it's negative, or the number itself otherwise.
๐ŸŒ
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.
๐ŸŒ
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.