The Math Behind the Haversine Formula
How we calculate great-circle distances between two points on a spherical Earth.
Distance on a Sphere
Calculating the distance between two points on a flat plane is straightforward: you use the Pythagorean theorem. However, when you’re calculating the distance between London and New York, the “flat” approach fails because the Earth is an oblate spheroid.
The Problem with Flat Maps
If you try to use a standard $x, y$ coordinate system on a globe, your results will be significantly off. This is because the distance between lines of longitude shrinks as you move toward the poles.
Did you know? A straight line on a flat map (Mercator projection) is rarely the shortest path between two points on Earth. Pilots fly “Great Circle” routes which look curved on a map but are actually the most direct path.
Visualizing the Great Circle
A Great Circle is the largest possible circle that can be drawn on a sphere. Its center is identical to the center of the sphere itself. The shortest distance between any two points on a sphere is the length of the arc of the great circle that connects them.
Shortest path (Arc) vs. Straight line through the Earth
The Haversine Formula
The term “Haversine” comes from half-versed-sine. In the era before calculators, this formula was vital for sailors because it allowed them to calculate distances using logarithmic tables without needing to handle high-precision division or square roots.
The Equation Breakdown
To calculate the distance $d$ between two points, we follow these three steps:
-
Calculate the square of half the chord length ($a$):
a = \sin^2(\Delta\phi/2) + \cos \phi_1 \cdot \cos \phi_2 \cdot \sin^2(\Delta\lambda/2) -
Calculate the angular distance in radians ($c$):
c = 2 \cdot \text{atan2}( \sqrt{a}, \sqrt{1-a} ) -
Multiply by Earth’s radius ($R$):
d = R \cdot c
Variable Definitions
| Variable | Definition | Typical Value |
|---|---|---|
| $\phi_1, \phi_2$ | Latitude of Point 1 and 2 | -90° to +90° |
| $\lambda_1, \lambda_2$ | Longitude of Point 1 and 2 | -180° to +180° |
| $\Delta\phi$ | Difference in Latitude | $\phi_2 - \phi_1$ |
| $\Delta\lambda$ | Difference in Longitude | $\lambda_2 - \lambda_1$ |
| $R$ | Mean Earth Radius | 6,371 km (3,959 mi) |
Comparison: Flat vs. Spherical
| Distance Path | Formula | Accuracy |
|---|---|---|
| Flat Plane | Pythagorean Theorem | Low (only for < 10km) |
| Spherical | Haversine Formula | High (99.5% accuracy) |
| Ellipsoid | Vincenty’s Formulae | Very High (99.9% accuracy) |
Put it into Practice
Calculating this by hand is prone to errors, especially when converting degrees to radians (a required step). Our Haversine Distance Calculator automates this entire process. Simply plug in your coordinates, and we’ll handle the trigonometric heavy lifting instantly.
Whether you’re building a logistics app or just curious about how far you are from the equator, the Haversine formula remains the industry standard for fast, reliable spherical distance calculations.