Haversine formula

The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.

The first table of haversines in English was published by James Andrew in 1805, but Florian Cajori credits an earlier use by José de Mendoza y Ríos in 1801. The term haversine was coined in 1835 by James Inman.

These names follow from the fact that they are customarily written in terms of the haversine function, given by $hav θ = sin^{2}(θ⁄2)$. The formulas could equally be written in terms of any multiple of the haversine, such as the older versine function (twice the haversine). Prior to the advent of computers, the elimination of division and multiplication by factors of two proved convenient enough that tables of haversine values and logarithms were included in 19th- and early 20th-century navigation and trigonometric texts. These days, the haversine form is also convenient in that it has no coefficient in front of the $sin^{2}$ function.

Formulation
Let the central angle $&theta;$ between any two points on a sphere be:
 * $$\theta = \frac{d}{r}$$

where
 * $d$ is the distance between the two points along a great circle of the sphere (see spherical distance),
 * $r$ is the radius of the sphere.

The haversine formula allows the haversine of $&theta;$ to be computed directly from the latitude (represented by $φ$) and longitude (represented by $λ$) of the two points:

\operatorname{hav}\theta = \operatorname{hav}\left(\Delta \varphi \right) + \cos\left(\varphi_1\right)\cos\left(\varphi_2\right)\operatorname{hav}\left(\Delta \lambda \right) $$

where
 * $φ_{1}$, $φ_{2}$ are the latitude of point 1 and latitude of point 2,
 * $λ_{1}$, $λ_{2}$ are the longitude of point 1 and longitude of point 2,
 * $$\Delta \varphi = \varphi_2 - \varphi_1$$, $$\Delta \lambda = \lambda_2 - \lambda_1$$.

Finally, the haversine function $hav(&theta;)$, applied above to both the central angle $&theta;$ and the differences in latitude and longitude, is
 * $$\operatorname{hav}\theta= \sin^2\left(\frac{\theta}{2}\right) = \frac{1 - \cos(\theta)}{2}$$

The haversine function computes half a versine of the angle $θ$, or the squares of half chord of the angle on a unit circle (sphere).

To solve for the distance $d$, apply the archaversine (inverse haversine) to $hav(&theta;)$ or use the arcsine (inverse sine) function:
 * $$d = r\operatorname{archav}(\operatorname{hav}\theta) = 2r\arcsin\left(\sqrt{\operatorname{hav}\theta}\right)$$

or more explicitly:
 * $$\begin{align}

d &= 2r \arcsin\left(\sqrt{\operatorname{hav}(\Delta \varphi ) + ( 1 - \operatorname{hav}(\Delta \varphi) - \operatorname{hav}(2 \varphi_\text{m} ))\cdot\operatorname{hav}(\Delta \lambda)}\right) \\ &= 2r \arcsin\left(\sqrt{\sin^2\left(\frac{\Delta \varphi }{2}\right) + \left(1- \sin^2\left(\frac{\Delta \varphi }{2}\right) - \sin^2\left(\varphi_\text{m}\right)\right) \cdot \sin^2\left(\frac{\Delta \lambda}{2}\right)}\right) \\ &= 2r \arcsin\left(\sqrt{\sin^2\left(\frac{\Delta \varphi }{2}\right) + \cos \varphi_1 \cdot \cos \varphi_2 \cdot \sin^2\left(\frac{\Delta \lambda}{2}\right)}\right) \\ &= 2r \arcsin\left(\sqrt{\sin^2\left(\frac{\Delta \varphi }{2}\right) \cdot \cos^2\left(\frac{\Delta \lambda}{2}\right) + \cos^2\left(\varphi_\text{m}\right) \cdot \sin^2\left(\frac{\Delta \lambda}{2}\right)}\right) \\ &= 2r \arcsin\left(\sqrt{\frac{1 - \cos\left(\Delta \varphi \right) + \cos \varphi_1 \cdot \cos \varphi_2 \cdot \left(1 - \cos\left(\Delta \lambda\right)\right)}{2}}\right) \end{align}$$ where $$\varphi_\text{m} = \frac{\varphi_2 + \varphi_1}{2}$$.

When using these formulae, one must ensure that $h = hav(&theta;)$ does not exceed 1 due to a floating point error ($d$ is real only for $0 ≤ h ≤ 1$). $h$ only approaches 1 for antipodal points (on opposite sides of the sphere)—in this region, relatively large numerical errors tend to arise in the formula when finite precision is used. Because $d$ is then large (approaching $πR$, half the circumference) a small error is often not a major concern in this unusual case (although there are other great-circle distance formulas that avoid this problem). (The formula above is sometimes written in terms of the arctangent function, but this suffers from similar numerical problems near $h = 1$.)

As described below, a similar formula can be written using cosines (sometimes called the spherical law of cosines, not to be confused with the law of cosines for plane geometry) instead of haversines, but if the two points are close together (e.g. a kilometer apart, on the Earth) one might end up with $cos(d⁄R) = 0.99999999$, leading to an inaccurate answer. Since the haversine formula uses sines, it avoids that problem.

