the distance between two points (again)

Started by
4 comments, last by mrtie_dye 18 years, 10 months ago
Ok, I finally got around to messing with finding the distance between two points. But I noticed that most of the time, the ditance is 0. For example, the code below prints out "Why did it not work?"

int main()
{
	int x1 = 10;
	int y1 = 10;
	int x2 = 70;
	int y2 = 70;
		
	if(sqrt((x2-x1)^2 + (y2-y1)^2) != 0)
	     std::cout << "YEAH, it worked!";
	else
	     std::cout << "Why did it not work?";
}

But if I change the two 70's to 60's, it prints out "YEAH, it worked!" And if I change the two 10's to 0's and the two 70's to 50's, then just do this.. std::cout << sqrt((x2-x1)^2 + (y2-y1)^2); It prints out a 2. What does 2 represent? What am I doing wrong? Thanks
Marriage is the #1 cause of divorce
Advertisement
http://en.wikipedia.org/wiki/Distance

Why don't you print out the actual answer? You are assuming anything that isn't a 0 is the right answer.
Instead of using integers try using floats.
Quote:
std::cout << sqrt((x2-x1)^2 + (y2-y1)^2);


outch...
do you know what operator ^ means? I think you don't.
^ is the EXCLUSIVE OR OPERATOR, not the power.

What you want to do is x*x + y*y, or pow(x,2)+pow(y,2), not x^2+y^2.
our new version has many new and good features. sadly, the good ones are not new and the new ones are not good
In addition to using ^ instead of pow or *, you are comparing the result of sqrt, which I believe returns a double, to an integer 0. You may actually get away with this, since I think 0 is exactly representable in real implementations (erm, maybe even has to be?), but in general, using an equality operator on a floating point value is a bad idea.
const float epsilon = 0.000001;float x1 = 10.0f;float y1 = 10.0f;float x2 = 70.0f;float y2 = 70.0f;float dx = x2 - x1;float dy = y2 - y1;float distance = sqrt(dx * dx + dy * dy);if (distance < epsilon)    std::cout << "they are very very close\n";else    std::cout << "they are not very very close\n";


If you really do want exactly the same, and you really do mean integers, either compare x1 == x2 && y1 == y2, or leave the square root operation out and compare the square of the distance with 0.
Sorry, I have been on a "programming vacation" for a while. I am sort of warming up my programming muscles, so to speak.

Quote:
Why don't you print out the actual answer?

I did print out the actual answer when I was testing it. I changed the code when I posted it here to illustrate that the answer was usually 0.
Quote:
do you know what operator ^ means? I think you don't.

You would be correct. I copied the code out of another post a while back. I just assumed it was for powers.
Quote:
the result of sqrt, which I believe returns a double

Good to know. I guess I should have done a little more homework first. :)

At any rate, as usual, you guys really came through. Thanks so much for all your input.
Marriage is the #1 cause of divorce

This topic is closed to new replies.

Advertisement