The Haversine Formula

Compute the great-circle distance between two points on a spherical Earth using the haversine function and the Haversine formula. Includes sign conventions for latitude/longitude and unit handling for kilometers and nautical miles.

Step 1 of 157%

Tutorial

The Haversine Function

To compute great-circle distances on a sphere cleanly, we use the haversine function, defined for any angle θ\theta as

hav(θ)=sin2 ⁣(θ2)=1cosθ2.\operatorname{hav}(\theta) = \sin^2\!\left(\dfrac{\theta}{2}\right) = \dfrac{1-\cos\theta}{2}.

The two forms are equivalent via the half-angle identity. The haversine is non-negative, equals 00 at θ=0,\theta = 0, and grows to 11 at θ=180°.\theta = 180°. It is also an even function: hav(θ)=hav(θ).\operatorname{hav}(-\theta) = \operatorname{hav}(\theta).

For example,

hav(90°)=sin2(45°)=(22)2=12.\operatorname{hav}(90°) = \sin^2(45°) = \left(\dfrac{\sqrt{2}}{2}\right)^2 = \dfrac{1}{2}.

The haversine is the building block of the Haversine formula for great-circle distance. It is preferred over the law-of-cosines formula because it remains numerically stable for points that are close together on the sphere.

navigate · Enter open · Esc close · ⌘K/Ctrl K toggle