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. Convert both latitudes φ and longitudes λ to radians.
- 2. Compute a = sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2).
- 3. The central angle is 2·arcsin(√a).
- 4. Multiply by Earth's mean radius, R ≈ 6371 km.
The key formula
- the latitudes of the two points (north–south angle)
- the difference in longitude (east–west angle)
- Earth's mean radius, about 6371 km
- 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.
Try it yourself
- Two points on the equator, 180° apart: what does the formula give? (Half of Earth's circumference, about 20,015 km.)
- Stretch a string on a globe between your city and a far-away one. Which countries does it pass over?
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 θ.