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,[1] but Florian Cajori credits an earlier use by José de Mendoza y Ríos in 1801.

[4][5] These names follow from the fact that they are customarily written in terms of the haversine function, given by hav θ = sin2(⁠θ/2⁠).

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.

[6][7][8] These days, the haversine form is also convenient in that it has no coefficient in front of the sin2 function.

Let the central angle θ between any two points on a sphere be: where The haversine formula allows the haversine of θ to be computed directly from the latitude (represented by φ) and longitude (represented by λ) of the two points: where Finally, the haversine function hav(θ), applied above to both the central angle θ and the differences in latitude and longitude, is The haversine function computes half a versine of the angle θ, or the squares of half chord of the angle on a unit circle (sphere).

When using these formulae, one must ensure that h = hav(θ) 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.)

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)—so the haversine formula and law of cosines cannot be guaranteed correct to better than 0.5%.

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.

One can prove the formula: by transforming the points given by their latitude and longitude into cartesian coordinates, then taking their dot product.

: 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: 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.

A diagram illustrating great-circle distance (drawn in red) between two points on a sphere, P and Q. Two antipodal points , u and v are also shown.
Spherical triangle solved by the law of haversines