liveBook · Manning

Haversine formula

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A trigonometric formula that computes spherical distance between two points from their latitude and longitude differences.

The haversine formula calculates the geographic distance between two points on Earth from their latitudes and longitudes, modeling Earth with a mean spherical radius. The result is also called great-circle distance. section 4.3.3

The calculation proceeds through these quantities:

  • Compute a = sin²(Δlat/2) + cos(lat1) × cos(lat2) × sin²(Δlon/2). section 4.3.3
  • Compute C = 2 × atan2(√a, √(1 − a)). section 4.3.3
  • Multiply by the mean Earth radius: d = R × C, using 6,371 km or 3,691 miles. section 4.3.3

Los Angeles to Madrid

A Python implementation passes Los Angeles (34.052235, −118.243683) and Madrid (40.416775, −3.703790) to haversine(), then prints the resulting distance.

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Defines the formula, intermediate quantities, Earth-radius parameter, and Python usage without developing its derivation.
Examples
Uses Los Angeles and Madrid coordinates in a Python haversine-library example.
What it assumes
Assumes latitude-longitude pairs and distinguishes geographic coordinates from graph nodes without spatial data.
What you can do after
Apply it via the haversine Python library to calculate distance from two latitude-longitude pairs.
How it connects
Connects geographic coordinates with great-circle distance, A* search, heuristics, and routing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.