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
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)
🌐
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'; }
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.

🌐
Cplusplus
cplusplus.com › reference › cmath › abs
std::abs
double abs (double x); float abs (float x);long double abs (long double x); double abs (T x); // additional overloads for integral types · Compute absolute value · Returns the absolute value of x: |x|. These convenience abs overloads are exclusive of C++. In C, abs is only declared in <stdlib.h> ...
🌐
Reddit
reddit.com › r/cpp_questions › why is abs() in cmath also getting rid of my decimals?
r/cpp_questions on Reddit: why is abs() in cmath also getting rid of my decimals?
October 26, 2022 -

Hey guys, I have a very simple code, which is the following:

#include <iostream>
#include <cmath>

int main()
{
    double a = 2.1; 
    double b = abs(a); 

    std::cout << b; 
}

The output I get is b=2. Which doesn't really make sense to me. The output should just be 2.1, no?

Thanks in advance!

🌐
W3cubDocs
docs.w3cub.com › cpp › numeric › math › fabs
Std::abs(float) - C++ - W3cubDocs
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.
🌐
Educative
educative.io › answers › what-is-abs-in-cpp
What is abs() in C++?
float abs (float x); Or · long double abs (long double x); Or · double abs (T x); Consider the code snippet below, which demonstrates the use of the abs() function: #include <cstdlib> #include <iostream> using namespace std; int main() { int ...
🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › abs
C++ cmath abs() - C++ Standard Library
#include <iostream> #include <cmath> using namespace std; int main() { // get absolute value of -5.5 cout << abs(-5.5); return 0; } // Output: 5.5 ... double abs(double num); float abs(float num); long double abs(long double num); // for integral types double abs(T num);
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › abs
std::abs, std::labs, std::llabs, std::imaxabs - cppreference.com
Computes the absolute value of the integer number num. The behavior is undefined if the result cannot be represented by the return type. If std::abs is called with an unsigned integral argument that cannot be converted to int by integral promotion, the program is ill-formed.
🌐
W3Schools
w3schools.com › cpp › ref_math_abs.asp
C++ Math abs() Function
double abs(double number); float abs(float number); ❮ Math Functions · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services ...
🌐
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)
If std::abs is called with an argument of type X such that std::is_unsigned<X>::value is true and X cannot be converted to int by integral_promotion, the program is ill-formed. (since C++17) ... If successful, returns the absolute value of arg (|arg|). The value returned is exact and does not depend on any rounding modes. This function is not subject to any of the error conditions specified in math_errhandling. If the implementation supports IEEE floating...
🌐
Marzer
marzer.github.io › muu › group__abs.html
Math » abs() module | muu Miscellaneous useful utilities for C++
Constexpr-friendly alternatives to std::abs. ... Returns the absolute value of a half-precision float.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -9876543210 is 9876543210 The absolute value of 0xffffffffffffffff is 0x0000000000000001 Microsoft implementation-specific results: abs(INT_MIN) returns -2147483648 labs(LONG_MIN) returns -2147483648 llabs(LLONG_MIN) returns -9223372036854775808 _abs64(_I64_MIN) returns 0x8000000000000000 · Data conversion Math and floating-point support _cabs fabs, fabsf, fabsl imaxabs
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cmath › abs
C++ cmath abs() - Compute Absolute Value | Vultr Docs
September 27, 2024 - The abs() function from the C++ <cmath> library is very effective in dealing with absolute values, either for integers with std::abs() or for floating-point numbers with std::fabs(). Utilizing this function properly allows you to handle mathematical ...