Illustration for Haversine Formula Explained with Examples - DigiToolkit

Haversine Formula Explained with Examples

The haversine formula explained term by term with worked Indian examples like Mumbai to Bengaluru. Understand great-circle distance the easy way.

Quick Answer: The haversine formula is d = 2R · arcsin(√(sin²(Δlat/2) + cos(lat1)·cos(lat2)·sin²(Δlon/2))), where R is Earth’s radius (6,371 km) and all angles are in radians. It converts two latitude/longitude pairs into the great-circle distance between them, which is the shortest path over the Earth’s surface.

Key takeaways:

  • The formula is built on the “haversine” function, hav(θ) = sin²(θ/2).
  • It returns a central angle first, which you then multiply by Earth’s radius.
  • The cosine terms weight the longitude difference by how far you are from the equator.
  • For India (8–37° N) the cosine factor stays high, so longitude differences carry real weight.
  • The formula stays numerically stable even for very short distances within a single Indian city.

The haversine formula is one of the most elegant results in navigation mathematics, and once you understand what each piece does, it stops looking like a wall of symbols. In this guide we break the formula down term by term and walk through fully worked Indian examples, so whether you are a developer building a delivery app in Hyderabad or a student revising spherical trigonometry, you will leave knowing exactly why it works.

Expert insight: The word “haversine” is short for “half the versed sine.” Navigators favoured it for centuries because the sin²(θ/2) form always stays positive, avoiding the sign confusion that plagued older distance methods.

The Formula and What Each Term Means

Written in full, the haversine distance is:

d = 2R · arcsin( √( sin²(Δlat/2) + cos(lat1) · cos(lat2) · sin²(Δlon/2) ) )

Each part has a clear job. The term sin²(Δlat/2) captures the north–south separation between your two points. The term sin²(Δlon/2) captures the east–west separation. The two cosine terms, cos(lat1) and cos(lat2), shrink the east–west contribution as you move away from the equator, because lines of longitude crowd together near the poles. Finally, the arcsine converts the combined value into a central angle, and multiplying by R turns that angle into a real distance on the ground.

Why the Cosine Terms Matter in India

India sits entirely in the northern hemisphere, roughly between 8° N (Kanyakumari) and 37° N (the far north of Kashmir). At these latitudes the cosine factor ranges from about 0.99 down to 0.80, meaning a degree of longitude represents between roughly 110 km and 89 km on the ground. This is why two cities at the same latitude but different longitudes — say Ahmedabad and Kolkata — are closer together than their raw longitude gap alone would suggest. The cosine correction is doing exactly this adjustment automatically.

Worked Example 1: Mumbai to Bengaluru

Take Mumbai (19.0760° N, 72.8777° E) and Bengaluru (12.9716° N, 77.5946° E).

  1. Convert to radians and find the differences: Δlat = −0.10654 rad, Δlon = 0.08233 rad.
  2. sin²(Δlat/2) = sin²(−0.05327) = 0.002835.
  3. cos(19.0760°) × cos(12.9716°) = 0.9451 × 0.9745 = 0.9210.
  4. sin²(Δlon/2) = sin²(0.04116) = 0.001693.
  5. a = 0.002835 + (0.9210 × 0.001693) = 0.004395.
  6. d = 2 × 6,371 × arcsin(√0.004395) = 845 km.

The straight-line distance is about 845 km, while the NH-48 road distance is closer to 980 km.

Worked Example 2: Chennai to Hyderabad

For Chennai (13.0827° N, 80.2707° E) and Hyderabad (17.3850° N, 78.4867° E), the same procedure gives a central angle that resolves to roughly 515 km of great-circle distance. The Chennai–Hyderabad drive is about 625 km, so the circuity ratio here is around 1.21, typical of peninsular Indian highways.

The Haversine Function in a Table

Symbol Meaning Unit
lat1, lat2 Latitudes of the two points radians
lon1, lon2 Longitudes of the two points radians
Δlat, Δlon Differences (point 2 minus point 1) radians
R Earth’s mean radius 6,371 km
d Great-circle distance km

Benefits of Understanding the Formula

Grasping the formula rather than treating it as a black box pays off in several ways. Developers who understand the cosine weighting can debug distance bugs quickly, such as spotting when latitude and longitude have been swapped in a database. Data scientists can vectorise the calculation confidently across millions of rows for a pan-India logistics dataset. Students who understand the derivation can adapt it to related problems, such as bearing calculations or destination-point projection. And anyone quoting distances gains the confidence to explain to a manager why the app’s figure differs from the odometer reading.

Challenges and Limitations

The formula’s main limitation is its spherical-Earth assumption, which introduces an error of up to about 0.5%. It also cannot capture elevation, so a route through the Western Ghats or Himalayas will be understated. Additionally, the arcsine version can lose precision for nearly antipodal points, which is why production code often uses the atan2 variant instead. None of these limits matter for typical Indian city-scale work, but they are worth knowing before you rely on the number for surveying or aviation planning.

Common Mistakes to Avoid

  • Applying cosine to the latitude difference. The cosine terms use the individual latitudes, not Δlat; mixing this up is a frequent coding bug.
  • Leaving angles in degrees. Radians are mandatory throughout; a missing conversion is the top cause of wrong answers.
  • Using arcsin outside its domain. Floating-point rounding can push the input just above 1; clamp it to avoid a math error.
  • Forgetting the factor of 2. The arcsine form needs the leading 2; dropping it halves your distance.
  • Assuming symmetry breaks. Haversine is symmetric, so A to B equals B to A; if your code disagrees, you have a bug elsewhere.
  • Hard-coding the equatorial radius. Use the mean radius 6,371 km unless you have a specific reason to model the ellipsoid.

Best Practices and Expert Recommendations

  • Prefer the atan2 form in code. It is more stable than arcsin across the full range of distances.
  • Store coordinates at full precision. Keep at least six decimal places to preserve sub-metre accuracy.
  • Unit-test with known Indian routes. Verify your implementation against Delhi–Mumbai (about 1,148 km) before trusting it.
  • Document your radius choice. State clearly whether outputs are in kilometres or miles to avoid downstream confusion.
  • Vectorise for large datasets. Use array operations rather than per-row loops when processing many Indian pin-code centroids.
  • Switch to Vincenty for survey work. When one-metre accuracy is not enough, move to the ellipsoidal model.

Frequently Asked Questions

What does the word haversine mean?
Haversine is short for “half the versed sine” and is defined as hav(θ) = sin²(θ/2). It was used historically in navigation tables because it keeps values positive and simplifies great-circle distance calculations.

Why are there two cosine terms in the formula?
The cosine of each latitude scales the east–west (longitude) component of the distance. Near the equator this factor is close to 1, but it shrinks toward the poles because lines of longitude converge, so the formula automatically corrects for your position on the globe.

Is the arcsine or atan2 version better?
Mathematically they give the same answer, but the atan2 version is numerically more stable for points that are nearly opposite each other on the globe. Most production libraries use atan2 for this reason.

Does the haversine formula work for short distances in one city?
Yes. Unlike some older formulas, haversine stays accurate for very small separations, so it works reliably whether you are measuring two metro stations in Delhi or two cities across India.

Can I use the formula to find direction as well as distance?
The haversine formula itself returns only distance. To find the initial bearing between two points you use a separate but related formula based on atan2 of the longitude and latitude differences.

Leave a Reply

Your email address will not be published. Required fields are marked *