The haversine formula calculates the geographic distance between two points on Earth from their latitudes and longitudes, modeling Earth with a mean spherical radius. The result is also called great-circle distance. section 4.3.3
The calculation proceeds through these quantities:
- Compute a = sin²(Δlat/2) + cos(lat1) × cos(lat2) × sin²(Δlon/2). section 4.3.3
- Compute C = 2 × atan2(√a, √(1 − a)). section 4.3.3
- Multiply by the mean Earth radius: d = R × C, using 6,371 km or 3,691 miles. section 4.3.3
Los Angeles to Madrid
A Python implementation passes Los Angeles (34.052235, −118.243683) and Madrid (40.416775, −3.703790) to haversine(), then prints the resulting distance.
