A curiosity journal of math, physics, programming, astronomy, and more.

Great circle distance formulas

To calculate the distance between two points on a sphere, I've used the haversine formula, specifically this expression:

d equals 2 r arc sine StartRoot sine squared left-parenthesis StartFraction normal psi 2 minus normal psi 1 Over 2 EndFraction right-parenthesis plus cosine normal psi 1 dot cosine normal psi 2 dot sine squared left-parenthesis StartFraction normal lamda 2 minus normal lamda 1 Over 2 EndFraction right-parenthesis EndRoot

in which ψn denotes the latitude and λn the longitude of points on the sphere. Though Wikipedia provides derivation and proof of the formula's validity, it's still somewhat opaque, and every time I need it I have to look it up to remember the specifics.

An alternative approach is to convert latitude and longitude to Cartesian coordinates, then apply the geometric definition of dot product and the definition of radians:

StartLayout 1st Row 1st Column ModifyingAbove a With right-arrow 2nd Column equals mathematical left-angle r sine normal psi 1 cosine normal lamda 1 comma r sine normal psi 1 sine normal lamda 1 comma r cosine normal psi 1 mathematical right-angle 2nd Row 1st Column ModifyingAbove b With right-arrow 2nd Column equals mathematical left-angle r sine normal psi 2 cosine normal lamda 2 comma r sine normal psi 2 sine normal lamda 2 comma r cosine normal psi 2 mathematical right-angle 3rd Row 1st Column ModifyingAbove a With right-arrow dot ModifyingAbove b With right-arrow 2nd Column equals double-vertical-bar ModifyingAbove a With right-arrow double-vertical-bar double-vertical-bar ModifyingAbove b With right-arrow double-vertical-bar cosine normal theta 4th Row 1st Column normal theta 2nd Column equals arc cosine StartFraction ModifyingAbove a With right-arrow dot ModifyingAbove b With right-arrow Over double-vertical-bar ModifyingAbove a With right-arrow double-vertical-bar double-vertical-bar ModifyingAbove b With right-arrow double-vertical-bar EndFraction 5th Row 1st Column d 2nd Column equals r normal theta EndLayout

Combining these equations into one produces this unweildy expression:

d equals r arc cosine left-parenthesis StartFraction r squared sine normal psi 1 cosine normal lamda 1 sine normal psi 2 cosine normal lamda 2 plus r squared sine normal psi 1 sine normal lamda 1 sine normal psi 2 cosine normal lamda 2 plus r squared cosine normal psi 1 cosine normal psi 2 Over StartRoot r squared sine squared normal psi 1 cosine squared normal lamda 1 plus r squared sine squared normal psi 1 sine squared normal lamda 1 plus r squared cosine squared normal psi 1 EndRoot dot StartRoot r squared sine squared normal psi 2 cosine squared normal lamda 2 plus r squared sine squared normal psi 2 sine squared normal lamda 2 plus r squared cosine squared normal psi 2 EndRoot EndFraction right-parenthesis

Superficially, that's less intelligible and harder to remember than the haversine formula, which by comparison is not only shorter but more symmetric.

Fortunately, we don't need a single equation. Executing the steps one at a time works just as well, and I'd argue better. The individual steps are more general and apply to a wider variety of situations, making them more worth committing to memory. Also, conversion of spherical to Cartesian coordinates can be extended to higher dimensions, which allows calculation of great circle distances on a hypersphere, an operation for which I have yet to find or derive an equivalent haversine-like formula (if you know of one, please email me).

Despite the clarity of the dot product approach, using the haversine formula is more computationally efficient. The more classical math I play with, the more I suspect that many of the arcane expressions we're used to weren't originally written for comprehension but instead for calculation, especially for computation by hand. For example, Wikipedia notes another haversine formula, which simplifies to:

d equals 2 r arc sine StartRoot normal h normal a normal v left-parenthesis normal psi 2 minus normal psi 1 right-parenthesis plus left-parenthesis 1 minus normal h normal a normal v left-parenthesis normal psi 2 minus normal psi 1 right-parenthesis minus normal h normal a normal v left-parenthesis normal psi 1 plus normal psi 2 right-parenthesis right-parenthesis dot normal h normal a normal v left-parenthesis normal lamda 2 minus normal lamda 1 right-parenthesis EndRoot

The article notes that actual computation of that formula was eased by tables of haversine values. With reusable tables, the above calculation probably would have required about five lookups: three different haversine values, a square root, and an arcsine. Operations like converting to Cartesian coordinates and working out dot products may compose well, but it's hard to imagine what reusable tables could reduce the calculation to only five table lookups. I count four sine lookups, four cosine lookups, two square root lookups, and an arccosine lookup—eleven lookups, not to mention all the multiplication.

When computation was difficult, it made sense to do some extra work to find easier-to-calculate formulas, even if it obscured the underlying geometry. Most of the people doing such math historically didn't need to understand the geometry, just get an answer. Despite faster computation today, the same is still true, when computations may need to run millions or billions of times.

However, for instructive purposes, and for my own understanding, I prefer the composition of a few fundamentals to hundreds of efficient yet bespoke formulas.