abs() function in C++
In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned as it is. The abs in C++ is described in <cstdlib> header file. The absolute value in C++ works similar to modulus operator in Mathematics: abs(a) = |a|.
Syntax of abs() in C++:
abs ( int a);
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//absolute value of -5
cout<< abs(-5);
return 0;
}
Output:
5
Parameters of abs() in C++
The abs () function in C++ takes only one parameter, but they are of three data types:
- Int
- long int
- longlong int
Return Values of abs() in C++
In C++, the abs() function returns the absolute value of a number. The data type value that is returned by abs in C++ is equal to the data type of the argument passed, that means it could be int, long int, or even long long int.
Example for abs() in C++:
//header files
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int a = -42;
int a1 = 42;
int a2 = 0;
/// Using abs() function on a, a1, a2.
int ab = abs(a);
int ab1 = abs(a1);
int ab2 = abs(a2);
cout<<"abs () value of "<<a<< " is: "<< ab << "\n";
cout<<"abs () value of "<<a1<< " is: "<< ab1 << "\n";
cout<<"abs () value of "<<a2<< " is: "<< ab2 << "\n";
return 0;
}
Output:
abs () value of -42 is: 42
abs () value of 42 is: 42
abs() value of 0 is: 0
Explanation:
- As we have discussed above, in C++, abs () returns the absolute value of the value passed. In the above code, for integer number a = -42, the absolute value of -42= |-42| = 42, so abs(-42) returned 42.
- For integer number a1 = 42, the absolute value of 42 = |42| = 42, so abs(42) returned 42.
- For integer number a2 = 0, the absolute value of 0 = |0| = 0, hence absolute value of 0 is 0, so abs(0) will return 0.
Prototypes in abs()
int abs( int a); or
long int abs(long int a); or
longlong int abs(long long int a);
- If the num passed is of int type, then the return will be of int type.
- If the num passed is of long type, then the return will be of long type.
- If the num passed is of long long type, then the return will be of long long type.
Exceptions of abs() in C++
No-throw guarantee: This function usually throws no exceptions. If the result cannot be represented by the returned type in an implementation with two's complement signed values, it causes undefined behaviour.
For Example: abs is called on INT_MIN = -2147483648. The range of int in C++ is from -2147483648 to 2147483647. So if abs(-2147483648) is called, it will do (-1)*(-2147483648), which will result in it being out of range of int. Thus it will give undefined behaviour.
abs() Overloading
The abs() in C++ can be overloaded to be used for floating, complex types.
- cmath header file is to be included for floating-point types.
- Complex header file is to be included for complex numbers.
What is cstdlib abs() in C++?
The abs() in C++ returns the absolute value of an integer number.
- If the number is negative, it will return the positive value (with the same magnitude), for example, abs(-1) = 1.
- If the number is already positive or zero, it will return the number as it is, for example, abs(1) = 1.
What is cstdlib fabs() in C++?
The fabs() in C++ returns the absolute value of an integer, float, or double-type value. In case of char type, it returns the ASCII code of that character.
- If the number is negative, it will return the positive value (with the same magnitude), for example, abs(-1) = 1.
- If the number is already positive or zero, it will return the number as it is, for example, abs(1) = 1.
Difference Between abs() and fabs() in C++
abs() | fabs() |
The abs() is used for integer data type. | The fabs() is used for int, float, double, and char data types. |
The abs() function is described in <cstdlib> header file. | The fabs() function is described in <cmath> header file. |