C++ Absolute Value

beginner c++11 math

#include <cmath>

The C++ Standard Library provides the abs function which returns the absolute value of its only argument. For example:

#include <cmath>
#include <iostream>

int main() {
  auto result = std::abs(-3.141592f);

  std::cout << result << "\n";

  auto result2 = std::abs(42);

  std::cout << result2 << "\n";
}
3.14159
42


For more C++ By Example, click here.