1

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;
}
uyetch
  • 2,150
  • 3
  • 28
  • 33
  • What do you think will happen if ydiff is greater than xdiff? – Jim H. Jan 21 '12 at 04:20
  • Downvoted; questions that are (mostly) just a giant wall of unreduced code are not likely to be relevant or helpful to anyone other than the person asking the question. You could have substantially simplified this by removing all of the `Point` object overhead, and just written it as `p1_x = 0; p1_y = 0; p2_x = 0; p2_y = 0; dist = sqrt((p2_x - p1_x) * (p2_x - p1_x) - (p2_y - p1_y) * (p2_y - p1_y));`, which shows the same error in five lines of code. And you could have even reduced it further, since you knew the problem was in the `sqrt()` call.... – Brooks Moses Jan 21 '12 at 05:41

4 Answers4

7

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).

craigmj
  • 4,827
  • 2
  • 18
  • 22
3

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.

Brooks Moses
  • 9,267
  • 2
  • 33
  • 57
1

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

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.

fdh
  • 5,256
  • 13
  • 58
  • 101