Search code examples
c++unsignedsignedgcc-warningabsolute-value

Why does std::abs return signed types


I'm getting warning for signed vs. unsigned comparison when I'm comparing a std::abs(int) against an unsigned. And indeed, std::abs returns signed values. Why was that choice made? It would have solved the problem of negative values whose absolute value cannot be represented in the signed type.

And then, is there something cleaner (i.e., without cast) than this to avoid warnings?

#include <cassert>
#include <cstdlib>

// max(1, lhs + rhs). (lhs must be > 0)
unsigned add(unsigned lhs, int rhs)
{
  return
    (0 < rhs || static_cast<unsigned>(-rhs) < lhs
     ? rhs + lhs
     : 1);
}

int main()
{
  assert(add(42, -41) == 1);
  assert(add(42, 0) == 43);
  assert(add(42, 1) == 43);
  assert(add(42, -51) == 1);
}

Solution

  • The short answer is that this is done so that the return type of abs is the same as its input type. This is exactly what you want, most of the time.

    Mostly, when calling abs, you're dealing with an equation where all elements are of the same type (or you'd get warnings) and you want to use the magnitude of some variable in that equation. That doesn't mean you want to change the type of one of the variables in your equation. That would give the kind of issues/warnings you're mentioning.

    So, in short, it is more common and more natural to want the same input and output type when asking for the absolute value of a signed variable. The magnitude of value isn't commonly used as an index.