user1815763
user1815763

Reputation: 103

calculate distance of two cities using Haversine formula-how to deal with minus longitudes

I'm going to find the distance between two cities using Haversine formula. below is the code in VC++.

But I could't find the distance between the points (18.567367, -68.363431) and (33.636719,-84.428067) [first value is latitude, second is longitude]. It gives something like -1.#IND.

Could you please tell me how to deal with this issue?

below is the code:

double deg2rad(double deg) {
  return (deg * pi / 180);
};

double TravelDistance(double lat1d, double lon1d, double lat2d, double lon2d) {
  double lat1r, lon1r, lat2r, lon2r, u, v,dblDistance;
  lat1r = deg2rad(lat1d);
  lon1r = deg2rad(lon1d);
  lat2r = deg2rad(lat2d);
  lon2r = deg2rad(lon2d);
  u = sin(lat2r - lat1r);
  v = sin(lon2r - lon1r);

 return ( 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)))};

Thank in advance....!!

Upvotes: 0

Views: 1732

Answers (1)

tjltjl
tjltjl

Reputation: 1479

Looking at

http://en.wikipedia.org/wiki/Haversine_formula

it appears you have forgotten to divide by two in the arguments to sin() for u and v.

The answer you get is most likely because either the sqrt argument is < 0 or because the asin argument is > 1.

Upvotes: 1

Related Questions