C++ fabs()

Last Updated On Thursday 21st Apr 2022


The C++ fabs() function returns the absolute value of the argument and it takes a single argument.It is defined in the <cmath> header file.
Mathematically, fabs(num) = |num|.


	fabs(double num);


The fabs() function takes a one argument. The argument can be of double, long double or float type.

Return value

fabs() – Returns the absolute value of num i.e. |num|

fabs() prototype

	double fabs(double num);

float fabs(float num);

long double fabs(long double num);

// for integral type
double fabs(T num);

std fabs

Note: The fabs() function is identical to the cmath abs() function.

fabs function

Example : C++ fabs()

	#include <iostream>
#include <cmath>
using namespace std;

int main() {
  double num = -11.44, result;
  result = fabs(num);
  cout << "fabs(" << num << ") = |" << num << "| = " << result;
  return 0;


	fabs(-11.44) = |-11.44| = 11.44