The Problem with Flat Maps
If you place a ruler between two points on a paper map, you can measure the distance in centimetres and multiply by the scale to get an answer. This works well enough for very short distances, but it falls apart over longer ones. The reason is simple: the Earth is not flat. It is roughly spherical, which means the surface curves away in every direction. A straight line drawn on a flat map does not represent the shortest path between two points on a curved surface.
To calculate real world distances accurately, we need a formula that accounts for the curvature of the Earth. That is where the Haversine formula comes in.
What Is the Haversine Formula?
The Haversine formula is a mathematical equation used to calculate the great circle distance between two points on the surface of a sphere. A great circle is the largest circle that can be drawn on a sphere's surface, and the shortest path between any two points on a sphere always follows a great circle arc. Think of it as the equivalent of a straight line, but on a curved surface.
The formula was first described in the early 19th century and takes its name from the haversine function, which is half of a versed sine. It was originally popular with navigators because it could be computed using logarithm tables, making it practical for maritime and aviation use long before electronic calculators existed.
How It Works Step by Step
The formula takes four inputs: the latitude and longitude of the first point, and the latitude and longitude of the second point. All values must be converted from degrees to radians before the calculation begins.
First, you find the difference in latitude and the difference in longitude between the two points. These differences are each halved and then passed through the sine function. The squared results are combined using the cosines of the two latitudes. This intermediate value, known as "a", is then passed through an arctangent function to produce the central angle between the two points. Finally, that angle is multiplied by the radius of the Earth, roughly 6,371 kilometres, to give the distance.
The result is the shortest distance over the Earth's surface between the two points. It ignores terrain, roads and any obstacles. It is purely the distance as a bird would fly, or more precisely, as an arc drawn on the surface of a sphere.
Where We Use It on Postcodes UK
The Haversine formula is used extensively across this site. Every distance you see has been calculated using it.
Our Postcode Distance Calculator is the most obvious example. When you enter two postcodes, we look up the centroid coordinates for each one and pass them through the Haversine formula. The result is displayed in both miles and kilometres.
On individual postcode pages, the nearby postcodes section is powered by Haversine. We calculate the distance from the current postcode's centroid to every neighbouring postcode and return the closest results, sorted by distance. The same approach is used for nearby schools and nearby train stations on town pages. In each case, we compare the centroid of the place you are viewing against the coordinates of surrounding points of interest.
We also use it in our database queries. MySQL supports the Haversine formula directly within SQL, which allows us to sort and filter results by distance without needing to load everything into memory first. This keeps the site fast even when comparing a single postcode against millions of others.
How Accurate Is It?
The Haversine formula treats the Earth as a perfect sphere. In reality, the Earth is an oblate spheroid, meaning it is slightly flattened at the poles and bulges at the equator. This introduces a small margin of error, typically less than 0.3% for most calculations.
For the distances we deal with on Postcodes UK, which are almost exclusively within the British Isles, this margin of error is negligible. The difference between the Haversine result and a more precise geodesic calculation, such as Vincenty's formula, would be a matter of metres over a distance of hundreds of kilometres. For postcode level accuracy, the Haversine formula is more than sufficient.
Why Not Driving Distance?
A common question is why we show straight line distances rather than driving distances. There are two main reasons. First, driving distances require road network data and routing algorithms, which are significantly more complex and expensive to compute. A Haversine calculation takes microseconds. A driving distance calculation requires querying a road graph with potentially thousands of segments.
Second, straight line distance is consistent and objective. Driving distance depends on which route you take, current road conditions, road closures and personal preference. Straight line distance between two postcodes will always return the same answer, making it a reliable basis for comparison.
A Practical Example
Take the postcodes SW1A 1AA in Westminster and M1 1AE in Manchester. Westminster has a centroid at roughly 51.5014 degrees north, 0.1419 degrees west. Manchester sits at about 53.4723 degrees north, 2.2389 degrees west. Feeding these coordinates into the Haversine formula gives a distance of approximately 262 kilometres, or 163 miles.
That is the distance as the crow flies. The driving distance via the M1 and M6 motorways would be closer to 320 kilometres. Both numbers are useful in different contexts, but the Haversine result gives you a clean, comparable measure of how far apart two places truly are on the surface of the Earth.
Every distance figure you see on Postcodes UK, whether it is the gap between two postcodes, the walk to the nearest school, or how far a train station is from a town centre, is calculated this way. It is a formula that has been trusted by navigators for over two hundred years, and it remains one of the most practical tools in geographic computing.



