Visual Studio does not allow me to use sqrt or gender, an ambiguous call to an overloaded function

I have a call

long long a = sqrt(n/2); 

Both a and n are long long , but it will not allow me to compile because it says that my use of sqrt() is an ambiguous call. I do not see here, perhaps ambiguous here. How to resolve this? I have the same problem with floor() .

My includes

 #include "stdafx.h" #include <iostream> #include <cmath> using namespace std; 
+6
source share
4 answers

There are several sqrt() and floor() overloads; there is no “best match” for calling sqrt(long long) in accordance with the rules for resolving overloads. Just pass the argument to the appropriate type - for example,

 long long a = sqrt(static_cast<double>(n/2)); 
+8
source
 //use sqrt(static_cast<double>(n/2)); //instead of sqrt(n/2); 
+6
source

sqrt functions expect a float , a double or long double :

 long long a = sqrt(n * 0.5); 

You may lose some precision converting long long to double , but the value will be very close.

+3
source

According to the link

http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

I would suggest first converting to a long double . No sqrt overload takes an integral value

the integral parameter can always lead to a "real" value (float, double, long double)

+1
source

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


All Articles