平方根在 C++ 中返回的不是数字
在下面的程序中,我试图计算两点之间的距离。为此,我制作了两个 Point 对象。在返回距离的方法中,我使用距离公式来计算空间中两点之间的距离。但是,每次运行该程序时,我都会得到一个不应该存在的非数字值。请帮忙。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的公式是错误的。不是,
但
您正在尝试获取
sqrt(-1)
,它确实不是一个数字(或不是一个实数)。Your formula is wrong. It's not
but
You're trying to get the
sqrt(-1)
which is indeed not a number (or not a real number).以下是如何自己解决此类问题,或者至少更接近一个好的 StackOverflow 问题:
您知道问题出在
sqrt()
调用中。那么,它是用什么来调用的呢?在这种情况下,您可以手动跟踪计算:或者,在更复杂的情况下 - 为了检查您的工作,您可以使用调试器打印出参数的值,也可以插入打印语句:
无论哪种方式,您现在知道您正在计算
sqrt(-1)
,并且您可以尝试直接运行它以确认它确实产生相同的结果。所以要么你有一个问题“为什么sqrt(-1)
返回NaN
?”或者“为什么我的距离计算试图计算负数的平方根?”的问题希望您已经知道第一个问题的答案,第二个问题应该表明您需要仔细检查您的距离公式,这应该很快就会向您显示答案 - 但即使您无法弄清楚为什么会这样这样做,至少可以在这里提出一个更有用的问题。
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: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:
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 issqrt(-1)
returningNaN
?" 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.
您应该在此处进行加法,而不是减法:
由于输入数据不是(实数)数字,减法会导致您取 -1 的平方根。
You should be adding here, not subtracting:
Subtracting causes you to take the square root of -1 due to the input data, which is not a (real) number.
正如craigmj所说,距离的公式是sqrt((x1-x2) + (y1-y2))。是加法不是减法。你所做的是生成一个虚数(sqrt(-1)),这会导致错误。
只是一个建议,但如果析构函数不执行任何操作,则不要创建它;将为您提供一个析构函数。添加一个不执行任何操作的析构函数只会添加不需要的代码并使其看起来更混乱。
同样在getDistance函数中,不需要使用this->getX()和this->getX()。获取Y()。由于这是一个成员函数,它可以访问私有数据,因此您可以通过 x 和 y 直接访问变量。
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.