Below are three test cases with results in metres for the pairs of Delta geographic co-ords.
The results shown in brackets match both with Excel and Google Maps.
However, using the math.h library anything below 1000m is very inaccurate
Obviously as the distance goes up the inaccuracy get swamped.
I have checked that 'treat doubles as floats' is not set.
Why are my results so inaccurate please?
//-42.78724667 147.246516667 to -42.7872449999 147.2465333 (1.36m) 30m with math.h
// -42.7872466666, 147.247016667, -42.7872449999, 147.247742 (100m) 60m with math.h
// -42.7872466666, 147.247016667, -42.7872449999, 147.25877 (1000m) 990m with math.h
double result = haversine(-42.7872466666, 147.247016667, -42.7872449999, 147.247742);
#define M_PI (double)3.14159265358979
double haversine(double lat1, double lon1, double lat2, double lon2)
// Convert degrees to radians
lat1 = lat1 * M_PI / (double)180.0;
lon1 = lon1 * M_PI / (double)180.0;
lat2 = lat2 * M_PI / (double)180.0;
lon2 = lon2 * M_PI / (double)180.0;
double dlat = lat2-lat1;
double dlon = lon2-lon1;
double sumlat = lat1 + lat2;
double calc = dlon * cos(sumlat/(double)2.0);
double distance = sqrt(calc + dlat) * (double)6371000.0;
Please sign in to leave a comment.