Home > Mobile >  Why this program is giving nan is the distance between the points?
Why this program is giving nan is the distance between the points?

Time:12-06

Why this program is giving nan is the distance between the points? I have made it to use friend function and oop concept but whenever i try to find the distance between coords it shows either zero of nan

#include<iostream>
#include<cmath>
using namespace std;
class point{
    int x,y;
    friend void disCoord(point,point);
    public:
    point(int a,int b){
        x=a;
        y=b;
    }
    void displaypoint(){
        cout<<"the point is("<<x<<","<<y<<")"<<endl;
    }

};//END OF CLASS
void disCoord(point o1,point o2){
    double dis=sqrt(pow(o2.x-o1.x,2)-pow(o2.y-o1.y,2));
    cout<<"The distance between point"<<"("<<o1.x<<","<<o1.y<<") and point"<<"("<<o2.x<<"," 
<<o2.y<<") is "<<dis<<endl;
}


int main(){
    point A=point(1,2);
    point B=point(1,3);
    A.displaypoint();
    B.displaypoint();
    disCoord(A,B);

return 0;
}

CodePudding user response:

The problem is that you are using incorrect formula which result in taking square root of negative number. In particular, in your case

dis = sqrt((1-1)2 - (3-2)2)

= sqrt(0 - 1) = sqrt(-1)

And i quote from nan documentation

The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0.

To solve this, replace double dis=sqrt(pow(o2.x-o1.x,2)-pow(o2.y-o1.y,2)); with:

double dis=sqrt(pow(o2.x-o1.x,2)   pow(o2.y-o1.y,2));

Note there is a in between while you were using - for some reason.

This is because the actual(correct) formula for calculating distance is:

d(P,Q) = sqrt((x2-x1)2 (y2-y1)2)

CodePudding user response:

As others said, the formula you're using is incorrect. BUT, the reason you're getting a NaN or 0 as a result is slightly more complicated.

Your test points (1,2) and (1,3) share the same X component. So, your formula:

double dis=sqrt(pow(o2.x-o1.x,2)-pow(o2.y-o1.y,2));

Resolves to:

dis = sqrt(pow(1-1,2) - pow(3-2,2))

or

dis = sqrt(0 - 1)

Which we all affectionately know as i, the square root of -1. That's one of the few cases that will (correctly) yield a result of NaN. (NAN means "Not A Number," in case that wasn't clear.)

The correct distance formula:

sqrt(pow(o2.x-o1.x,2)   pow(o2.y-o1.y,2))

Doesn't allow for a negative argument to sqrt, so this never happens.

It's tough to find these issues if you're not sure what you're looking for. It'd be easier to debug if you didn't do everything on one line:

double a2 = pow(o2.x-o1.x,2);
double b2 = pow(o2.y-o1.y,2);
double c2 = a2   b2;
double c = sqrt(c2);

That's overly verbose, but the general idea is to split complex statements up into components. It's easier to read, easier to debug, and, if you'd done this, you'd have seen that c2 was negative.

White space is cheap, carriage returns cost nothing. Don't scrimp on keystrokes in order to save time.

  • Related