Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 364
akim Avatar asked Apr 19 '13 09:04

akim


People also ask

Does abs return an int?

abs(int a) returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

What does std :: abs do?

std::abs(float), std::fabs, std::fabsf, std::fabsl. 1-8) Computes the absolute value of a floating point value arg .

How do you use absolute value in C++?

The abs() in C++ returns the absolute value of an integer number. If the number is negative, it will return the positive value (with the same magnitude), for example, abs(-1) = 1. If the number is already positive or zero, it will return the number as it is, for example, abs(1) = 1.


1 Answers

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.

like image 138
Agentlien Avatar answered Sep 28 '22 06:09

Agentlien