C++

Absolute Value Function C++

At times, while doing mathematical calculations, you require the absolute values of the numbers. The absolute value refers to a value without any sign. For example, the absolute value of a negative number will always be a positive number. In other words, you can say that the absolute value only refers to the magnitude of a number and not its sign. For finding the absolute values in C++, we have a dedicated function known as the absolute value function.

Unlike the unsigned integers, the absolute value function in C++ does not change the magnitude of a number; rather, it simply removes its sign. If a number is positive, then it will be returned as it is. The earlier implementation of this function was only meant for the integers. However, now it can also be used to find the absolute values of “long”, “long long”, “float”, and “double”. We will learn to use this function with different data types in C++ in this guide.

Using the Absolute Value Function in C++ in Ubuntu 20.04

The following five examples have been designed to teach you the usage of the absolute value function in C++ with different data types, i.e. integer, long, long long, float, and double.

Example 1: Finding the Absolute Value of an Integer

For finding the absolute value of an integer using the absolute value function in C++, we have implemented the following code snippet:

In this program, we have included the “cstdlib” header file along with the regular header file since this one contains the absolute value function’s implementation in C++. Then, we have defined an integer “x”. We wanted to take a negative integer as input from the user for which we have displayed a message. Then, we have used the “cin” statement for taking the value of this integer as input from the user at runtime. Finally, we have used the absolute value function in C++ and the “cout” statement to display the absolute value of the provided integer on the terminal.

For making an object file of this C++ program, we executed the command shown below:

$ g++ Absolute.cpp –o Absolute

Then, for running this compiled program, we executed the following command:

$ ./Absolute

Upon executing this code snippet, we were asked to enter a negative integer as shown in the image below:

We provided “-42” as input to this program.

The absolute value returned as the output of this program is as follows:

Example 2: Finding the Absolute Value of a Long

For finding the absolute value of a long using the absolute value function in C++, we have implemented the following code snippet:

In this program, we have included the “cstdlib” header file along with the regular header file since this one contains the absolute value function’s implementation in C++. Then, we have defined a long “x”. We wanted to take a negative long as input from the user for which we have displayed a message. Then, we have used the “cin” statement for taking the value of this long as input from the user at runtime. Finally, we have used the absolute value function in C++ along with the “cout” statement to display the absolute value of the provided long on the terminal.

We provided a negative long to this program:

The absolute value returned as the output of this program is as follows:

Example 3: Finding the Absolute Value of a Long Long

For finding the absolute value of a long long using the absolute value function in C++, we have implemented the following code snippet:

In this program, we have included the “cstdlib” header file along with the regular header file since this one contains the absolute value function’s implementation in C++. Then, we have defined a long long “x”. We wanted to take a negative long long as input from the user for which we have displayed a message. Then, we used the “cin” statement to take the value of this long as input from the user at runtime. Finally, we have used the absolute value function in C++ along with the “cout” statement to display the absolute value of the provided long long on the terminal.

We provided a negative long long to this program:

The absolute value returned as the output of this program is as follows:

Example 4: Finding the Absolute Value of a Float

For finding the absolute value of a float using the absolute value function in C++, we have implemented the following code snippet:

In this program, we have included the “cstdlib” header file along with the regular header file since this one contains the absolute value function’s implementation in C++. Then, we have defined a float “x”. We wanted to take a negative float as input from the user for which we have displayed a message. Then, we have used the “cin” statement for taking the value of this float as input from the user at runtime. Finally, we have used the absolute value function in C++ along with the “cout” statement to display the absolute value of the provided float on the terminal.

We provided a negative float to this program:

The absolute value returned as the output of this program is as follows:

Example 5: Finding the Absolute Value of a Double

For finding the absolute value of a double using the absolute value function in C++, we have implemented the following code snippet:

In this program, we have included the “cstdlib” header file along with the regular header file since this one contains the absolute value function’s implementation in C++. Then, we have defined a double “x”. We wanted to take a negative double as input from the user for which we have displayed a message. Then, we have used the “cin” statement for taking the value of this double as input from the user at runtime. Finally, we have used the absolute value function in C++ along with the “cout” statement to display the absolute value of the provided double on the terminal.

We provided a negative double to this program:

The absolute value returned as the output of this program is as follows:

With the help of the very same program, we would also like to mention that if you provide a value starting with a decimal to the absolute value function, this value is returned with a “0” at the beginning. For example, we provided the input shown in the image below to the very same program:

The absolute value returned as the output of this program is as follows:

Conclusion

This article guides the usage of the absolute value function in C++ in Ubuntu 20.04. We first stated the purpose of this function, followed by all the data types with which it can be used. Along with that, we also drew a comparison between this function and the unsigned integers in C++. After that, we shared with you the five different examples depicting the usage of this function that you can use as a baseline for proceeding with this function in C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.