🌐
Cppreference
en.cppreference.com › w › c › numeric › math › fabs
fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128 - cppreference.com
December 20, 2024 - The functions with decimal floating-point parameters are declared if and only if the implementation predefines __STDC_IEC_60559_DFP__ (i.e. the implementation supports decimal floating-point numbers) · If successful, returns the absolute value of arg (\(\small |arg| \)|arg|). The value returned ...
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fabs.htm
C library - fabs() function
The C math library fabs() function accepts the parameter(x) of type double that returns the absolute value of x. This function is defined under the header that calculates the absolute value of a float point number.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › fabs-fabsf-fabsl
fabs, fabsf, fabsl | Microsoft Learn
July 28, 2025 - API reference for fabs, fabsf, and fabsl; which calculate the absolute value of a floating-point value.
🌐
Cplusplus
cplusplus.com › reference › cmath › fabs
Cplusplus
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).
🌐
Linux Man Pages
linux.die.net › man › 3 › cabsf
cabsf(3): absolute value of complex number - Linux man page
The cabs() function returns the absolute value of the complex number z. The result is a real number.
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › c › numeric › complex › cabs
cabsf, cabs, cabsl - cppreference.com
March 26, 2015 - If no errors occur, returns the absolute value (norm, magnitude) of z · Errors and special cases are handled as if the function is implemented as hypot(creal(z), cimag(z))
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_abs.htm
C library - abs() function
The C stdlib library abs() function is used to returns the absolute value of the specified number, where absolute represents the positive number. This function only returns the positive integer.
🌐
Scaler
scaler.com › home › topics › abs() function in c
abs() Function in C - Scaler Topics
March 21, 2024 - Whatever integer value we pass into the absolute function abs(), the absolute value of that integer is the return value of abs() in C.
🌐
Runebook.dev
runebook.dev › en › docs › c › numeric › math › abs
Mastering the abs function in C: Common Pitfalls and Safer Alternatives
The abs function calculates the absolute value of an integer. The absolute value of a number is its distance from zero on the number line, so it's always non-negative.
🌐
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 - 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++.
🌐
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
🌐
Oreate AI
oreateai.com › blog › a-detailed-explanation-of-the-cc-abs-function-from-principles-to-practical-applications › 98179184802234d17e6a4a7370fbad68
A Detailed Explanation of the C/C++ Abs Function: From Principles to Practical Applications - Oreate AI Blog
January 7, 2026 - The absolute value function abs ... core functionality of this function is to return the absolute value of any integer, which represents its distance from zero on a number line....
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.

🌐
Quora
quora.com › What-is-the-meaning-of-abs-in-C-programming
What is the meaning of 'abs' in C programming? - Quora
Answer (1 of 5): C Language: abs function (Absolute Value of Integer) In the C Programming Language, the abs function returns the absolute value of an integer. Syntax The syntax for the abs function in the C Language is: [code]int abs(int x); ...