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
🌐
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...
🌐
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 ...
🌐
Youth4work
youth4work.com › talent › c language › forum › what is the difference between abs() and fabs() functions?
What is the difference between abs() and fabs() functions?
March 12, 2018 - abs() is for integer types and fabs is for floating types..try to check this by abs(-6.7) and fabs(-6.7). You can easily figure out the difference. ... Both functions are to retrieve absolute value. abs() is for integer values and fabs() is ...
🌐
Youth4work
youth4work.com › talent › c language › forum › answer › answer for what is the difference between abs() and fabs() functions?
Answer for What is the difference between abs() and fabs() functions?
What is the difference between abs() and fabs() functions in C language · Description The C library function int abs(int x) returns the absolute value of int x. Declaration Following is the declaration for abs() function. int abs(int x) Parameters • x − This is the integral value.
🌐
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.
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.

🌐
Scaler
scaler.com › home › topics › fabs() function in c
fabs() Function in C - Scaler Topics
November 7, 2023 - The fabs in C is a library function provided within the math.h header file of the C programming language. It returns the absolute value of the given input, i.e., it will return the positive value of the given input irrespective of its sign (positive/negative).
🌐
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.
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 1581565 › what-is-the-difference-between-abs-and-fabs-functions
What is the difference between abs() and fabs() functions? | Sololearn: Learn to code for FREE!
Prototype: int abs(int n); Computes the absolute value of an integer number `n`. Prototype: double fabs(double n); Computes the absolute value of a floating point number `n`. fabs has defined in <math.h> abs has defined in <stdlib.h> ____ https://en.cppreference.com/w/c/numeric/math/abs https://en.cppreference.com/w/c/numeric/math/fabs
🌐
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
🌐
Reddit
reddit.com › r/askprogramming › simple c question on abs() and fabs()
r/AskProgramming on Reddit: Simple C Question on abs() and fabs()
October 24, 2020 -

Hi guys, I know that abs() takes in an int and returns an int, while fabs() takes in a float/double and returns a float/double.

However, if I pass in an int to fabs, it works as per usual, but not when I pass a double into abs. I would expect some syntax error to appear or smth, or does type promotion occur here, where the int gets promoted to a double and so fabs() work as per usual?

🌐
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 ...
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - Obtains the absolute value of a number · 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
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
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.
Top answer
1 of 2
5

math.h first appears in 7th Research Unix. It is hard to tell how it got there. For example, [1] claims that bits of C library were merged from "PWB/Unix" which included troff and C compiler pcc, but I cannot prove it.

Another interesting piece of information is library manual from V7 Unix: intro.3:

(3)   These functions, together with those of section 2 and those marked (3S),
      constitute library libc, which is automatically loaded by the C compiler
      cc(1) and the Fortran compiler f77(1).  The link editor  ld(1)  searches
      this  library  under  the  `-lc' option.  Declarations for some of these
      functions may be obtained from include files indicated on the  appropri-
      ate pages.

<...>

(3M)  These  functions  constitute the math library, libm.  They are automati-
      cally loaded as needed by the Fortran compiler f77(1).  The link  editor
      searches  this  library  under the `-lm' option.  Declarations for these
      functions may be obtained from the include file <math.h>.

If you look into V7 commands makefiles, only few C programs are linked with -lm flag. So my conclusion is speculative:

  1. libm.a (and math.h) was primarily needed for FORTRAN programs mostly, so it was separated into library to reduce binary footprint (note that it was linked statically).
  2. Not many machines had floating point support. For example, you would need to buy an optional FPP for PDP-11 [2], there is also libfpsim simulation library in Unix to mitigate that, so floating point can be hardly used in early C programs.

1. A History of UNIX before Berkeley: UNIX Evolution: 1975-1984

2. PDP-11 architecture

2 of 2
-1

Most operators like + - / * are also math operators yet these are also readily available. When programming you use so much math, that developers have started to differentiate between math that is needed for everyday stuff and math that is more specialized that you only use some of the time. Abs is one of those functions that are just used to often. Like with pointer arithmetic when you just want to know the difference to calculate the size of a memory block. But you are not interested in knowing which is higher in memory and which is lower.

So to sum up: abs is used often because it calculates the difference of two integers. The difference between two pointers for instance is also an integer. And so it is in stdlib.h. fabs how ever is not something you will need much unless you are doing math specific stuff. Thus it is in math.h.

🌐
Apple Developer
developer.apple.com › forums › thread › 90710
What does the function "fabs(_:)" … | Apple Developer Forums
In the case of the "abs" function for absolute value, when the floating point library functions were added to the specification, there was already an "abs" function that took an "int" parameter. That meant the floating point versions needed different names, so: "fabs", "fabsf" and "fabsl", ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › fabs-fabsf-fabsl
fabs, fabsf, fabsl | Microsoft Learn
Calculates the absolute value of ... long double x ); #define fabs(X) // Requires C11 or later ... The fabs functions return the absolute value of the argument x....