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 ...
🌐
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...
🌐
Programiz
programiz.com › c-programming › library-function › math.h › fabs
C fabs() - C Standard Library
Become a certified C programmer. Try Programiz PRO! ... The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double).
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fabs.htm
C library - fabs() function
#include <stdio.h> #include <math.h> ... types of absolute value − positive and negative, and when these value are passes to the function fabs(), it display the result....
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
/* Example using fabs by TechOnTheNet.com */ #include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ double value; double result; /* Assign the value we will find the fabs of */ value = -2.1; /* Calculate the absolute value of value */ result = fabs(value); /* Display the result of the calculation */ printf("The Absolute Value of %f is %f\n", value, result); return 0; } When compiled and run, this application will output:
🌐
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?
April 18, 2018 - Both functions are to retrieve absolute value. abs() is for integer values and fabs() is for floating type numbers. Prototype for abs() is under the library file < stdlib.h > and fabs() is under < math.h >. ... Didn't get the answer.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - // C program to show the // use of fabs() function #include <math.h> #include <stdio.h> int main() { double a = 980; double b = -1231; double res; res = fabs(a); printf("The absolute value of %.3lf is %.3lf\n", a, res); res = fabs(b); printf("The absolute value of %.3lf is %.3lf\n", b, res); return 0; } ... Example 2: Below is the C program to show what happens when the fabs() function is used for int & long double numbers. ... // C Program to show use of fabs() function // with int and long double numbers #include <math.h> #include <stdio.h> // Driver code int main() { long double a = -7.546; long double b = 4.980; double res; res = fabs(a); printf("The absolute value of %.3lf is %.3lf\n", a, res); res = fabs(b); printf("The absolute value of %.3lf is %.3lf\n", b, res); return 0; }
Find elsewhere
🌐
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.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - float x = 4.0F * atanf( 1.0F ); ... equal:\n" "x is %.8f; y is %.8Lf.\n", x, y ); ... The absolute value functions for integer types: abs(), labs(), llabs(), and imaxabs(); the absolute value functions for complex numbers: cabs(), ...
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
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
🌐
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 the floating-point argument. double fabs( double x ); float fabs( float x ); // C++ only long double fabs( long double x ); // C++ only float fabsf( float x ); long double fabsl( long double x ); #define ...
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › fabs
C math.h fabs() - Get Absolute Value | Vultr Docs
December 12, 2024 - This code snippet demonstrates obtaining the absolute value of the value variable. The function fabs() correctly converts -23.45 to 23.45. Initialize variables with positive and zero values.
🌐
Cplusplus
cplusplus.com › forum › beginner › 136274
fabs versus abs? - C++ Forum
Well, if programming in C++, someone ... things. Do you agree? ... 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 ...
🌐
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 ... Returns the absolute value of x: |x|. Header <tgmath.h> provides a type-generic macro version of this function. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double (defined for T being any integral type).
🌐
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?

🌐
W3Schools
w3schools.com › c › ref_math_fabs.php
C Math fabs() Function
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The fabs() function returns the absolute (positive) ...
🌐
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 ...
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.