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);
}
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.
std::abs(float), std::fabs, std::fabsf, std::fabsl. 1-8) Computes the absolute value of a floating point value arg .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With