// [snippet: measures and type declerations] [] type rad [] type deg [] type km type Location = { Latitude : float; Longitude : float } // [/snippet] // [snippet: calculation with haversine-formula] let GreatCircleDistance<[] 'u> (R : float<'u>) (p1 : Location) (p2 : Location) = let degToRad (x : float) = System.Math.PI * x / 180.0 let sq x = x * x // take the sin of the half and square the result let sinSqHf (a : float) = (System.Math.Sin >> sq) (a / 2.0) let cos (a : float) = System.Math.Cos (degToRad a / 1.0) let dLat = (p2.Latitude - p1.Latitude) |> degToRad let dLon = (p2.Longitude - p1.Longitude) |> degToRad let a = sinSqHf dLat + cos p1.Latitude * cos p2.Latitude * sinSqHf dLon let c = 2.0 * System.Math.Atan2(System.Math.Sqrt(a), System.Math.Sqrt(1.0-a)) R * c // [/snippet] // [snippet: using the mean-earth-radius] let GreatCircleDistanceOnEarth = GreatCircleDistance 6371.0 // [/snippet] // [snippet: example] let p1 = { Latitude = 53.147222222222222222222222222222; Longitude = 0.96666666666666666666666666666667 } let p2 = { Latitude = 52.204444444444444444444444444444; Longitude = 0.14055555555555555555555555555556 } GreatCircleDistanceOnEarth p1 p2 // [/snippet]