In standard-conforming code that has included cmath and only calls std::abs on floats, doubles, and long doubles, there is no difference. However, it is instructive to look at the types returned by std::abs on various types when you call it with various sets of header files included.

On my system, std::abs(-42) is a double if I've included cmath but not cstdlib, an int if I've included cstdlib, and it produces a compilation error if I've included neither. Conversely, std::abs(-42.0) produces a compilation error (ambiguous overload) if I've included cstdlib but I haven't included cmath or a different compilation error (never heard of std::abs) if I've included neither.

On my platform, std::abs('x') gives me a double if I've included cmath or an int if I've included cstdlib but not cmath. Similar for a short int. Signedness does not appear to matter.

On my platform, the complex header apparently causes both the integral and the floating-point overloads of std::abs to be declared. I'm not certain this is mandated; perhaps you can find an otherwise reasonable platform on which std::abs(1e222) returns infinity with the wrong set of standard headers included.


The usual consequence of "you forgot a header in your program" is a compilation failure complaining of an undefined symbol, not a silent change in behaviour. With std::abs, however, the result can be std::abs(42) returning a double if you forgot cstdlib or std::abs('x') returning an int if you didn't. (Or perhaps you expected std::abs to give you an integral type when you pass it a short? Then, assuming my compiler got its promotion and overload resolution right, you had better make sure you don't include cmath.)

I have also spent too much time in the past trying to work out why code like double precise_sine = std::sin(myfloat) gives imprecise results. Because I don't like wasting my time on these sorts of surprises, I tend to avoid the overloaded variants of standard C functions in namespace std. That is, I use ::fabs when I want a double to be returned, ::fabsf when I want a float out, and ::fabsl when I want a long double out. Similarly for ::abs when I want an int, ::absl when I want a long, and ::absll when I want a long long.

Answer from tmyklebu on Stack Overflow
🌐
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)
Top answer
1 of 2
15

In standard-conforming code that has included cmath and only calls std::abs on floats, doubles, and long doubles, there is no difference. However, it is instructive to look at the types returned by std::abs on various types when you call it with various sets of header files included.

On my system, std::abs(-42) is a double if I've included cmath but not cstdlib, an int if I've included cstdlib, and it produces a compilation error if I've included neither. Conversely, std::abs(-42.0) produces a compilation error (ambiguous overload) if I've included cstdlib but I haven't included cmath or a different compilation error (never heard of std::abs) if I've included neither.

On my platform, std::abs('x') gives me a double if I've included cmath or an int if I've included cstdlib but not cmath. Similar for a short int. Signedness does not appear to matter.

On my platform, the complex header apparently causes both the integral and the floating-point overloads of std::abs to be declared. I'm not certain this is mandated; perhaps you can find an otherwise reasonable platform on which std::abs(1e222) returns infinity with the wrong set of standard headers included.


The usual consequence of "you forgot a header in your program" is a compilation failure complaining of an undefined symbol, not a silent change in behaviour. With std::abs, however, the result can be std::abs(42) returning a double if you forgot cstdlib or std::abs('x') returning an int if you didn't. (Or perhaps you expected std::abs to give you an integral type when you pass it a short? Then, assuming my compiler got its promotion and overload resolution right, you had better make sure you don't include cmath.)

I have also spent too much time in the past trying to work out why code like double precise_sine = std::sin(myfloat) gives imprecise results. Because I don't like wasting my time on these sorts of surprises, I tend to avoid the overloaded variants of standard C functions in namespace std. That is, I use ::fabs when I want a double to be returned, ::fabsf when I want a float out, and ::fabsl when I want a long double out. Similarly for ::abs when I want an int, ::absl when I want a long, and ::absll when I want a long long.

2 of 2
0

Is there any difference at all between std::abs and std::fabs when applied to floating point values?

No there is not. Nor is there a difference for integral types.

It is idiomatic to use std::abs() because it is closest to the commonly used mathematical nomenclature.

🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › fabs
C++ fabs() - C++ Standard Library
result = fabs(num); cout << "fabs(" << num << ") = |" << num << "| = " << result; return 0; } ... #include <iostream> #include <cmath> using namespace std; int main() { int num = -23; double result;
🌐
Lsu
ld2016.scusa.lsu.edu › 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'; } Possible output: abs(+3.0) = 3 abs(-3.0) = 3 abs(-0.0) = 0 abs(-Inf) = inf · Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/fabs&oldid=71300"
🌐
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'; }
🌐
GitHub
github.com › catchorg › Catch2 › issues › 543
fabs() vs std::fabs() causing build errors · Issue #543 · catchorg/Catch2
December 3, 2015 - /home/smex/NGL/DEV/5000core/../3rd_party/catch/catch.hpp: In function 'bool Catch::Detail::operator==(double, const Catch::Detail::Approx&)': /home/smex/NGL/DEV/5000core/../3rd_party/catch/catch.hpp:2140: error: 'fabs' was not declared in this scope cc: /opt/qnx650/host/linux/x86/usr/lib/gcc/arm-unknown-nto-qnx6.5.0/4.4.2/cc1plus error 1 · The issue was produced under GCC 4.4.2 with an empty .CPP file that has a single line to #include <catch/catch.hpp> The errors are all due to the use of the 'C' global namespace functions instead of the C++ std:: namespace functions such as fabs()
Author   ghost
🌐
Codecademy
codecademy.com › docs › c++ › math functions › fabs()
C++ (C Plus Plus) | Math Functions | fabs() | Codecademy
April 13, 2025 - The fabs() function in C++ calculates and returns the absolute value of a floating-point number. It returns the non-negative value of the input, effectively converting any negative number to its positive equivalent.
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › fabs.html
fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128 - cppreference.com
December 20, 2024 - #include <math.h> #include <stdio.h> #define PI 3.14159 // This numerical integration assumes all area is positive. double integrate(double f(double), double a, double b, // assume a < b unsigned steps) // assume steps > 0 { const double dx = (b - a) / steps; double sum = 0.0; for (double x = a; x < b; x += dx) sum += fabs(f(x)); return dx * sum; } int main(void) { printf("fabs(+3) = %f\n", fabs(+3.0)); printf("fabs(-3) = %f\n", fabs(-3.0)); // special values printf("fabs(-0) = %f\n", fabs(-0.0)); printf("fabs(-Inf) = %f\n", fabs(-INFINITY)); printf("Area under sin(x) in [-PI, PI] = %f\n", integrate(sin, -PI, PI, 5101)); }
🌐
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 › c++ › fabs-in-c
fabs() in C++ - GeeksforGeeks
January 31, 2023 - The fabs() function returns the absolute value of the argument. Mathematically |a|. If a is value given in the argument.
🌐
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
🌐
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 ... double fabs ( IntegralType arg ); (7) (since C++11) 1-6) Computes the absolute value of a floating point value arg. 7) A set of overloads or a function template accepting an argument of any integral_type.
🌐
GitHub
github.com › cole-trapnell-lab › cufflinks › issues › 116
Please use std::fabs instead of just fabs in C++ to avoid conflicts · Issue #116 · cole-trapnell-lab/cufflinks
March 23, 2019 - In file included from codons.cpp:1: In file included from ./codons.h:3: In file included from ./GBase.h:13: /usr/include/c++/v1/math.h:761:41: error: no member named 'fabsf' in the global namespace; did you mean 'fabs'? abs(float __lcpp_x) _NOEXCEPT {return ::fabsf(__lcpp_x);} ~~^ /usr/include/math.h:255:8: note: 'fabs' declared here double fabs(double) __pure2; ^ In file included from codons.cpp:1: In file included from ./codons.h:3: In file included from ./GBase.h:13: /usr/include/c++/v1/math.h:769:47: error: no member named 'fabsl' in the global namespace; did you mean 'fabs'? abs(long double __lcpp_x) _NOEXCEPT {return ::fabsl(__lcpp_x);} ~~^ /usr/include/math.h:255:8: note: 'fabs' declared here double fabs(double) __pure2; ^ Same with all other mathematical functions. They are safer when invoked from the std:: namespace.
Published   May 25, 2019
Author   yurivict
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cmath › abs
C++ cmath abs() - Compute Absolute Value | Vultr Docs
September 27, 2024 - #include <cmath> int main() { double negative_double = -23.55; double result = std::fabs(negative_double); std::cout << "The absolute value of " << negative_double << " is " << result << std::endl; } Explain Code
🌐
CERN
root-forum.cern.ch › t › fabs-and-abs › 14556
Fabs and abs - ROOT - ROOT Forum
July 10, 2012 - Try simply this: [code]#if 1 /* 0 or 1 / #include <stdlib.h> // for “abs” #include <math.h> // for “fabs” #else / 0 or 1 / #include // for “std::abs” and “std::fabs” #endif / 0 or 1 */ · You should have got it by now that there’s no “default c++ behaviour” what concerns ...
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › libstdc++-html-USERS-4.3 › a01697.html
libstdc++: std Namespace Reference
March 26, 2008 - Classes struct __abs struct __acos struct __add_c_ref struct __add_c_ref< _Tp & > struct __add_lvalue_reference_helper struct __add_lvalue_reference_helper< _Tp, false, true > struct __add_lvalue_reference_helper< _Tp, true, false > struct __add_ref struct __add_ref< _Tp & > struct ...
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cmath › fabs
C++ cmath fabs() - Compute Absolute Value | Vultr Docs
November 12, 2024 - The fabs() function in C++ serves as a straightforward yet powerful tool for obtaining the absolute value of floating-point numbers, thereby ensuring non-negative results useful in various mathematical computations.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
3 weeks ago - math.fabs(x)¶ · Return the absolute value of x. math.floor(x)¶ · Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value. math.fma(x, y, z)¶ ...