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
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
March 14, 2025 - #include <cmath> #include <iostream> int main() { std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\n' << "abs(-3.0) = " << std::abs(-3.0) << '\n'; // special values std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\n' << "abs(-Inf) = " << std::abs(-INFINITY) << '\n' << "abs(-NaN) = " << std::abs(-NAN) << '\n'; }
🌐
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.
🌐
SACO Evaluator
saco-evaluator.org.za › docs › cppreference › en › cpp › numeric › math › fabs.html
std::abs(float), std::fabs - cppreference.com
#include <iostream> #include <cmath> int main() { std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\n' << "abs(-3.0) = " << std::abs(-3.0) << '\n'; // special values std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\n' << "abs(-Inf) = " << std::abs(-INFINITY) << '\n'; }
🌐
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!

🌐
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
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 ...
🌐
GNU
gnu.org › s › libc › manual › html_node › Absolute-Value.html
Absolute Value (The GNU C Library)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts. This function returns the absolute value of the floating-point number number.
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › fabs
C math.h fabs() - Get Absolute Value | Vultr Docs
December 12, 2024 - Define a floating-point variable with a negative value. Use fabs() to compute the absolute value.
🌐
Teensy Forum
forum.pjrc.com › home › forums
Teensy Forum
October 8, 2022 - Please report any problems, bugs or missing features. For bugs, always include the code or info necessary to reproduce the problem!
🌐
GitHub
gist.github.com › dockimbel › e479ad63adcacb690dd80c8ebec10de9
C function for comparing float values that are close enough (<= 10 ULP)
Learn more about clone URLs · Clone this repository at &lt;script src=&quot;https://gist.github.com/dockimbel/e479ad63adcacb690dd80c8ebec10de9.js&quot;&gt;&lt;/script&gt; Save dockimbel/e479ad63adcacb690dd80c8ebec10de9 to your computer and use it in GitHub Desktop. Download ZIP · C function for comparing float values that are close enough (<= 10 ULP) Raw ·
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › math › abs
abs()
April 25, 2025 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
🌐
Cornell Computer Science
cs.cornell.edu › courses › cs3410 › 2025sp › notes › float.html
Floating Point - CS 3410
If you ever end up comparing two floating-point numbers for equality, with f1 == f2, be suspicious. For example, try 0.1 + 0.2 == 0.3 to be disappointed. Consider using an “error tolerance” in comparisons, like abs(f1 - f2) < epsilon.
🌐
Hackage
hackage.haskell.org › package › nri-prelude › docs › src › Basics.html
Untitled
-- -- > min 42 12345678 == 42 -- > min "abc" "xyz" == "abc" min :: Prelude.Ord comparable => comparable -> comparable -> comparable min :: comparable -> comparable -> comparable min = comparable -> comparable -> comparable forall a. Ord a => a -> a -> a Prelude.min -- | Compare any two comparable values. Comparable values include @String@, -- @Char@, @Int@, @Float@, or a list or tuple containing comparable values.
🌐
SysTutorials
systutorials.com › docs › linux › man › 3-std::abs(float),std::fabs,std::fabsf,std::fabsl
std::abs(float),std::fabs,std::fabsf,std::fabsl: std::abs(float),std::fabs,std::fabsf,std::fabsl - Linux Manuals (3)
If successful, returns the absolute value of arg (|arg|). The value returned is exact and does not depend on any rounding modes. 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),
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › abs.html
abs, labs, llabs, imaxabs - cppreference.com
for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647. ... #include <limits.h> #include <stdio.h> #include <stdlib.h> int main(void) { printf("abs(+3) = %d\n", abs(+3)); printf("abs(-3) = %d\n", abs(-3)); // printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems }