Haversine vs Vincenty: Choosing the Right Distance Formula

Geographic Math

The Distance Formula Problem

Calculating the distance between two GPS coordinates sounds simple, but the Earth is not flat — and not a perfect sphere either. Two formulas dominate practical implementations: Haversine and Vincenty.

Haversine Formula

Haversine assumes the Earth is a perfect sphere of radius 6,371 km. It's fast, simple, and accurate enough for most applications.

Error: up to 0.5% (about 5 km over a 1,000 km distance)

import math

def haversine(lat1, lng1, lat2, lng2):
    R = 6371  # km
    dlat = math.radians(lat2 - lat1)
    dlng = math.radians(lng2 - lng1)
    a = math.sin(dlat/2)**2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlng/2)**2
    return R * 2 * math.asin(math.sqrt(a))

Vincenty Formula

Vincenty uses an oblate spheroid model (WGS-84), the same reference used by GPS. It's significantly more accurate but also more computationally expensive.

Error: up to 0.5 mm (essentially exact)

API Usage

curl -X POST https://api.toolkitapi.io/v1/geo/distance \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": { "lat": 51.5074, "lng": -0.1278 },
    "to":   { "lat": 48.8566, "lng":  2.3522 },
    "formula": "vincenty"
  }'
{
  "distance_km": 340.57,
  "distance_miles": 211.60,
  "formula": "vincenty"
}

When to Choose Each

Scenario Use
Store locator, delivery radius Haversine
Aviation, maritime navigation Vincenty
Geofence boundary calculations Vincenty
Large-scale proximity queries Haversine (speed matters)

For most web applications, Haversine is the right default. Only switch to Vincenty when sub-kilometre accuracy matters.

Try it out

Browse Tools →

More from the Blog