distances.online

Blog article

Haversine calculator: formula step by step, with a map

Why do planes seem to fly in a curve? The maths behind the answer, step by step.

Published: Updated: All articles

Drag marker A or B anywhere on the map. The blue line is the shortest path over the globe, the grey one the straight line on the flat map; the distance and every step of the formula update as you move them.

Point A
Point B
Distance
5,576.5 km
5,576,462 m

You can also type the coordinates here.

Open New York to London in the calculator

The formula, step by step, for the two points above

StepFormulaValue
Latitude of A in radiansφ₁ = lat(A) · π / 1800.710572 rad
Latitude of B in radiansφ₂ = lat(B) · π / 1800.898974 rad
Latitude differenceΔφ = φ₂ − φ₁0.188401 rad
Longitude differenceΔλ = λ₂ − λ₁1.289418 rad
Haversine terma = sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2)0.17923643
Central anglec = 2 · atan2(√a, √(1 − a))0.874309 rad
Distanced = R · c (R = 6,378.137 km)5,576.5 km

When you type New York and London into the calculator, the first number you get is 5,576 km. No road was consulted for it, no plane timetable: it is the shortest possible path over the surface of the Earth between the two city centres, and it comes out of a short piece of trigonometry called the haversine formula. This article writes the formula out, walks through that exact New York–London calculation, and lets you drag two points around a map to see every step update live. If you would rather have the idea without the equations first, start with the plain-words introduction.

Why a ruler on a flat map lies

On a map, the route of a long flight almost never appears as a straight line but as an arc. It looks like a detour, but it is the shortest way.

A globe and a piece of string are enough to check it. Fix one end on London and the other on New York and pull the string tight: it does not follow the parallel westwards; it rises over the Atlantic, passes close to Greenland and comes back down. It is the same path planes take. That path is called a great circle, and it is the edge you get when you cut the globe in two through its centre. On the sphere it is the straightest possible line. The line the calculator draws between two cities is that arc, drawn onto the flat map.

The same route on a globe (left) and on the flat map (right). On the sphere it is the straightest line there is; on the map it bows north, and the grey line is what a ruler would draw. Click an image to enlarge it.

The trouble starts when the sphere is flattened onto a plane, as in most maps: proportions get distorted. Near the equator the flat map is almost identical to the sphere, but the further you move towards the poles, the more it stretches. On the most common map, Greenland appears almost the size of Africa, which is really about fourteen times larger.

That is why a flight path drawn on the plane looks more curved the further from the equator it runs. London–New York draws a marked arc, while Madrid–Buenos Aires, which crosses the equator, looks almost straight.

So to measure distance properly we leave the map aside and work on the sphere itself, with latitudes and longitudes. That is what haversine does.

The formula

Call the two points A and B. Each has a latitude φ (phi) and a longitude λ (lambda), the two angles that locate it on the globe. Latitude says how far north or south of the equator; longitude how far east or west of the Greenwich meridian.

a= sin2 (Δφ2) + cos⁡φ1 · cos⁡φ2 · sin2 (Δλ2)
c= 2·atan2⁡ (a,1−a)
d=R·c

Reading it line by line:

  • Δφ (delta phi) is the difference in latitude between A and B, and Δλ (delta lambda) the difference in longitude. Both must be in radians, not degrees: multiply degrees by π and divide by 180.
  • a is the "haversine" of the central angle. It mixes the north–south gap and the east–west gap, scaling the east–west part by the cosines of both latitudes, because a degree of longitude is shorter the closer you are to a pole (at the equator one degree of longitude is about 111 km; at 60° north it is only 56 km).
  • c turns that number into the angle, in radians, between the two points as seen from the centre of the Earth. The atan2 form is the one used in practice: it stays accurate even when the two points are extremely close or exactly opposite each other.
  • d is the distance: that angle times the radius of the Earth, R. With R in kilometres you get kilometres.

The word haversine is short for "half the versed sine", a trigonometric function that sailors had in printed tables since the 1800s because it made exactly this calculation easy to do by hand on a ship. The modern computer version is the same idea written with sin².

New York to London, step by step

The calculator uses these city-centre coordinates: New York at latitude 40.7128°, longitude −74.0060° (west is negative), and London at latitude 51.5074°, longitude −0.1278°. Running the formula with the same Earth radius the calculator uses, 6378.137 km:

StepFormulaValue
Latitude of New York in radiansφ₁ = 40.7128 · π / 1800.710572 rad
Latitude of London in radiansφ₂ = 51.5074 · π / 1800.898974 rad
Latitude differenceΔφ = φ₂ − φ₁0.188401 rad
Longitude differenceΔλ = (−0.1278 − (−74.0060)) · π / 1801.289418 rad
Haversine terma = sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2)0.17923643
Central anglec = 2 · atan2(√a, √(1 − a))0.874309 rad
Distanced = 6378.137 km · c5,576.5 km

Two things worth noticing. The central angle is about 50 degrees, a seventh of a full turn, which is why the arc between New York and London bows so visibly on the flat map. And the longitude difference (1.2894 rad, about 74°) matters far more than the latitude difference (0.1884 rad, about 11°): the two cities are roughly at the same height, so most of the distance is east–west.

How accurate is it?

The formula assumes the Earth is a perfect sphere. It is not: it bulges at the equator and is flattened at the poles, by about one part in 300. The distance from the centre to the equator is 6378.1 km, to the poles 6356.8 km.

That has two practical consequences:

  1. The radius you pick changes the answer. This site uses the equatorial radius, 6378.137 km, and gets 5,576.5 km for New York–London. Many references use the mean radius of 6371 km instead and get 5,570.2 km. Both are "the" haversine distance; they differ by about 0.1 %.
  2. Against the real shape of the Earth, haversine can be off by up to about 0.5 %. For New York–London that is up to about 28 km. For London–Sydney, about 17,000 km, it can be 50 km or more. Methods such as Vincenty's formulae work on the ellipsoid and get within millimetres, at the price of being much heavier to compute and occasionally failing to converge for near-antipodal points.

For a distance calculator or a running tracker, half a percent is noise and haversine is the right tool: a handful of operations, no iterations, no failure cases. Surveyors and navigation systems that care about centimetres use the ellipsoidal methods.