🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › abs
C++ cmath abs() - C++ Standard Library
The abs() function in C++ returns the absolute value of the argument. It is defined in the cmath header file.
Discussions

why is abs() in cmath also getting rid of my decimals?
You're calling the C abs, not the one from the C++ stdlib. More on reddit.com
🌐 r/cpp_questions
16
13
October 26, 2022
math - Function abs() in C++ console project - Stack Overflow
Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... It's C++, so use the C++ header. ... Yes, I just realized this is the thrust of your question. I always use personally. More on stackoverflow.com
🌐 stackoverflow.com
c++ - Using #include <cmath>, why it always select the long version of abs instead of choose due to the input type? - Stack Overflow
I've noticed this strange thing on an app I'm using (trying to recreate here, not sure if I'm able). Basically, if I include #include and I call abs() function (without namespace), whatever values I pass to it (double, float, and so on) it seems to evaluate the int version of it: More on stackoverflow.com
🌐 stackoverflow.com
performance - What is different about C++ math.h abs() compared to my abs() - Stack Overflow
abs takes an integer, and return an integer. So using abs is just wrong in your comparison. Use fabs from instead. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cmath › abs
C++ cmath abs() - Compute Absolute Value | Vultr Docs
September 27, 2024 - Here, std::abs(-0) is evaluated, which dutifully returns 0. C++ does not differentiate between negative zero and zero in integer arithmetic. Consider floating-point data types like float or double in computation. Use the fabs() function for floating-point numbers to ensure precision. ... #include <cmath> int main() { double negative_double = -23.55; double result = std::fabs(negative_double); std::cout << "The absolute value of " << negative_double << " is " << result << std::endl; } Explain Code
🌐
W3Schools
w3schools.com › cpp › ref_math_abs.asp
C++ Math abs() Function
The abs() function is defined in the <cmath> header file.
🌐
Scaler
scaler.com › home › topics › abs() in c++
abs() in C++ | abs() Function in C++ - Scaler Topics
October 10, 2025 - The abs() in C++ can be overloaded to be used for floating, complex types . cmath header file is to be included for floating-point types.
🌐
cpprefjp
cpprefjp.github.io › reference › cmath › abs.html
abs - cpprefjp C++日本語リファレンス
<cmath> namespace std { float abs(float ... long long abs(long long x); // (8) C++23 } 浮動小数点数型の絶対値を求める。abs は absolute value(絶対値)の略。 ·...
🌐
Cplusplus
cplusplus.com › reference › cstdlib › abs
abs
Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs).
🌐
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!

Find elsewhere
🌐
Educative
educative.io › answers › what-is-abs-in-cpp
What is abs() in C++?
The abs() function in the cstlib header file computes the absolute value of integer data type i.e. int. To calculate the absolute value of floating-point types, the abs() function is overloaded in the library cmath.
🌐
YouTube
youtube.com › watch
C++ Weekly - Ep 480 - cmath vs math.h vs abs vs std::abs, fight! - YouTube
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟C++ Best Practices - NDC Toronto - May 5,6 - https://ndctoronto.com/workshops/c-best-practices/fce45061e333A robust c...
Published   May 12, 2025
🌐
Python
docs.python.org › 3 › library › cmath.html
cmath — Mathematical functions for complex numbers
The modulus (absolute value) of a complex number z can be computed using the built-in abs() function. There is no separate cmath module function for this operation.
🌐
Programiz
programiz.com › cpp-programming › library-function › cstdlib › abs
C++ cstdlib abs() - C++ Standard Library
Create a simple calculator · Check ... C++ div() C++ cmath abs() C++ fabs() C++ strtoull() C++ strtol() The abs() function in C++ returns the absolute value of an integer number....
🌐
TutorialKart
tutorialkart.com › cpp › cpp-cmath-abs
C++ abs() - Absolute Value Function
May 5, 2023 - In this C++ tutorial, you will learn how to find the absolute value of given argument using abs() function of cmath, with syntax and examples.
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs
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'; }
🌐
AlphaCodingSkills
alphacodingskills.com › cpp › notes › cpp-cmath-abs.php
C++ <cmath> abs() Function - AlphaCodingSkills
The C++ <cmath> abs() function returns the absolute value (positive value) of the specified number.