Kolton Ndoj

The haversine formula: distance on a ball

When a map app or fitness tracker says how far apart two places are, or why flights from London to Los Angeles often pass over Greenland

What problem does it solve?

What's the shortest distance between two points on a sphere, given their latitudes and longitudes?

The big idea

On a ball, the shortest path is a 'great circle', the circle whose centre is the centre of the Earth, like a rubber band snapped tight around a globe. Flat maps distort this, which is why straight-line routes look curved on them. The haversine formula finds the angle between two points as seen from Earth's centre, and the distance is that angle times the radius. It uses sin²(θ/2) on purpose: that form stays accurate even for points just metres apart, where other formulas lose precision.

How it works, step by step

  1. 1. Convert both latitudes φ and longitudes λ to radians.
  2. 2. Compute a = sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2).
  3. 3. The central angle is 2·arcsin(√a).
  4. 4. Multiply by Earth's mean radius, R ≈ 6371 km.

The key formula

d=2Rarcsin⁡sin⁡2 ⁣Δφ2+cos⁡φ1cos⁡φ2sin⁡2 ⁣Δλ2d = 2R\arcsin\sqrt{\sin^2\!\frac{\Delta\varphi}{2}+\cos\varphi_1\cos\varphi_2\sin^2\!\frac{\Delta\lambda}{2}}
φ1,φ2\varphi_1,\varphi_2
the latitudes of the two points (north–south angle)
Δλ\Delta\lambda
the difference in longitude (east–west angle)
RR
Earth's mean radius, about 6371 km
sin⁡2(θ/2)\sin^2(\theta/2)
the 'haversine' of θ: half of 1 − cos θ

How fast is it?

A few trig functions, instant. It treats Earth as a perfect sphere, but Earth is slightly squashed (its equatorial radius is about 21 km longer than its polar radius), so errors can reach about 0.5%. Surveyors use ellipsoid formulas such as Vincenty's or Karney's for millimetre accuracy.

Graph for The haversine formula: distance on a ball 06.28210000
Great-circle distance in km between two points at the same latitude a, separated by t radians of longitude. Walking along the latitude line instead would take 6371·cos(a)·t km, always at least as long. The great circle cuts toward the pole. (drawn with the default slider values — move the sliders live)

Try it yourself

Fun fact

The haversine was once a real named function with printed tables in navigation books, because sailors could compute it accurately by hand. 'Haversine' just means half (ha-) of the versed sine (versine), 1 − cos θ.

Related

Open it in the Math Lab →

Maps · Intermediate