Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::fabs

 

std::fabs is an STL function to take the absolute value of a double.

 

std::fabs is defined in the header file cmath.h.

 

#include <cassert>
#include <cmath>

int main()
{
  const double x = 3.14;
  const double y = -x;
  assert(x == std::fabs(y));
}

 

 

 

 

 

Difference between std::abs and std::fabs

 

std::abs can be used to calculate the absolute value of both an integer and a double, where std::fabs can only be used to obtain the absolute value of a double.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict