abs is for int arguments. Using it with a floating-point argument truncates the value to an integer (or overflows).

Use fabsf, fabs, or fabsl for float, double, or long double, respectively. These are declared in <math.h>.

Alternatively, include <tgmath.h> (type-generic math) and use fabs with any floating-point type.

Answer from Eric Postpischil on Stack Overflow
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ math_h โ€บ fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
In the C Programming Language, the fabs function returns the absolute value of a floating-point number. The syntax for the fabs function in the C Language is:
๐ŸŒ
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). ... To find absolute value of an integer or a float, you can explicitly convert the number to double.
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ why is abs() in cmath also getting rid of my decimals?
r/cpp_questions on Reddit: why is abs() in cmath also getting rid of my decimals?
October 26, 2022 -

Hey guys, I have a very simple code, which is the following:

#include <iostream>
#include <cmath>

int main()
{
    double a = 2.1; 
    double b = abs(a); 

    std::cout << b; 
}

The output I get is b=2. Which doesn't really make sense to me. The output should just be 2.1, no?

Thanks in advance!

๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_fabs.htm
C library - fabs() function
Below the program calculate absolute value of floating-point numbers. Here, we have two types of absolute value โˆ’ positive and negative, and when these value are passes to the function fabs(), it display the result.
Find elsewhere
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cmath โ€บ abs
std::abs
double abs (double x); float abs (float x);long double abs (long double x); double abs (T x); // additional overloads for integral types ... Returns the absolute value of x: |x|. These convenience abs overloads are exclusive of C++. In C, abs is only declared in <stdlib.h> (and operates on ...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ c-in-a โ€บ 0596006977 โ€บ re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - #include <math.h> doublefabs( double x ); float fabsf( float x ); long double fabsl( long double x ); 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.
Authors ย  Peter PrinzTony Crawford
Published ย  2005
Pages ย  618
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ libc โ€บ manual โ€บ html_node โ€บ Absolute-Value.html
Absolute Value (The GNU C Library)
See Integers for a description of the intmax_t type. ... Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts. This function returns the absolute value of the floating-point number number.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 32193-abs-float.html
abs of float
The abs( ) function takes an integer number and returns an integer value. You want to use the function fabs( ) which takes a double and returns a double. ... Alternatively you can use fabs() defined in math.h, which has been created specifically for floating point arguements.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cmath โ€บ fabs
Cplusplus
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 ...
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ numeric โ€บ math โ€บ fabs
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
1-4) Computes the absolute value of the floating-point value num. The library provides overloads of std::abs and std::fabs for all cv-unqualified floating-point types as the type of the parameter num.(since C++23) For integral arguments, the integral overloads of std::abs are likely better matches.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-evaluate-absolute-value-expressions-in-c-435174
How to Evaluate Absolute Value Expressions in C | LabEx
Then, you will use the abs() function for integers and the fabs() function for floating-point numbers to calculate the absolute value, and print the results.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_stdlib_abs.php
C stdlib abs() Function
There are two other variants of the function: labs() for long int arguments and llabs() for long long int arguments. Note: The abs() function is only intended for int type values. For float and double type values use the fabs() function instead.
๐ŸŒ
MKSSoftware
mkssoftware.com โ€บ docs โ€บ man3 โ€บ fabs.3.asp
fabs(), fabsf() -- floating-point absolute value function
Is a floating point value. ... If x is NaN, NaN is returned. If the result underflows, 0 is returned. ... PTC MKS Toolkit for Professional Developers PTC MKS Toolkit for Professional Developers 64-Bit Edition PTC MKS Toolkit for Enterprise Developers PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
abs() function does apparently not work on floats. - Programming - Arduino Forum
June 4, 2020 - I just had to make this experience: The abs() function does apparently not work on floats. with internal_resistance = abs(delta_voltage / delta_current); and delta_voltage (in mV) being -4 and delta _current (in micrโ€ฆ