The haversine formula: distance between two coordinates
6 min read · updated 30 November 2026
The haversine formula gives the great-circle distance between two points from their latitudes and longitudes, treating the Earth as a sphere. It is quick to compute and accurate to about half a percent, which is why it shows up in so many apps and database queries.
The formula
For two points with latitudes φ₁, φ₂ and longitudes λ₁, λ₂ in radians, on a sphere of radius R:
a = sin²(Δφ / 2) + cos φ₁ · cos φ₂ · sin²(Δλ / 2)
c = 2 · atan2(√a, √(1 − a))
d = R · cΔφ and Δλ are the differences in latitude and longitude, and c is the angle between the two points as seen from the centre of the Earth. For R, use the mean Earth radius, 6,371.0088 km (often rounded to 6,371 km), or 3,958.8 miles.
"Haversine" is an old trigonometric function, hav(θ) = sin²(θ/2). Navigators used printed tables of it because it keeps the arithmetic accurate for small distances.
JavaScript
function haversineKm(lat1, lon1, lat2, lon2) {
const R = 6371.0088; // mean Earth radius in km
const rad = (d) => (d * Math.PI) / 180;
const dPhi = rad(lat2 - lat1);
const dLambda = rad(lon2 - lon1);
const a = Math.sin(dPhi / 2) ** 2 +
Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLambda / 2) ** 2;
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
haversineKm(51.5085, -0.1257, 48.8534, 2.3488); // London to Paris: 343.8Python
from math import radians, sin, cos, atan2, sqrt
def haversine_km(lat1, lon1, lat2, lon2):
R = 6371.0088
dphi = radians(lat2 - lat1)
dlmb = radians(lon2 - lon1)
a = sin(dphi / 2) ** 2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlmb / 2) ** 2
return 2 * R * atan2(sqrt(a), sqrt(1 - a))How accurate is it?
The Earth is not a sphere but a slightly flattened ellipsoid, 21 km narrower pole to pole than across the equator. The haversine formula ignores that, so its answers are off by up to about 0.5%. Here is how it compares with the exact distance on the WGS84 ellipsoid:
| Route | Exact (WGS84) | Haversine | Error |
|---|---|---|---|
| London – Paris | 344.1 km | 343.8 km | −0.11% |
| New York – Los Angeles | 3,944.4 km | 3,935.7 km | −0.22% |
| Sydney – Santiago | 11,369.6 km | 11,347.3 km | −0.20% |
| Oslo – Cape Town | 10,423.8 km | 10,458.2 km | +0.33% |
| Quito – Singapore | 19,741.6 km | 19,725.9 km | −0.08% |
Haversine with R = 6,371.0088 km. City-centre coordinates.
More accurate methods
- Vincenty's formulae (1975) work on the ellipsoid and are accurate to about half a millimetre, but they can fail to converge for points on nearly opposite sides of the Earth.
- Karney's algorithm (2013), implemented in GeographicLib, is accurate to about 15 nanometres and always converges. This is what MapDist uses for every distance, circle and area.
// npm install geographiclib-geodesic
import { Geodesic } from 'geographiclib-geodesic';
const r = Geodesic.WGS84.Inverse(51.5085, -0.1257, 48.8534, 2.3488);
console.log(r.s12 / 1000); // 344.1 km, exact on the ellipsoid
console.log(r.azi1); // initial bearing in degreesTo check your own code, paste the same two coordinates into the distance tool: it shows the exact ellipsoidal distance.