Points
Texts
Sets
Route
View
Distance metrics: nine ways to measure how far apart
How far apart are two things? It depends on what they are and how you measure. A crow flies the Euclidean distance, a taxi in a grid city drives the Manhattan distance and a chess king walks the Chebyshev distance. Two documents are close when their vectors point the same way (cosine), two codes when few letters differ (Hamming), two baskets when they share many items (Jaccard and Sørensen–Dice), and two cities along the great circle over the Earth (haversine). Here each metric gets its own picture and its formula with your numbers. Pick one of the nine metrics and see how it measures the distance between two things. For points in the plane (Euclidean, Manhattan, Minkowski and Chebyshev) drag A and B on the grid: the coloured curve shows every point as far from A as B is, a circle for Euclidean, a diamond for Manhattan, a square for Chebyshev and a rounded square for Minkowski, and Other metrics draws the curves of the others for comparison. Cosine treats A and B as arrows from the origin and only compares their directions. Hamming compares two texts letter by letter, Jaccard and Sørensen–Dice compare two sets of items (write them separated by commas or spaces) in a Venn diagram, and Haversine measures the great-circle distance between two places on the Earth: pick a route or drag the cities on the globe. The Formula card shows the formula and the calculation with your numbers.
How it works
- Choose one of the nine metrics.
- For points, drag A and B on the grid; the coloured curve shows every point as far from A as B is.
- For texts and sets, type them in; for haversine, pick a route or drag the cities on the globe.
- Read the distance in the tiles and the calculation in the Formula card, and turn on Other metrics to compare.
Points: Euclidean, Manhattan, Minkowski and Chebyshev
For two points A and B the four plane metrics are built from the same two numbers, the differences Δx and Δy. Euclidean distance √(Δx² + Δy²) is the straight line of Pythagoras. Manhattan distance |Δx| + |Δy| is the walk along a grid of streets: every shortest route has the same length. Chebyshev distance max(|Δx|, |Δy|) is the number of moves a chess king needs. Minkowski distance (|Δx|ᵖ + |Δy|ᵖ)^(1/p) contains them all: p = 1 is Manhattan, p = 2 Euclidean, and as p grows it approaches Chebyshev. The points at a fixed distance from A form a diamond, a circle, a rounded square or a square, which the app draws through B.
Directions: cosine similarity
Cosine similarity compares the directions of two vectors and ignores their lengths: cos θ = A·B / (‖A‖ ‖B‖) is 1 for vectors that point the same way, 0 for vectors at right angles and −1 for opposite ones. Cosine distance is 1 − cos θ, from 0 to 2. Search engines and language models compare documents and embeddings this way, because a long text and a short text about the same subject point in about the same direction even though their word counts differ.
Texts and sets: Hamming, Jaccard and Sørensen–Dice
Hamming distance counts the positions where two strings of equal length differ: karolin and kathrin differ in 3 of 7 letters. It measures errors in codes and transmitted bits and mutations in DNA. Jaccard and Sørensen–Dice compare sets regardless of order: Jaccard divides the items both share by all the items, J = |A ∩ B| / |A ∪ B|, and Dice divides twice the shared items by the two sizes added up, 2|A ∩ B| / (|A| + |B|). Dice is always at least as large as Jaccard, and DSC = 2J / (1 + J).
Places: the haversine formula
On the curved surface of the Earth the shortest path between two places is an arc of a great circle, the circle whose centre is the centre of the Earth. The haversine formula a = sin²(Δφ/2) + cos φ₁ cos φ₂ sin²(Δλ/2) gives the central angle c = 2 asin √a between two places from their latitudes φ and longitudes λ, and the distance is R × c with the Earth's mean radius R = 6371 km. Amsterdam to New York is about 5,860 km. It stays accurate for short distances, where the plain law of cosines loses digits, which is why navigation and mapping software use it.
Frequently asked questions
What is the difference between Euclidean and Manhattan distance?
Euclidean distance is the straight line between two points, √(Δx² + Δy²). Manhattan distance only moves along the axes, |Δx| + |Δy|, like a taxi in a city of square blocks. For A = (−4, 2) and B = (4, 8) the differences are 8 and 6, so the Euclidean distance is 10 and the Manhattan distance 14. Manhattan is never shorter than Euclidean and at most √2 times as long in the plane.
Which distance metric should I use?
It depends on the data. Use Euclidean for positions and measurements in the same units, Manhattan when moves follow a grid or when features should add up independently, Chebyshev when only the largest difference matters, cosine for text, documents and embeddings where direction matters more than size, Hamming for codes and strings of equal length, Jaccard or Sørensen–Dice for sets such as tags, shopping baskets or image masks, and haversine for latitudes and longitudes.
Is cosine distance a real metric?
Not strictly. A metric must satisfy the triangle inequality, d(A, C) ≤ d(A, B) + d(B, C), and 1 − cos θ does not always do so. The angle θ itself (the angular distance) is a true metric. Cosine distance is still the standard way to compare word vectors and document embeddings, because it is fast and ignores the vectors' lengths.
What is the difference between the Jaccard index and the Dice coefficient?
Both measure the overlap of two sets. Jaccard divides the shared items by all distinct items, |A ∩ B| / |A ∪ B|; Dice divides twice the shared items by the two set sizes added up, 2|A ∩ B| / (|A| + |B|). For sets with 2 items in common out of 4 and 5 items, Jaccard is 2/7 = 0.29 and Dice 4/9 = 0.44. They always rank pairs of sets the same way, since Dice = 2J / (1 + J), but only Jaccard distance, 1 − J, is a true metric.
How accurate is the haversine distance?
The haversine formula is exact for a sphere. The Earth is slightly flattened, so the distance on a sphere of mean radius 6371 km differs from the distance on the WGS 84 ellipsoid by up to about 0.5 %, a few kilometres on long flights. For geodesy, Vincenty's or Karney's formulas on the ellipsoid are more accurate; for flight distances, maps and nearest-place searches haversine is the usual choice.
Can I use the picture?
Yes. Save gives a PNG at the size you choose, and the pencil opens the picture in the PictorX photo editor with the background, the grid, the equal-distance curve, the path, the points, the labels and the Formula card on layers of their own, and the title, the formula and the calculation as editable text. In the editor itself it is under Apps ▸ Science ▸ Computing ▸ Distance Metrics. Everything runs in your browser.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Go to Editor