Either formula is only an approximation when applied to the Earth, which is not a perfect sphere: the "Earth radius" $R$ varies from 6356.752 km at the poles to 6378.137 km at the equator. More importantly, the radius of curvature of a north-south line on the earth's surface is 1% greater at the poles (≈6399.594 km) than at the equator (≈6335.439 km)&mdash;so the haversine formula and law of cosines cannot be guaranteed correct to better than 0.5%. More accurate methods that consider the Earth's ellipticity are given by Vincenty's formulae and the other formulas in the geographical distance article.

The law of haversines
Given a unit sphere, a "triangle" on the surface of the sphere is defined by the great circles connecting three points $u$, $v$, and $w$ on the sphere. If the lengths of these three sides are $a$ (from $u$ to $v$), $b$ (from $u$ to $w$), and $c$ (from $v$ to $w$), and the angle of the corner opposite $c$ is $C$, then the law of haversines states:
 * $$\operatorname{hav}(c) = \operatorname{hav}(a - b) + \sin(a)\sin(b)\operatorname{hav}(C).$$

Since this is a unit sphere, the lengths $a$, $b$, and $c$ are simply equal to the angles (in radians) subtended by those sides from the center of the sphere (for a non-unit sphere, each of these arc lengths is equal to its central angle multiplied by the radius $R$ of the sphere).

In order to obtain the haversine formula of the previous section from this law, one simply considers the special case where $u$ is the north pole, while $v$ and $w$ are the two points whose separation $d$ is to be determined. In that case, $a$ and $b$ are $π⁄2 − φ_{1,2}$ (that is, the, co-latitudes), $C$ is the longitude separation $λ_{2} − λ_{1}$, and $c$ is the desired $d⁄R$. Noting that $sin(π⁄2 − φ) = cos(φ)$, the haversine formula immediately follows.

To derive the law of haversines, one starts with the spherical law of cosines:


 * $$\cos(c) = \cos(a)\cos(b) + \sin(a)\sin(b)\cos(C). \,$$

As mentioned above, this formula is an ill-conditioned way of solving for $c$ when $c$ is small. Instead, we substitute the identity that $cos(θ) = 1 − 2 hav(θ)$, and also employ the addition identity $cos(a − b) = cos(a) cos(b) + sin(a) sin(b)$, to obtain the law of haversines, above.

Proof
One can prove the formula:

\operatorname{hav}\left(\theta\right) = \operatorname{hav}\left(\Delta \varphi \right) + \cos\left(\varphi_1\right)\cos\left(\varphi_2\right)\operatorname{hav}\left(\Delta \lambda\right) $$

by transforming the points given by their latitude and longitude into cartesian coordinates, then taking their dot product.

Consider two points $$\bf p_1,p_2$$ on the unit sphere, given by their latitude $$\varphi$$ and longitude $$\lambda$$:


 * $$\begin{align}

{\bf p_2} &= (\lambda_2, \varphi_2) \\ {\bf p_1} &= (\lambda_1, \varphi_1) \end{align}$$

These representations are very similar to spherical coordinates, however latitude is measured as angle from the equator and not the north pole. These points have the following representations in cartesian coordinates:


 * $$\begin{align}

{\bf p_2} &= (\cos(\lambda_2)\cos(\varphi_2), \;\sin(\lambda_2)\cos(\varphi_2), \;\sin(\varphi_2)) \\ {\bf p_1} &= (\cos(\lambda_1)\cos(\varphi_1), \;\sin(\lambda_1)\cos(\varphi_1), \;\sin(\varphi_1)) \end{align}$$

From here we could directly attempt to calculate the dot product and proceed, however the formulas become significantly simpler when we consider the following fact: the distance between the two points will not change if we rotate the sphere along the z-axis. This will in effect add a constant to $$\lambda_1, \lambda_2$$. Note that similar considerations do not apply to transforming the latitudes - adding a constant to the latitudes may change the distance between the points. By choosing our constant to be $$-\lambda_1$$, and setting $$\lambda' = \Delta \lambda$$, our new points become:


 * $$\begin{align}

{\bf p_2'}	&= (\cos(\lambda')\cos(\varphi_2), \;\sin(\lambda')\cos(\varphi_2), \;\sin(\varphi_2)) \\ {\bf p_1'}	&= (\cos(0)\cos(\varphi_1), \;\sin(0)\cos(\varphi_1), \;\sin(\varphi_1)) \\ &= (\cos(\varphi_1), \;0, \;\sin(\varphi_1)) \end{align}$$

With $$\theta$$ denoting the angle between $${\bf p_1}$$ and $${\bf p_2}$$, we now have that:


 * $$\begin{align}

\cos(\theta) &= \langle{\bf p_1},{\bf p_2}\rangle = \langle{\bf p_1'},{\bf p_2'}\rangle = \cos(\lambda')\cos(\varphi_1)\cos(\varphi_2) + \sin(\varphi_1)\sin(\varphi_2) \\ &= \sin(\varphi_2)\sin(\varphi_1) + \cos(\varphi_2)\cos(\varphi_1) - \cos(\varphi_2)\cos(\varphi_1) + \cos(\lambda')\cos(\varphi_2)\cos(\varphi_1) \\ &= \cos(\Delta \varphi) + \cos(\varphi_2)\cos(\varphi_1)(-1 + \cos(\lambda')) \Rightarrow \\ \operatorname{hav}\left(\theta\right) &= \operatorname{hav}\left(\Delta \varphi \right) + \cos(\varphi_2)\cos(\varphi_1)\operatorname{hav}\left(\lambda' \right) \end{align} $$