First, you need to include the correct title. As gx_ pointed out, <cmath> has abs and floating point abs on my compiler, which it actually compiles, but the result is probably not the one you expected:
1.84467e+19
Include <cstdlib> . Now the error:
main.cpp:7:30: error: call of overloaded 'abs(long long unsigned int)' is ambiguous main.cpp:7:30: note: candidates are: /usr/include/stdlib.h:771:12: note: int abs(int) /usr/include/c++/4.6/cstdlib:139:3: note: long int std::abs(long int) /usr/include/c++/4.6/cstdlib:173:3: note: long long int __gnu_cxx::abs(long long int)
As you can see, overloading this function is not unsigned , because calculating the absolute value of something like unsigned does not make sense.
I see answers suggesting you use the unsigned type for a signed one, but I think it's thoughtful if you really don't know what you are doing! Let me first ask, what is the expected range of values โโof a and b that you are going to work on? If both are below 2^63-1 , I would strongly suggest just using long long int . If this is not the case, then let me note that your program is for values:
a=0, b=1
and
a=2^64-1, b=0
will give exactly the same result, because in fact you need 65 bits to represent any possible result of the difference in 2 64-bit values. If you can confirm that this will not be a problem, use the cast as suggested. However, if you do not know, you may need to rethink what you are actually trying to achieve.
source share