0

I am writing a program in C++ for the distance formula. The answer to x1=0 y1=0 x2=1 y2=1 should be around 1.14, however the answer printed out is 2.00. Every single variable is stored as double I don't know what is going wrong here. Here is my code, and thank you for any help!!

//  main.cpp
//  Chap6_42
//
//  Created on 10/21/14.
//

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double distance(double,double,double,double); //distance prototype


int main()
{
    double d = 0;

    double x1 = 0; //coordinate x1
    double x2 = 0; //coord x2
    double y1 = 0; //coord y1
    double y2 = 0; //coord y2




    cout << "Enter four cords (x1,y1,x2,y2) to find the distance between them " << endl;
    cout << "x1 = ";
    cin >> x1;
    cout << "y1 = ";
    cin >> y1;
    cout << "x2 = ";
    cin >> x2;
    cout << "y2 = ";
    cin >> y2;

    d = distance (x2,x1, y2,y1); //calls to distance function, performs computations

    cout << "The distance is " << fixed << setprecision(2) << showpoint << d << endl;



    return 0;
}
double distance(double x2,double x1,double y2,double y1) //distance function header
{

    return sqrt(pow(x2-x1,2.0)) + sqrt(pow(y2-y1,2.0)); //distance function computations


}
                                                //function definition
Zach
  • 85
  • 1
  • 1
  • 9
  • 1
    Pythagoras (hence a down vote) - sorry –  Oct 22 '14 at 20:47
  • apart from the downvote, why everyone posted an answer with "pow(x,2.0)". Basically there are 3 wrong answers because "pow(-3.0,2.0)" returns "-9.0" wich is wrong in this case because Zach wanted Pythagora. – CoffeDeveloper Oct 22 '14 at 20:58
  • possible duplicate of [distance calculation error in c++](http://stackoverflow.com/questions/4217733/distance-calculation-error-in-c) – CoffeDeveloper Oct 22 '14 at 21:01
  • @DarioOO This question is not a duplicate of that post. That question is about polluting the global namespace with `using namespace std;` and causing function name resolution problems. – Cory Kramer Oct 22 '14 at 21:06

5 Answers5

2

Why do you think the answer is around 1.14? Given your example, it should return 2.0

sqrt((pow(2.0 - 1.0, 2.0))) + sqrt((pow(2.0 - 1.0, 2.0)))
sqrt((pow(1.0, 2.0))) + sqrt((pow(1.0, 2.0)))
sqrt(1.0) + sqrt(1.0)
1.0 + 1.0
2.0

Tada!

If you are calculating distance, which the function name implies, you need to adjust your formula.

return sqrt(pow(abs(x2 - x1), 2.0) + pow(abs(y2 - y1), 2.0));
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

Your calculation is wrong. You're calling sqrt twice when you should only call it once on the entire sum.

return sqrt(pow(x2-x1,2.0) + pow(y2-y1,2.0));
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

Your formula is wrong.

You wrote:

sqrt(pow(x2-x1,2.0)) + sqrt(pow(y2-y1,2.0));

It should be:

sqrt(pow(x2-x1,2.0) + pow(y2-y1,2.0));

Anyway, do not use pow there but multiply by hand, that's (probably) faster and more accurate.

sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );

Also, often you can use squared distances instead of the distance for a small performance-boost.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
1

You and pythagorus disagree about how to calculate the distance

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
-1

Given you intend to use "pow" for some esoteric reason, your code should be

return sqrt(abs(pow(x2-x1,2.0)) + abs(pow(y2-y1,2.0)));

or

return sqrt(pow(x2-x1,int(2)) + pow(y2-y1,int(2)));

If you are going to programming you'll probably need a lot of math. you need to know "on paper" the algorithms you are going to use (even simple ones like the one by Pitagora). Also a rounding error with doubles is not going to change a result so much (unless you are using some bad conditioned algorithm, wich is not the case for Pitagora.

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • Apparently my answer is downvoted for no reason, it is not worst than other answers. downvoter, be polite and explain your reasons. If the downvote is for "abs" usage, then downvoter read more carefully what abs and pow are really doing and you'll see my code is working, while code of currently upvoted answers is not working. – CoffeDeveloper Dec 23 '14 at 14:58