C++ cstdlib abs - Syntax
mixed abs(mixed n);
The abs() function in C++ is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (short int, integer, long, float or double) (*Required) |
---|---|
Return mixed |
Returns a number respective to an input argument. |
Convert the positive and negative integer to an absolute value.
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive integer value
int x = abs(9);
cout << "The absolute value of 9 is " << x << endl;
// Negative integer value
int y = abs(-6);
cout << "The absolute value of -6 is " << y << endl;
return 0;
}
Convert the positive and negative float to an absolute value.
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive floating point value
float x = abs(9.3);
cout << "The absolute value of 9.3 is " << x << endl;
// Negative floating point value
float y = abs(-6.3);
cout << "The absolute value of -6.3 is " << y << endl;
return 0;
}
Convert the positive and negative Infinity to an absolute value.
#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive ∞ infinite value
cout << "The absolute value of inf is " << abs(std::numeric_limits<float>::infinity()) << endl;
// Negative ∞ infinite value
cout << "The absolute value of -inf is " << abs(-std::numeric_limits<float>::infinity()) << endl;
return 0;
}