In C++, std::abs() gives the absolute value of a number. The absolute value is how far a number is from zero, so it is never negative. abs(-5) is 5 and abs(5) is also 5.
The name works for whole numbers and for decimals. The integer versions are in <cstdlib> and the float, double, and long double versions are in <cmath>. Include <cmath> when you pass a decimal number.
std::abs() is declared in <cmath> and is part of the group Absolute value and sign. These functions work with the sign of a number. abs() and fabs() remove a minus sign, and copysign() and signbit() copy or read it.
Syntax
Include <cmath> and call std::abs. Start with these lines.
C++
int a = std::abs(-5); // 5 (int version)
double b = std::abs(-2.5); // 2.5 (double version, <cmath>)
float c = std::abs(-1.5f); // 1.5 (float version)
Call
What it does
std::abs(-5)
The int version. Returns 5.
std::abs(-2.5)
The double version from <cmath>. Returns 2.5.
std::abs(-1.5f)
The float version. Returns a float.
Always write std::abs. The old C function ::abs only takes an int and would cut -2.5 down to a whole number.
Library declarations
C++
// <cstdlib>
int abs(int n);
long abs(long n);
long long abs(long long n);
// <cmath>
float abs(float x);
double abs(double x);
long double abs(long double x);
Parameters
Parameter
Description
n or x
Required. The number to convert. It can be int, long, long long, float, double, or long double.
Return Value
Return Value
Description
Absolute value
The number without its sign, in the same type as the argument.
NaN
If a floating-point argument is NaN, the result is NaN.
Undefined behavior
std::abs(INT_MIN) cannot fit in an int, so the result is undefined.
C++ Examples
Example 1 — Absolute Value of Ints and Doubles
Call std::abs() on a negative int, a positive int, and two decimals.
std::abs(-12) picks the int version, drops the minus sign, and returns 12.
A positive number such as 7 is returned unchanged. The sign is only removed when it exists.
-3.75 is a double, so the <cmath> version runs and prints 3.75. The decimal part is kept, and 0.0 prints as 0.
Example 2 — Distance Between Two Numbers
The distance between two numbers is the absolute value of their difference. The order of the two numbers does not matter.
C++
#include <cmath>
#include <iostream>
int main() {
int a = -4;
int b = 9;
double morning = 21.5;
double evening = 29.0;
std::cout << "Distance on the number line: " << std::abs(a - b) << '\n';
std::cout << "Same distance, other order: " << std::abs(b - a) << '\n';
std::cout << "Temperature change: " << std::abs(morning - evening) << " degrees\n";
return 0;
}
Output
Distance on the number line: 13
Same distance, other order: 13
Temperature change: 7.5 degrees
How It Works
a - b is -13. std::abs() turns it into 13, which is the real distance between -4 and 9.
b - a is 13 already, and abs() leaves it alone. Both orders give the same answer.
The temperature values are double, so the decimal version runs: 21.5 - 29.0 is -7.5, and the program prints 7.5 as the change.
Example 3 — Compare Decimals with a Tolerance
Two decimals that should be equal are often a tiny bit apart. Check that the absolute difference is smaller than a small limit.
C++
#include <cmath>
#include <iostream>
int main() {
double a = 0.1 + 0.2;
double b = 0.3;
double limit = 1e-9;
std::cout << "a == b: " << (a == b) << '\n';
std::cout << "difference: " << std::abs(a - b) << '\n';
std::cout << "close enough: " << (std::abs(a - b) < limit) << '\n';
return 0;
}
Output
a == b: 0
difference: 5.55112e-17
close enough: 1
How It Works
0.1 + 0.2 is stored as 0.30000000000000004, so the plain == test gives 0, which means false.
std::abs(a - b) measures the gap and prints 5.55112e-17. The gap is real, but very small.
The gap is smaller than 1e-9, so close enough is 1. This is the usual way to compare two double values.
Frequently Asked Questions
std::abs() returns the absolute value of a number. That is the number without its minus sign, so -5 becomes 5 and 7 stays 7.
The absolute value, with the same type as the argument. An int gives an int, and a double gives a double.
Both. The integer versions (int, long, long long) are in <cstdlib>. The float, double, and long double versions are in <cmath>. Include <cmath> when you work with decimals.
fabs() is the older C name and only works with floating-point numbers. In C++, std::abs() has overloads for both integers and decimals, so one name does both jobs. The result for a double is the same.
Include <cmath> and write std::abs(). That always works and picks the C++ overloads. The global name ::abs often works too, but the standard does not promise it, and it may only take a double.