Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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()
11.1.
Watch Out for These Points
12.
Difference Between abs() and fabs() in C++
13.
abs() Overloading
14.
Frequently Asked Questions
14.1.
What does the abs() function do in C++?
14.2.
What is abs and fabs in C++?
14.3.
Where is abs defined in C?
14.4.
What is the use of abs ()?
14.5.
What does std::abs do?
14.6.
What is abs and fabs in C++?
15.
Conclusion
Last Updated: May 8, 2024
Easy

abs() in C++

Author Gunjan Batra
0 upvote
gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

In the realm of C++ programming, the abs() function plays a vital role in manipulating numerical values. Short for "absolute," this function calculates the absolute value of a given number, disregarding its sign.

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

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

Output

output

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;
}

Output:

output

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;
}

Output:

output

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;
}

Output:

output

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.

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

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 calculates the absolute value of an integer in C++.

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>).

Where is abs defined in C?

The abs() function is defined in the stdlib.h header file in C programming language.

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:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. If you're interested, you can explore the courses we offer.

Previous article
toupper() in C++
Next article
Switch Case C++
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass