Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio not letting me use sqrt or floor, ambiguous call to overloaded function

Tags:

c++

sqrt

I have a call to

long long a = sqrt(n/2);

Both a and n are long long's but it won't let me compile because it says my use of sqrt() is an ambiguous call. I don't see how it's possibly ambiguous here at all. How do I resolve this? I have the same problem with floor().

My includes

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
like image 736
MyNameIsKhan Avatar asked Jun 10 '12 16:06

MyNameIsKhan


3 Answers

There are several overloads of sqrt() and floor(), there's no "best match" for a call to sqrt(long long) according to the overload resolution rules. Just cast the argument to the appropriate type -- i.e.,

long long a = sqrt(static_cast<double>(n/2));
like image 106
Ernest Friedman-Hill Avatar answered Nov 07 '22 15:11

Ernest Friedman-Hill


//use 
sqrt(static_cast<double>(n/2));
//instead of 
sqrt(n/2);
like image 29
Maziar Aboualizadehbehbahani Avatar answered Nov 07 '22 14:11

Maziar Aboualizadehbehbahani


The sqrt functions expects a float, a double or a long double:

long long a = sqrt(n * 0.5);

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

like image 3
fredoverflow Avatar answered Nov 07 '22 13:11

fredoverflow