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.
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
March 14, 2025 - 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),
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.
Videos
03:40
Absolute Value Functions abs() And fabs() | C Programming Tutorial ...
Absolute Value Function abs() | C Programming Tutorial - YouTube
03:33
C Programming Tutorial - Absolute Value - YouTube
08:16
Your Twelfth Day in C (Floating Point Comparison) - Crash Course ...
23:49
Floating Point Numbers in C - YouTube
06:20
C Programming Tutorial - 37 - Absolute Value with abs - YouTube
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
IBM
ibm.com › docs › en › zvm › 7.4.0
abs(), absf(), absl() — Calculate Integer Absolute Value
We cannot provide a description for this page right now
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.
University of Chicago
naipc.uchicago.edu › 2015 › ref › cppreference › en › cpp › numeric › math › fabs.html
std::abs(float), std::fabs - cppreference.com
Computes the absolute value of a floating point value arg.
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 <script src="https://gist.github.com/dockimbel/e479ad63adcacb690dd80c8ebec10de9.js"></script> 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.
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 }