The "conditional abs" you propose is not equivalent to std::abs (or fabs) for floating point numbers, see e.g.

#include <iostream>
#include <cmath>

int main () {
    double d = -0.0;
    double a = d < 0 ? -d : d;
    std::cout << d << ' ' << a << ' ' << std::abs(d);
}

output:

-0 -0 0

Given -0.0 and 0.0 represent the same real number '0', this difference may or may not matter, depending on how the result is used. However, the abs function as specified by IEEE754 mandates the signbit of the result to be 0, which would forbid the result -0.0. I personally think anything used to calculate some "absolute value" should match this behavior.

For integers, both variants will be equivalent both in runtime and behavior. (Live example)

But as std::abs (or the fitting C equivalents) are known to be correct and easier to read, you should just always prefer those.

Answer from Baum mit Augen on Stack Overflow
๐ŸŒ
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).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number.
Discussions

Difference between abs() and fabs(), and can i replace with each other in the program?? And abs() fn is kinda not working.....
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 . More on reddit.com
๐ŸŒ r/cpp_questions
9
1
November 26, 2019
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 26, 2020
๐ŸŒ
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) value of a number.
๐ŸŒ
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 ); long double y = 4.0L * atanl( 1.0L ); if ( x == y ) printf( "x and y are exactly equal.\n" ); else if (fabs( x โˆ’ y ) < 0.0001 * fabsl( y ) ) printf( "x and y are approximately equal:\n" "x is %.8f; y is %.8Lf.\n", x, y ); This code produces the following output: x and y are approximately equal: x is 3.14159274; y is 3.14159265. The absolute value functions for integer types: abs(), labs(), llabs(), and imaxabs(); the absolute value functions for complex numbers: cabs(), cabsf(), cabsl(); the C99 functions fdim() and copysign(); the functions fmax() and fmin() Read now ยท
Authors ย  Peter PrinzTony Crawford
Published ย  2005
Pages ย  618
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ fabs.html
fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128 - cppreference.com
December 20, 2024 - #include <math.h> #include <stdio.h> #define PI 3.14159 // This numerical integration assumes all area is positive. double integrate(double f(double), double a, double b, // assume a < b unsigned steps) // assume steps > 0 { const double dx = (b - a) / steps; double sum = 0.0; for (double x = a; x < b; x += dx) sum += fabs(f(x)); return dx * sum; } int main(void) { printf("fabs(+3) = %f\n", fabs(+3.0)); printf("fabs(-3) = %f\n", fabs(-3.0)); // special values printf("fabs(-0) = %f\n", fabs(-0.0)); printf("fabs(-Inf) = %f\n", fabs(-INFINITY)); printf("Area under sin(x) in [-PI, PI] = %f\n", integrate(sin, -PI, PI, 5101)); }
๐ŸŒ
Vultr
docs.vultr.com โ€บ clang โ€บ standard-library โ€บ math-h โ€บ fabs
C math.h fabs() - Get Absolute Value | Vultr Docs
December 12, 2024 - The fabs() function from the C standard library's math.h header file is used to compute the absolute value of a floating-point number. This function is essential in various programming scenarios, particularly in fields requiring precise mathematical ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-fabs-function-in-cpp
What is the fabs() function in C++?
The fabs() function in C++ is used to return the absolute value of an argument passed to the function.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ fabs() function in c
fabs() Function in C - Scaler Topics
November 7, 2023 - So when using inputs of char data type, make sure you always, ALWAYS use single quotes and never double quotes. ... As we can see, fabs in C converted the negative floating value -5.0 to a positive floating value 5.000000.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ numeric โ€บ math โ€บ fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
March 14, 2025 - This function is not subject to any of the error conditions specified in math_errhandling. If the implementation supports IEEE floating-point arithmetic (IEC 60559), If the argument is ยฑ0, +0 is returned. If the argument is ยฑโˆž, +โˆž is returned. If the argument is NaN, NaN is returned. The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::fabs(num) has the same effect as std::fabs(static_cast<double>(num)).
๐ŸŒ
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).
๐ŸŒ
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...
๐ŸŒ
MKSSoftware
mkssoftware.com โ€บ docs โ€บ man3 โ€บ fabs.3.asp
fabs(), fabsf() -- floating-point absolute value function
#include <math.h> double fabs(double x) float fabsf(float x) The ยท fabs() function computes the absolute value of a floating-point number x. The ยท fabsf() function is a single-precision version of ยท fabs(). x ยท Is a floating point value. The ยท fabs() function returns the absolute value of x.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ fabs-function-in-c-language-absolute-value
Fabs() Function in C Language (Absolute Value) โ€“ Linux Hint
To get the absolute value of โ€œxโ€, we first create a variable of float type and assign it the value -3.1416. We store the result in โ€œaโ€, which should be of the same type as โ€œxโ€. Then, we call the fabs() function and pass โ€œxโ€ as input argument and โ€œaโ€ as output argument.
๐ŸŒ
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 ...