Table of contents
1.
Introduction
2.
What is the abs() in C++?
3.
Syntax of abs() in C++
4.
Parameters of abs() in C++
5.
Return Values of abs() in C++
6.
Example for abs() in C++
6.1.
C++
7.
C++ abs() Prototypes
8.
Exceptions of abs() in C++ 
8.1.
C++
9.
What is cstdlib abs() in C++?
9.1.
Example:
9.2.
C++
10.
What is cstdlib fabs() in C++?
10.1.
Example:
10.2.
C++
11.
Deep Dive into abs()
12.
Watch Out for These Points
13.
Difference Between abs() and fabs() in C++
14.
abs() Overloading in C++
15.
Frequently Asked Questions
15.1.
What does the abs() function do in C++?
15.2.
What is abs and fabs in C++?
15.3.
Can you use abs for double C++?
15.4.
What is the use of abs ()?
15.5.
What does std::abs do?
15.6.
What is abs and fabs in C++?
16.
Conclusion
Last Updated: Jan 2, 2025
Easy

abs() in C++

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++ programming, the abs() function is vital in manipulating numerical values. Short for "absolute," this function calculates the absolute value of a given number, disregarding its sign. The abs() function is part of the <cmath> library and works with integer, floating-point, and long integer data types. It proves invaluable in scenarios where the magnitude of a number is needed, irrespective of its sign, simplifying mathematical operations and decision-making in C++ programs.

abs() in C++

In this blog, we will look at what abs() in C++ does, how to use it, and how it can help make your code more robust and versatile.

What is the abs() in C++?

In C++, the abs() function is a mathematical utility that calculates the absolute value of a given number. The absolute value is the magnitude of a number irrespective of its sign, providing the distance of the number from zero on the number line. 

The abs() function is particularly useful when the direction or sign of a value is irrelevant, and only its distance from zero matters. It's commonly employed in scenarios where the magnitude of a quantity is essential, such as distance calculations or comparisons.

Syntax of abs() in C++

Here's what the basic syntax of the abs() function looks like:

int abs(int x);

 

Here, x is the integer of which we want to find the absolute value. The function returns the absolute value of x.

Parameters of abs() in C++

  • For integer values, the abs() function takes a single argument of type int.
  • For floating-point values, the abs() function takes a single argument of type double or float.

Return Values of abs() in C++

  • For integer arguments, the abs() function returns an integer of the same type as the argument.
  • For floating-point arguments, the abs() function returns a floating-point number of the same type as the argument.

Example for abs() in C++

To illustrate how abs() works, let's go through a simple example:

  • C++

C++

#include 
#include 


int main() {
    int x = -10;
    std::cout << "The absolute value of " << x << " is: " << abs(x);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

In this code snippet, abs() calculates the absolute value of -10, and the output will be 10.

Output

The absolute value of -10 is: 10

 

Explanation

This C++ code calculates and prints the absolute value of an integer using the abs() function from the <cmath> (or <cstdlib>) library. In the example, the variable x is initialized with the value -10. The std::cout statement then displays a message indicating that it will show the absolute value of x. The abs(x) function is applied to obtain the absolute value, and the result is printed to the console. 

C++ abs() Prototypes

abs() has three prototypes:

  1. int abs(int n); returns the absolute value of an integer.
  2. long abs(long n); returns the absolute value of a long integer.
  3. long long int abs(long long int n); returns the absolute value of a long long integer.

Exceptions of abs() in C++ 

No exceptions are thrown by abs() in C++. 

For Example:

  • C++

C++

#include <iostream>
#include <cstdlib>

int main() {
int x = -5;
std::cout << "Absolute value: " << abs(x) << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Absolute value: 5

What is cstdlib abs() in C++?

cstdlib is the C Standard Library that provides the abs() function. This abs() function returns the absolute value of an integer number. 

Example:

  • C++

C++

#include <iostream>
#include <cstdlib>

int main() {
int x = -8;
std::cout << "Absolute value: " << std::abs(x) << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Absolute value: 8

What is cstdlib fabs() in C++?

fabs() in cstdlib is used for double-precision floating-point numbers. This fabs() function returns the absolute value of a integer, float, or double-type value. 

Example:

  • C++

C++

#include <iostream>
#include <cstdlib>

int main() {
double y = -12.34;
std::cout << "Absolute value: " << std::fabs(y) << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Absolute value: 12.34

Deep Dive into abs()

The abs() function simply turns negative numbers into positive numbers. It's vital in many areas of programming where only positive values are expected, such as distances, sizes, or quantities.

It's important to note that abs() deals with integer values. If you attempt to find the absolute value of a floating-point number, you should use the fabs() function from the cmath library instead.

Watch Out for These Points

  • While the abs() function is straightforward to use, a common mistake is to use it with floating-point numbers, which would lead to truncation of decimal places. Always remember to use fabs() when dealing with floating-point numbers.
     
  • When abs() is used with a floating-point number, it might call the integer version (depending on how it’s defined or included), leading to truncation of decimal places. Example: abs(-3.14) might return 3 instead of 3.14 if the integer version of abs() is called. 
     
  • Use std::abs() or fabs() for floating-point numbers to ensure the correct function is invoked and decimals are preserved.

Difference Between abs() and fabs() in C++

Featureabs()fabs()
DefinitionDefined in <cstdlib> for integersDefined in <cmath> for floating-point
Parameter TypeIntegers (int, long, long long)Floating-point numbers (float, double)
Return TypeAlways returns an integer typeAlways returns a floating-point type
OverloadingNo overloads, only integer typesOverloads for float, double, long double
LibraryC libraryC++ Standard Library (cmath header)
UsageFor integral types, including signedFor floating-point types

abs() Overloading in C++

C++ allows overloading the abs() function for different parameter types. Overloading means defining multiple functions with the same name but different parameter types. 

For example, in <cstdlib>, abs() is overloaded for int, long, and long long types. In <cmath>, fabs() overloads for float, double, and long double types. Overloading enables the use of the same function name for different data types, enhancing code flexibility and readability.

Also read -  File Handling in CPP

Frequently Asked Questions

What does the abs() function do in C++?

The abs() function in C++ computes the absolute value of an integer. It returns the non-negative value of its argument, removing any negative sign.

What is abs and fabs in C++?

abs is used for integers and is part of the C library (<cstdlib>). fabs is used for floating-point numbers and is part of the C++ Standard Library (<cmath>).

Can you use abs for double C++?

In C++, the abs() function primarily supports integers. To compute the absolute value of a double, you should use the fabs() function from the <cmath> library, specifically designed to handle floating-point numbers.

What is the use of abs ()?

The abs() function in C returns the absolute value of an integer. It ensures that the returned value is non-negative.

What does std::abs do?

In C++, std::abs is a templated function defined in the <cmath> header. It returns the absolute value of its argument, which can be of any arithmetic type.

What is abs and fabs in C++?

In C++, abs() is used for integers, returning the absolute value, while fabs() is used for floating-point numbers, returning the absolute value. Both functions are available in the <cmath> header.

Conclusion

The abs() function in C++ is a versatile tool for obtaining the absolute value of integer types, while the fabs() function is used for double-precision floating-point numbers. Understanding their application is crucial for writing efficient and accurate numerical programs in C++.  

Recommended articles:

Live masterclass