sqrt problem in c++
Why are there errors here?
#include <iostream>
#include <math.h>
#include <vector>
#include <ostream>
using namespace std;
struct Point {
int x,y;
};
int distance (const Point& a,const Point& b){
int k= sqrt(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
}
int main(){
return 0;
}
Build output:
1>------ Build started: Project: distance, Configuration: Debug Win32 ------
1> distance.cpp
1>d:\...\distance.cpp(13): error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\...\vc\include\math.h(589): could be 'long double sqrt(long 开发者_如何学Cdouble)'
1> c:\...\vc\include\math.h(541): or 'float sqrt(float)'
1> c:\...\vc\include\math.h(127): or 'double sqrt(double)'
1> while trying to match the argument list '(const int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There are three overloads of sqrt
which take different parameters: float sqrt(float)
, double sqrt(double)
and long double sqrt(long double)
. You can see these in the compiler output.
If you call sqrt
with an integer parameter, like sqrt(9)
, an integer can be cast to any of those three types. So which function should be called? The compiler doesn't know. It's ambiguous, so you get an error to force you to explicitly choose the overload you want. Just cast the parameter to match one of the overloads like this: sqrt(static_cast<float>(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)))
.
this should work
float k= sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
sqrt() does not take int as a parameter.
You cannot take the sqrt of an integer. It needs to be a floating point number.
You need to do something like this:
int k= (int)sqrt((double)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
The (double) will convert the int to a double ant the (int) converts it back to an int afterwards. You should also consider whether or not you want to use doubles consistently.
sqrt takes a double ( apparently various different doubles in your compiler) - you are passing it an int
just do sqrt( (double) .... )
Ok - to be more precise, sqrt() must take a floating point number - either a float or a double. For various historical reasons it's generally able to convert between different floating point types. The bit of your CPU doing the sqrt calculation is probably (assuming x86) doing the calculation in 80bits which is neither a float nor a double/
There are three sqrt-methods: One that takes a long, one that take a float and one that takes a double value.
Try
int k= (int)sqrt((double)(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y))));
to tell the compiler that you want to use the double version and then convert back to int.
You're in effect passing an int
to sqrt
, which only takes arguments of type float
, double
or long double
. Additionally, sqrt
doesn't return an int
. The compiler can't guess what type conversion to make, so you'll have to cast it yourself either using C-style casts or "new style" casts:
float k = sqrt((float)(...));
float k = sqrt(static_cast<float>(...));
Also, indent your code properly; it makes reading it much easier.
精彩评论