Haversine Formula
Calculate the great-circle distance between two points on Earth from their latitude and longitude coordinates.
The Formula
c = 2 × atan2(√a, √(1 − a))
d = R × c
The Haversine formula calculates the shortest distance between two points on a sphere (great-circle distance). It is widely used in navigation, aviation, and mapping applications.
The name comes from the mathematical function "half versed sine" (haversine), defined as hav(θ) = sin²(θ/2).
Variables
| Symbol | Meaning |
|---|---|
| lat₁, lon₁ | Latitude and longitude of point 1 (in radians) |
| lat₂, lon₂ | Latitude and longitude of point 2 (in radians) |
| Δlat | lat₂ − lat₁ |
| Δlon | lon₂ − lon₁ |
| R | Earth's radius: 6,371 km (3,959 miles) |
| d | Distance between the two points |
Example 1 — New York to London
New York (40.7128°N, 74.0060°W) to London (51.5074°N, 0.1278°W). Find the distance.
Convert to radians: lat₁ = 0.7102, lon₁ = −1.2918, lat₂ = 0.8988, lon₂ = −0.00223
Δlat = 0.1886, Δlon = 1.2896
a = sin²(0.0943) + cos(0.7102) × cos(0.8988) × sin²(0.6448)
a = 0.00889 + 0.7597 × 0.6272 × 0.3625 = 0.1816
c = 2 × atan2(√0.1816, √0.8184) = 2 × 0.4438 = 0.8876
d = 6,371 × 0.8876
d ≈ 5,653 km (3,513 miles)
Example 2 — Sydney to Tokyo
Sydney (33.8688°S, 151.2093°E) to Tokyo (35.6762°N, 139.6503°E). Find the distance.
Convert to radians: lat₁ = −0.5911, lon₁ = 2.6389, lat₂ = 0.6226, lon₂ = 2.4372
Δlat = 1.2137, Δlon = −0.2017
a = sin²(0.6069) + cos(−0.5911) × cos(0.6226) × sin²(−0.1009)
a = 0.3255 + 0.8305 × 0.8132 × 0.01017 = 0.3324
c = 2 × atan2(√0.3324, √0.6676) = 1.2298
d = 6,371 × 1.2298
d ≈ 7,835 km (4,868 miles)
When to Use It
- GPS navigation and route planning
- Finding the nearest store, restaurant, or point of interest
- Aviation flight path calculations
- Shipping and maritime navigation
- Geofencing and location-based services
Important Notes
- All angles must be converted to radians: radians = degrees × π / 180
- The formula assumes a perfect sphere, so results have about 0.3% error compared to the actual ellipsoidal Earth
- For higher accuracy, use the Vincenty formula (which accounts for Earth's flattening)