Zach
Zach

Reputation: 95

C++ double rounds up when unwanted

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

Upvotes: 0

Views: 258

Answers (5)

Cory Kramer
Cory Kramer

Reputation: 117856

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

Upvotes: 2

CoffeDeveloper
CoffeDeveloper

Reputation: 8317

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.

Upvotes: -1

Deduplicator
Deduplicator

Reputation: 45654

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.

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96109

You and pythagorus disagree about how to calculate the distance

Upvotes: 1

Rob Kennedy
Rob Kennedy

Reputation: 163247

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

Upvotes: 2

Related Questions