# How to Calculate Absolute Value in C++? 4 Best Methods

The task of calculating the absolute value in C++ is very important one. Programmers, specially those making some mathematical programs must know how to do it, so thatâ€™s what we will discuss in this article.

There are many ways to calculate the absolute value in C++. We will talk about them one by one. But first, we need to know about what is absolute value of a number.

Overview

## What is Absolute Value of a number ?

The absolute value of a number is the magnitude of that number irrespective of its sign. For example

Absolute value of -5 = 5

Absolute value of 5 = 5

Mathematically, the absolute value is denoted by the modulus of the number. E.g., |-5| = |5| = 5

## Calculating the Absolute Value using abs() function

The abs() function is used to find the absolute value in C++ for a number. It is defined in the cstdlib header file, which must be included to use the abs() function.

The syntax for abs function is :

absolute_value = abs(given_number)

### Input Parameters

The abs() takes only one input parameter. The input can be of following types

• Int
• Long int
• Long long int

Note: In C language, the input can be only of int type.

### Return Value of abs() function

The abs() function return only one value i.e., the absolute value of the input number.

See also  How to Convert A List of Strings to Integers in Python Like A Pro

The return type is the same as that of the input parameter.

Note: In C language, the output is only of int type.

Example:

```#include<iostream>
#include<cstdlib>

int main(){
int num1 = -5;
int num2 = 5;

cout<<"The Absolute value of num1 is : "<<abs(num1)<<endl;
cout<<"The Absolute value of num2 is : "<<abs(num2)<<endl;

return 0;
}

```

Output:

The Absolute value of num1 is : 5

The Absolute value of num2 is : 5

### Exception of abs() function

The abs() function will throw an error if the absolute value of the input parameter exceeds its data type. While in most cases this isnâ€™t a problem but there is one corner case that must be handled carefully.

If num = INT_MIN = -2147483648 and num is an integer

We know that the range of int in C++ is from -2147483648 to 2147483647. The absolute value in this case, which is 2147483648, exceeds the capacity of the int data type. Thus this will throw an error.

### The fabs() function

The fabs() function, declared in the cmath header file, works similar to the abs() function but has a broader set of input and output types. It can take input parameter of type:

• Int
• Long int
• Long long int
• Float
• Double
• Char

## Finding the Absolute Value in C++ without abs() function and if statement

The abs() function is surely a good way to find the absolute value, but as programmers we should learn different ways to solve a problem to open our minds. Now we will see some other ways to calculate the absolute value of a number.

### Method 1 : Using Ternary Operator

We can use the ternary operator to check for the sign and change the answer accordingly.

Example:

```#include<iostream>

int abs_val(int num){
// this expression translates to num*1 for num>0 and num*(-1) for num<0
return num * ((num>0) - (num<0));
}

int main(){
int n = -10;
cout<<"The absolute value of -10 is : "<<abs_val(n)<<endl;

return 0;
}
```

Output:

The absolute value of -10 is : 10

## You May Also Like

Top Uses of C++ Programming that You Should Know

### Method 3 : Using sqrt() function

We know that the square root of any number is always positive. So, if we square the given number and then find the square root of it, we will get the absolute value of the given number.

Note: The sqrt() function is defined in the math.h header and so, it must be included for using this method.

Example:

```#include<iostream>
#include<math.h>

int abs_val(int num){
return sqrt(num*num);
}

int main(){
int n = -10;
cout<<"The absolute value of -10 is : "<<abs_val(n)<<endl;

return 0;
}
```

Output:

The absolute value of -10 is : 10

### Method 4 : Using Binary Operations

In binary, the sign of a number is shown using the most significant bit(leftmost bit) known as Sign bit. It is 0 for positive numbers and 1 for negative numbers.

Also, if we find the 1â€™s compliment of a number (1â€™s compliment is calculated by inverting all the bits of a number i.e., 0s change to 1s and vice versa) and increment it by 1, we get the 2â€™s compliment of that number which is the same number but with its sign converted.

One effective way to find 1â€™s compliment is to perform the XOR operation on the number with 1. This works because it gives 1 for all 0s and 0 for all 1s.

The working of this method is as follows:

n = 13 = 01101

now perform XOR operation (^) with 1

01101 ^ 1 = 10010

Now increment by 1

10010+ 1 = 10011 = -13

Letâ€™s now see the C++ implementation of the above procedure

Example:

```#include<iostream>

int abs_val(int num){
// the right shift by 31 gives us the sign bit considering it is a 32 bit number system
return (num + num>>31) ^ (num >> 31);
}

int main(){
int n = -10;
cout<<"The absolute value of -10 is : "<<abs_val(n)<<endl;

return 0;
}
```

Output:

The absolute value of -10 is : 10

## Conclusion

It is all we can discuss about the absolute value in C++. The absolute value is the magnitude of a number without its sign. The abs() function is the most appropriate choice to get absolute value as it is easy to implement, but we can also use various other methods to get the same results.

The absolute value is a very important part of many arithmetic logic and is thus an important functionality to learn. If you think that we have missed anything about absolute value in C++ then comment down below.