(C++) std::fabs

January 11, 2018 · View on GitHub

 

 

 

 

 

(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.