function
abs
<cmath>
double abs ( double x );
float abs ( float x );
long double abs ( long double x );
Compute absolute value
Returns the absolute value of
x (
/x/ ).
In C++, this function is overloaded in
<cstdlib>,
<complex> and
<valarray> (see
cstdlib abs,
complex abs and
valarray abs).
Parameters
- x
- Floating point value.
Return Value
The absolute value of
x.
Portability
These overloads of
abs are only available in C++.
In C, only the
cstdlib version of exists (see
cstdlib abs) -
fabs can be used instead.
Example
1 2 3 4 5 6 7 8 9 10 11
|
// cmath's abs example
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
cout << "The absolute value of 3.1416 is " << abs (3.1416) << endl;
cout << "The absolute value of -10.6 is " << abs (-10.6) << endl;
return 0;
}
|
Output:
The absolute value of 3.1416 is 3.1416
The absolute value of -10.6 is 10.6
|
See also
- abs
- Absolute value (function)
- fabs
- Compute absolute value (function)
- labs
- Absolute value (function)