C++ Standard Library C++ STL Library

C++ <cmath> - abs() Function



The C++ <cmath> abs() function returns the absolute value (positive value) of the specified number. For example - absolute value of x will be |x|.

Syntax

double abs (double x);
float abs (float x);
long double abs (long double x);
double abs (double x);
float abs (float x);
long double abs (long double x);
//additional overloads for integral types
double abs (T x);                                      

Parameters

x Specify a number whose absolute value need to be determined.

Return Value

Returns the absolute value (positive value) of the argument.

Example:

In the example below, abs() function returns the absolute value (positive value) of the specified number.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<abs(10)<<"\n";
  cout<<abs(-10)<<"\n";
  cout<<abs(-5.45)<<"\n";
  cout<<abs(-5.55)<<"\n";  
  return 0;
}

The output of the above code will be:

10
10
5.45
5.55

❮ C++ <cmath> Library

5