C++ std::abs — absolute value

In C++, std::abs returns the magnitude of a number. For example, the absolute value of −6 is 6, and the absolute value of 0 is 0.

A simple example

std::abs

  1. Start with −3
  2. Do this Measure distance from zero
  3. Result 3
From −3, three unit steps reach zero. The magnitude is 3.
lesson.cpp
#include <iostream>
#include <cmath>

int main() {
    std::cout << (std::abs(-3)) << '\n';
}
Output
3