Fix a minor issue calculating geological distance when acos produces NaN

As well as how the distances between geological points were calculated

Calculating the distance between two points on the earth is fundamental to calculate all the measurements mentioned in earlier posts.
The distance in kilometers is calculated using the Spherical Law of Cosines:
d = acos( sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2-long1) )  * 6371 (km)
Step-by-step explanation:
The sin and cos functions take latitudes and longitudes input in radians in most softwares. For example, Shanghai has a coordination of (31.222, 121.458), its (lat1, long1) in radians is (31.222/180 * pi, 121.458/180 * pi) = (0.55, 2.12), pi = 3.14159...
The coordination for New York is (40.717, -74.004), in radian: (0.71, -1.29).

sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2-long1) 
sin(0.55)*sin(0.71) + cos(0.55)*cos(0.71)*cos(-1.29 - 2.12)
= -0.28  ---> this output is a value from -1 to 1

The angle AOB formed by A (Shanghai), O (Center of Earth), and B (New York) in the figure below (borrowed from: link, just to give an idea) is acos(-0.28) = 1.86 radians, or in degrees: 1.86/3.14*180 = 106.6°.
We use the angle in radians directly to get the length of the arc: 
d = acos(-0.283) * (radius of Earth) = 1.86 * 6371 = 11836 km. 
We can program this in R, or use package. Without rounding error in the calculation above, the more accurate result from R is 11859 km. In SAS there is a geodist function to get the same result. If we google the distance between Shanghai and New York the result is 11851 km. 

The minor issue is: sometimes in R the acos function can return NaN while the input is shown as 1. Somehow the calculated input from (sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2-long1)) shown as 1 is not really 1 due to rounding of small decimals. If c = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2-long1), using acos(pmin(c, 1)) to set the up bound to 1 avoids producing NaN. I use "pmin" (parallel min) here because c is a vector in my case. This issue was discussed in this post.

Comments

Popular posts from this blog

How to Draw Heatmap with Colorful Dendrogram

Power-law distribution (Pareto)& Zipf's Law: connection and how to fit the distribution of global city population

SAS’s Best Subset Selection by Mallows's Cp is actually Stepwise?