problem using 2tan

Hi everyone,

I am trying to use a coordinate formula called haversine formula for my project to find distance between two points on earth. The 2tan function appears to have no effect on the code, yet the math.h should be able to detect it. what am i doing wrong ?
E.G
[Paris : lat1 48.5. long1 2.2 , london: lat2 51.4, long2 0.05...answer should be ~343 km BUT INSTEAD I GET 16414KM !]

#define pi 3.1415926535897

#include<iostream>
#include<math.h>



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

cout << "enter in radians " << endl;
cin >> lat1;
cin >> long1;
cin >> lat2;
cin >> long2;


latz = (lat2 - lat1);
longz = (long2 - long1);
cout << latz << endl;
cout << longz << endl;
a = sin(latz/2) * sin(latz/2) + cos(lat1) * cos(lat2)
* sin(longz/2) * sin(longz/2);

cout << " A is equal to: " << a << endl;
x = sqrt(a);
cout << x << endl;
y = sqrt(1-a);
cout << y << endl;
c = 2* atan2(x ,y); // <-------- problem appears to be here

cout << " C is equal to: " << a << endl; //great circle distance

d = 6371 * c; //greatcircle distance in km

cout << " and D is: " << d <<"km";
char in;
cin >> in;
}
Your problem is you didn't convert to radians
1
2
3
4
5
6
lat2*=pi/180;
lat1*=pi/180;
long1*=pi/180;
long2*=pi/180;
latz = (lat2 - lat1);
longz = (long2 - long1);


also fix this line:
 
cout << " C is equal to: " << c << endl; //great circle distance 


In my opinion, doubles are better than floats.
Last edited on
thanks for the help, greatly appreciated.
Topic archived. No new replies allowed.