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

4

6

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;
Scofield answered 10/6, 2012 at 16:11 Comment(1)
Try casting the n/2 to double.Ursine
F
8

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));
Flogging answered 10/6, 2012 at 16:14 Comment(0)
D
6
//use 
sqrt(static_cast<double>(n/2));
//instead of 
sqrt(n/2);
Danieldaniela answered 10/6, 2012 at 16:15 Comment(8)
@Konrad Rudolph Kinderkram and also unconsistent unfair with other answersTopmast
@Stefan Not Kinderkram, it’s a substantial percentage of the whole answer, as well as being the central point. As for being inconsistent, I try to be consistent, and I will downvote any answer which, as its central point, advocates a practice which I oppose.Orta
@MaziarBouali C++. And the question is tagged C++. The C library you’re thinking of is math.h.Orta
@MaziarBouali, the C header is math.h. The C++ one is cmath. The prominent difference is that one is part of the std namespace.Expectation
@KonradRudolph, To be fair, I think he means that Ernest's answer used a C-style cast too, before it got changed.Expectation
@MaziarBouali Totally irrelevant. Besides the fact that this reference is outdated and contains errors, this code is clearly C++. What language the library is written in is irrelevant. And even if it weren’t, §26.8 of the C++ standard includes this library.Orta
@KonradRudolph, Oh, I thought it didn't have any when I checked. Maybe you did it right after :pExpectation
@MaziarBouali: note that if you changed (double) to static_cast<double>() form and pinged Konrad about it I am pretty sure he would remove his downvote. Downvotes on SO are (generally) not ad-hominem attacks, and most pave the way to a better answer.Dianthe
C
3

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.

Cantoris answered 10/6, 2012 at 16:15 Comment(4)
I don't understand why I need to change it to a double? Won't I lose accuracy/possibly truncate my variable? My n variable is quite largeScofield
@AgainstASicilian Well, there simply is no version for long long.Orta
Oh, I wasn't aware of that. For which datatypes does sqrt() and floor() exist? Int and double?Scofield
@AgainstASicilian These functions have overloads for float, double, and long double. Since there's no version for long long the compiler has to convert your long long to one of those other types. Since all those conversions are equally good the compiler can't choose any one over the others and the call is ambiguous.Choosey
T
1

According to the reference

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

I would propose to convert to long double first. No overload of sqrt accepts an integral value

integral parameter could always result in a "real" value (float, double, long double)

Topmast answered 10/6, 2012 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.