private double calculateDistance(double lat1, double lon1, double lat2, double lon2) { double R = 20902230.97112861; //feet double dLat = (lat2-lat1) * (exMath.PI / 180.0); double dLon = (lon2-lon1) * (exMath.PI / 180.0); double a = exMath.Sin(dLat/2.0) * exMath.Sin(dLat/2.0) + exMath.Cos(lat1 * (exMath.PI / 180.0)) * exMath.Cos(lat2 * (exMath.PI / 180.0)) * exMath.Sin(dLon/2) * exMath.Sin(dLon/2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a)); double d = R * c; return d; }
The problem arises when, *I think*, the distance gets small between the two points (say 1000 feet or so), the
exMath.Cos(lat1 * (exMath.PI / 180.0)) * exMath.Cos(lat2 * (exMath.PI / 180.0))
part goes to zero instead of being a really small number and then I get an incorrect result. Like the double in the .Net micro has less precision than it's desktop counterpart...
Any insight here would be greatly appreciated!
Ps.. I'm using this library for the math operations.