#include<iostream>
#include<cmath>
#define pi 3.1415926535897


using namespace std;
int main ()
{
double lat1;
double long1;
double lat2;
double long2; // initilaise values
double latz;
double longz;
double a = 0;
double c = 0;
double distance = 0;
double x = 0;
double y = 0;

cout << "enter lattitude 1 " << endl;
cin >> lat1;
cout << "enter longtitude 1 " << endl;
cin >> long1;
cout << "enter lattitude 2 " << endl;
cin >> lat2;
cout << "enter longtitude 2 " << endl;
cin >> long2;

lat1=lat1*(pi/180);
long1=long1*(pi/180);
lat2=lat2*(pi/180);
long2=long2*(pi/180);

latz = (lat2 - lat1)*(pi/180);
longz = (long2 - long1)*(pi/180);

x= pow(cos(lat2)*sin(longz),2.0) + pow((cos(lat1)*sin(lat2)) - sin(lat1)*cos(lat2)*cos(longz),2.0); 

y= (sin(lat1)*sin(lat2)) + (cos(lat1)*cos(lat2)*cos(longz));

a = sqrt(x);
c = 2* atan2(x ,y); 

distance = 6371.009 * c; //distance in km

cout << "Distance is: " << distance <<"km" << endl;

}

getting different answers for online caalculators & from my own code for example, if choose lat1,long1 &lat2,long 2 as 2,1 & 4,3 respectively i get roughly 15.53km, whilst on online calculators it comes out as 314.3km. this is the online version http://www.movable-type.co.uk/scripts/latlong.html any idea where i'm going wrong?

On line 38 you have

x= pow(cos(lat2)*sin(longz),2.0) + pow((cos(lat1)*sin(lat2))/*<--no exponent*/ - sin(lat1)*cos(lat2)*cos(longz),2.0); 

The second pow function has no exponent.

The second pow function has no exponent.

I thought this at first, but if you look closer you'll realise that it does.

On line 42. a = sqrt(x);
What is the purpose of a and where should it be used? (line 43?)

commented: Nice catch. +10

Perhaps you should asign to x it's sqrt(x) rather than to a variable that you'll never use. Either sqrt(x) is something added which has no effect on the code, or you missplaced the variables. Look a bit to the formulae and than apply the correct transformations.

im confident i've typed all in right, as i wrote it down on paper 1st, is it something to do about latz & longz, is it needed in degrees? lucaci, i'm not 100% sure what you mean,sorry!

  1. c = 2* atan2(x ,y);
    think it should be atan2(a ,y);
    that makes more sense i think?

did that & now i'm getting 444.8 km?

Since you have all of you lats and longs mulitiplied by pi/180 do you need that on lines 35 and 36?

This should work.

int main ()
{
    double lat1;
    double long1;
    double lat2;
    double long2;
    double latz;
    double longz;
    double c = 0;
    double distance = 0;
    double x = 0;


    cout << "enter latitude 1 " << endl;
    cin >> lat1;
    cout << "enter longitude 1 " << endl;
    cin >> long1;
    cout << "enter latitude 2 " << endl;
    cin >> lat2;
    cout << "enter longitude 2 " << endl;
    cin >> long2;

    lat1 = lat1 * (pi/180);
    long1 = long1 * (pi/180);
    lat2 = lat2 * (pi/180);
    long2 = long2 * (pi/180);
    // already radians
    latz = lat2 - lat1;
    longz = long2 - long1;

    x = pow(sin(latz/2), 2.0) + cos(lat1) * cos(lat2) * pow(sin(longz/2), 2.0);

    c = 2 * atan2(sqrt(x), sqrt(1 - x)); 

    distance = 6371.009 * c; //distance in km

    cout << "Distance is: " << distance <<"km" << endl;

    cin.ignore(90, '\n');

    getchar();

    return 0;
}

it was this line "c =2* atan2(a ,y);" i dont need the 2* part. have it working now.

#include<iostream>
#include<cmath>
#define pi 3.1415926535897


using namespace std;
int main ()
{
double lat1;
double long1;
double lat2;
double long2; // initilaise values
double latz;
double longz;
double a = 0;
double c = 0;
double distance = 0;
double x = 0;
double y = 0;

cout << "enter lattitude 1 " << endl;
cin >> lat1;
cout << "enter longtitude 1 " << endl;
cin >> long1;
cout << "enter lattitude 2 " << endl;
cin >> lat2;
cout << "enter longtitude 2 " << endl;
cin >> long2;

lat1=lat1*(pi/180);
long1=long1*(pi/180);
lat2=lat2*(pi/180);
long2=long2*(pi/180);

latz = (lat2 - lat1);
longz = (long2 - long1);

x= pow(cos(lat2)*sin(longz),2.0) + pow((cos(lat1)*sin(lat2)) - sin(lat1)*cos(lat2)*cos(longz),2.0); 

y= (sin(lat1)*sin(lat2)) + (cos(lat1)*cos(lat2)*cos(longz));

a = sqrt(x);
c = atan2(a ,y); 

distance = 6371.009 * c; //distance in km

cout << "Distance is: " << distance <<"km" << endl;

}

seems right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.