I don't know whether you'd regard it as making sense, but abs()
applied to an unsigned value can certainly return a value other than the one passed in. That's because abs()
takes an int
argument and returns an int
value.
For example:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
unsigned u1 = 0x98765432;
printf("u1 = 0x%.8X; abs(u1) = 0x%.8X\n", u1, abs(u1));
unsigned long u2 = 0x9876543201234567UL;
printf("u2 = 0x%.16lX; abs(u2) = 0x%.16lX\n", u2, labs(u2));
return 0;
}
When compiled as C or C++ (using GCC 4.9.1 on Mac OS X 10.10.1 Yosemite), it produces:
u1 = 0x98765432; abs(u1) = 0x6789ABCE
u2 = 0x9876543201234567; abs(u2) = 0x6789ABCDFEDCBA99
If the high bit of the unsigned value is set, then the result of abs()
is not the value that was passed to the function.
The subtraction is merely a distraction; if the result has the most significant bit set, the value returned from abs()
will be different from the value passed to it.
When you compile this code with C++ headers, instead of the C headers shown in the question, then it fails to compile with ambiguous call errors:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void)
{
unsigned u1 = 0x98765432;
cout << "u1 = 0x" << hex << u1 << "; abs(u1) = 0x" << hex << abs(u1) << "\n";
unsigned long u2 = 0x9876543201234567UL;
cout << "u2 = 0x" << hex << u2 << "; abs(u2) = 0x" << hex << abs(u2) << "\n";
return 0;
}
Compilation errors:
absuns2.cpp: In function ‘int main()’:
absuns2.cpp:8:72: error: call of overloaded ‘abs(unsigned int&)’ is ambiguous
cout << "u1 = 0x" << hex << u1 << "; abs(u1) = 0x" << hex << abs(u1) << "\n";
^
absuns2.cpp:8:72: note: candidates are:
In file included from /usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:72:0,
from absuns2.cpp:1:
/usr/include/stdlib.h:129:6: note: int abs(int)
int abs(int) __pure2;
^
In file included from absuns2.cpp:1:0:
/usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:174:3: note: long long int std::abs(long long int)
abs(long long __x) { return __builtin_llabs (__x); }
^
/usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:166:3: note: long int std::abs(long int)
abs(long __i) { return __builtin_labs(__i); }
^
absuns2.cpp:10:72: error: call of overloaded ‘abs(long unsigned int&)’ is ambiguous
cout << "u2 = 0x" << hex << u2 << "; abs(u2) = 0x" << hex << abs(u2) << "\n";
^
absuns2.cpp:10:72: note: candidates are:
In file included from /usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:72:0,
from absuns2.cpp:1:
/usr/include/stdlib.h:129:6: note: int abs(int)
int abs(int) __pure2;
^
In file included from absuns2.cpp:1:0:
/usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:174:3: note: long long int std::abs(long long int)
abs(long long __x) { return __builtin_llabs (__x); }
^
/usr/gcc/v4.9.1/include/c++/4.9.1/cstdlib:166:3: note: long int std::abs(long int)
abs(long __i) { return __builtin_labs(__i); }
^
So, the code in the question only compiles when only the C-style headers are used; it doesn't compile when the C++ headers are used. If you add <stdlib.h>
as well as <cstdlib>
, there's an additional overload available to make the calls more ambiguous.
You can make the code compile if you add (in)appropriate casts to the calls to abs()
, and the absolute value of a signed quantity can be different from the original signed quantity, which is hardly surprising news:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void)
{
unsigned u1 = 0x98765432;
cout << "u1 = 0x" << hex << u1 << "; abs(u1) = 0x" << hex << abs(static_cast<int>(u1)) << "\n";
unsigned long u2 = 0x9876543201234567UL;
cout << "u2 = 0x" << hex << u2 << "; abs(u2) = 0x" << hex << abs(static_cast<long>(u2)) << "\n";
return 0;
}
Output:
u1 = 0x98765432; abs(u1) = 0x6789abce
u2 = 0x9876543201234567; abs(u2) = 0x6789abcdfedcba99
Moral: Don't use the C headers for which there are C++ equivalents in C++ code; use the C++ headers instead.
abs()
call doesn't make sense. However,abs()
is overloaded in C++ and could be a macro, too, so what it does is not 100% clear to me. It could even be a function takingsigned long
, which then does something depending on the MSB of the argument. What's the reason to take the absolute value of something that can't be negative? If there is none, just remove theabs()
call. – Ulrich Eckhardt Jan 8 '15 at 7:26a = 10UL;
andb = 30UL;
, the correct answer should be20UL
, but dropping theabs
would give me the wrong answer. – Ken Y-N Jan 8 '15 at 8:52abs()
is called,10UL - 30UL
yields4294967276UL
(assuming a 32-bit unsigned long). Ifabs()
does with that what it suggests (see my remarks concerning macros/overloads), the result is also4294967276UL
. If you want to have 20, you must use signed arithmetic, like e.g.abs(static_cast<long>(a) - static_cast<long>(b))
. You have to make sure that this doesn't wrap around though, as that yields "undefined behaviour". In summary, I'd suggest you use Mohit Jain's suggestion, i.e. first check which one is bigger. – Ulrich Eckhardt Jan 8 '15 at 20:41