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
In C++, wzxhzdk:6 provides int ... for integral ... abs() and fabs() both return the absolute value of a number, but they differ in types, headers, return types, overloads, and portability....
Discussions

c++ - What's the difference between abs and fabs? - Stack Overflow
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++. More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the difference between abs() and fabs() functions?
What is the difference between abs() and fabs() functions in C language? More on youth4work.com
๐ŸŒ youth4work.com
5
0
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
Simple C Question on abs() and fabs()
It is just truncating for you and returning an int. What behavior were you expecting? More on reddit.com
๐ŸŒ r/AskProgramming
8
5
October 18, 2020
๐ŸŒ
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).
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.

๐ŸŒ
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?
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.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
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; }
๐ŸŒ
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.
๐ŸŒ
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
July 28, 2025 - ... 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 ...
๐ŸŒ
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).
๐ŸŒ
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
June 22, 2014 - 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 ...
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ simple c question on abs() and fabs()
r/AskProgramming on Reddit: Simple C Question on abs() and fabs()
October 18, 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) ...
๐ŸŒ
Apple Developer
developer.apple.com โ€บ forums โ€บ thread โ€บ 90710
What does the function "fabs(_:)" โ€ฆ | Apple Developer Forums
September 27, 2024 - In the C standard library, mathematical functions (such as sin, cos or tan) take a "double" parameter, and there are variants with a suffix that take other types (e.g. sinf for a "float" parameter, or sinl for a "long double" parameter). 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", with the initial "f" meaning "floating point".