Std :: abs for data type unsigned long long int

Why should I do this?

C2668: 'abs' : ambiguous call to overloaded function 

For simple code like this

 #include <iostream> #include <cmath> int main() { unsigned long long int a = 10000000000000; unsigned long long int b = 20000000000000; std::cout << std::abs(ab) << "\n"; // ERROR return 0; } 

The error is deleted after std:: is removed. However, if I use the int data type (with smaller values), there is no problem.

The traditional solution is to verify that manually

 std::cout << (a<b) ? (ba) : (ab) << "\n"; 

Is this the only solution?

+4
source share
4 answers

Verification seems to be the only really good solution. Alternatives require the type to be larger than yours and custom extensions in order to use it.

You can go with solutions cast up to a long long signing if your range is right. I would hardly suggest this method, especially if the implementation is placed in a function that does just that.

+10
source

You include <cmath> and thus use abs . "

" integer abs " is declared in <cstdlib> .

However, for unsigned long long int there is no overload (both a and b , so ab too), and overload for long long int exists only with C ++ 11.

+5
source

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.

+1
source

Since before C ++ with C you used abs, fabs, labs for each other type to use, C ++ allows abs overload, in which case it does not understand or is not happy with your overload.

Use labs(ab) , seeing how you use longs, this should solve your problem.

-2
source

Source: https://habr.com/ru/post/1487011/


All Articles