问题
In the program below, I am trying to calculate the distance between two points. For this, I have made two Point objects. In the method that returns the distance, I have used the distance formula to calculate distance between two points in space. However, every time I run the program, I get a not a number value, which shouldn't be there. Please help.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int a, int b);
~Point();
double getDistance(Point& P2);
void setPoints(int a, int b);
int getX();
int getY();
private:
int x;
int y;
};
Point::Point(int a, int b)
{
setPoints(a,b);
}
Point::~Point()
{
//Nothing much to do
}
void Point::setPoints(int a, int b)
{
x = a;
y = b;
}
double Point::getDistance(Point& P2)
{
int xdiff = P2.getX()-this->getX();
int ydiff = P2.getY()-this->getY();
xdiff = xdiff*xdiff;
ydiff = ydiff*ydiff;
double retval = sqrt((xdiff) - (ydiff));
return retval;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
int main(int argc, char* argv[])
{
Point P1(0,0);
Point P2(0,1);
Point& pr = P2;
cout<<P1.getDistance(pr)<<endl;
return 0;
}
回答1:
Your formula is wrong. It's not
sqrt(xdiff - ydiff)
but
sqrt(xdiff + ydiff)
You're trying to get the sqrt(-1)
which is indeed not a number (or not a real number).
回答2:
Here's how to figure this sort of thing out for yourself, or at least get a lot closer to a good StackOverflow question:
You know the problem is in the sqrt()
call. So, what is it being called with? In this case, you could trace through the computation manually:
int xdiff = P2.getX()-this->getX(); // this is 0 - 0, which is 0.
int ydiff = P2.getY()-this->getY(); // this is 1 - 0, which is 1.
xdiff = xdiff*xdiff; // this is still 0.
ydiff = ydiff*ydiff; // this is still 1.
double retval = sqrt((xdiff) - (ydiff)); // this is sqrt(0 - 1), or sqrt(-1).
Alternately, in more complicated cases -- and to check your work, you could either use a debugger to print out the values of the arguments, or you could insert print statements:
xdiff = xdiff*xdiff;
ydiff = ydiff*ydiff;
cout << 'xdiff: ' << xdiff << ' ydiff: ' << ydiff << endl
cout << 'computing sqrt(' << xdiff - ydiff << ')' << endl
double retval = sqrt((xdiff) - (ydiff));
Either way, you now know that you're computing sqrt(-1)
, and you can try running that directly to confirm that it does indeed produce the same result. So either you have a question of "Why is sqrt(-1)
returning NaN
?" or a question of "Why is my distance calculation trying to compute the square root of a negative number?"
Hopefully you already know the answer to the first question, and the second question should indicate that you need to double-check your distance formula, which should have showed you the answer pretty quickly -- but even if you can't figure out why it's doing that, it at least makes a more useful question to ask here.
回答3:
You should be adding here, not subtracting:
double retval = sqrt((xdiff) - (ydiff)); // correct is +
Subtracting causes you to take the square root of -1 due to the input data, which is not a (real) number.
回答4:
As craigmj said, the formula for distance is sqrt ((x1-x2) + (y1-y2)). It's addition not subtraction. What your doing is generating an imaginary number (sqrt (-1)) which will cause an error.
Just a tip of advice, but do not create the destructor if it doesn't do anything; a destructor will be provided for you. Adding a destructor that doesn't do anything just adds unneeded code and makes it look messier.
Also in the getDistance function, you do not need to use this ->getX() and this-> getY(). Since this is a member function it has access to private data, therefore you can directly access the variables through x and y.
来源:https://stackoverflow.com/questions/8950603/squareroot-returning-not-a-number-in-c