๐ŸŒ
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
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
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
๐ŸŒ
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.
๐ŸŒ
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๏ผˆ็ตถๅฏพๅ€ค๏ผ‰ใฎ็•ฅใ€‚ ยท...
๐ŸŒ
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.
๐ŸŒ
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'; }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cpp_standard_library โ€บ cpp_abs_function.htm
C++ cmath abs() Function
The C++ cmath abs() function calculates the absolute value of a number, which means it returns the number without its sign, making it non-negative. It works with different data types, such as integers (int), floating-point numbers (float), and
๐ŸŒ
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.