The abs() function in C++ returns the absolute value of the argument. It is defined in the cmath header file.
Mathematically, abs(num) = |num|.
Example
Syntax of abs()
The syntax of the abs() function is:
abs(double num);
abs() Parameters
The abs() function takes the following parameter:
- num - a floating point number whose absolute value is returned. It can be of the following types:
doublefloatlong double
abs() Return Value
The abs() function returns:
- the absolute value of num i.e.
|num|
abs() Prototypes
The prototypes of abs() as defined in the cmath header file are:
double abs(double num);
float abs(float num);
long double abs(long double num);
// for integral types
double abs(T num);
Note: The cmath abs() function is identical to the fabs() function.
Example 1: C++ abs()
Output
abs(-87.91) = |-87.91| = 87.91
Example 2: C++ abs() for Integral Types
Output
abs(-101) = |-101| = 101