Primary sources · 4
- [1] Inman (1835) — James Inman, navigation textbook that first published a haversine table — the term predates the modern formula attribution · Navigation and Nautical Astronomy for the Use of British Seamen · 1835 https://archive.org/details/bub_gb_pi04AAAAQAAJ
- [2] Sinnott (1984) — Modern reference for the Haversine numerical-stability rationale, Sky & Telescope · Sky and Telescope vol. 68, p. 158 · August 1984 https://archive.org/details/sky-and-telescope-1984-08
- [3] Veness — Movable Type Scripts — Widely-cited modern JavaScript reference implementation and numerical analysis · movable-type.co.uk/scripts/latlong.html · Continuously maintained https://www.movable-type.co.uk/scripts/latlong.html
- [4] NGA.STND.0036_1.0.0_WGS84 — Source of the mean spherical radius (6,371,008.8 m) used here · NGA Standard · 2014 https://earth-info.nga.mil/index.php?dir=wgs84&action=wgs84
Haversine is what AirMilesCalc uses when Vincenty's iteration cannot converge — the rare near-antipodal point pair. It treats Earth as a perfect sphere of radius 6,371 km, which is wrong by up to 0.5 % but always returns a finite, sensible answer.
The formula in one line
The Haversine formula is a = sin²(Δφ/2) + cos φ₁ cos φ₂ sin²(Δλ/2), followed by c = 2 · atan2(√a, √(1−a)) and finally d = R · c. The "haversine" half-versed-sine functions absorb the 1/2 factor and keep all intermediate values away from numerical-cancellation regions. Every term is well-behaved across the full input domain, including the antipodes.
A worked example: London Heathrow to John F Kennedy
Take LHR at φ₁ = 51.4706°, λ₁ = -0.461941° and JFK at φ₂ = 40.6398°, λ₂ = -73.7789°. Convert all four to radians: φ₁ = 0.8985, λ₁ = -0.00806, φ₂ = 0.7094, λ₂ = -1.2881. Compute Δφ = -0.1891 rad and Δλ = -1.2800 rad.
- 1Compute the haversine of the latitude difference
sin²(Δφ/2) = sin²(-0.09454) = (-0.09440)² = 0.008912.
- 2Compute the haversine of the longitude difference, weighted by cosines of the two latitudes
cos φ₁ · cos φ₂ · sin²(Δλ/2) = 0.6234 · 0.7591 · sin²(-0.6400) = 0.4732 · 0.3567 = 0.1688.
- 3Sum to get a
a = 0.008912 + 0.1688 = 0.1777.
- 4Compute the central angle c
c = 2 · atan2(√0.1777, √(1 - 0.1777)) = 2 · atan2(0.4215, 0.9069) = 2 · 0.4356 = 0.8712 rad.
- 5Multiply by R to get distance
d = 6,371 km · 0.8712 = 5,550 km.
The Vincenty + WGS-84 answer for the same pair is 5,555 km — the Haversine result is 5 km low, about 0.09 % under. That matches the "under 0.5 % worst case" envelope on a transatlantic mid-latitude route.
Implementation in code
The formula translates directly to about a dozen lines in any language. Both implementations below use the conventional sign convention (north / east positive) and return distance in kilometres.
Both implementations match to 9 significant figures on standard double-precision floats. The Python version is what AirMilesCalc uses internally for the fallback path when Vincenty does not converge; the JavaScript form is what most front-end "compute distance" libraries ship.
Why R = 6,371 km
WGS-84 is an ellipsoid, not a sphere, so Haversine has to pick one representative radius. Three candidates exist: the equatorial radius (6,378.137 km), the polar radius (6,356.752 km), and the mean. The arithmetic mean (2a + b)/3 = 6,371.009 km — typically rounded to 6,371 km — minimises worst-case distance error to about 0.5 %, and is the navigation-textbook standard.
| Mean | Definition | Value (km) | Optimises |
|---|---|---|---|
| Arithmetic | (2a + b)/3 | 6,371.009 | Generic worst-case distance error |
| Volumetric | (a²b)^(1/3) | 6,371.001 | Volume preservation |
| Authalic | Surface-area-equivalent sphere | 6,371.007 | Surface-area preservation |
| Rectifying | Meridian-length equivalent | 6,367.45 | Latitude-distance preservation |
The four means agree to within ~4 km. For aviation distances even the worst pick is comfortably under the ±0.5 % Haversine error envelope, so the choice is conventional rather than precision-critical. AirMilesCalc uses 6,371 km, the arithmetic mean.
When Vincenty hands off to Haversine
Vincenty's inverse iteration may not converge when the two points are within about half a degree of being antipodal — the original 1975 paper flagged this and we observe it empirically. AirMilesCalc treats the 100-iteration cap as a non-convergence signal and re-runs the distance with Haversine, which always returns a value.
The chart shows roughly 28 parts-per-million of typical request volume hits the antipodal fallback path. We keep the fallback because losing 28 ppm of queries to a NaN response is unacceptable — accurate-within-0.5% beats no-answer.
Accuracy bound and worst-case routes
The systematic Haversine error against Vincenty grows with route length and with departure-latitude away from the equator. A polar route like Helsinki → Tokyo at 60°N origin runs about 40 km long under Haversine; an equator-crossing route like Singapore → Lima runs about 25 km long. For end-user reference this is invisible, but for fuel planning a 40 km overstatement is roughly 600 kg of jet fuel.