我有下面的代碼用于計算機場串列之間的 Haversine 距離,但是它始終回傳不正確的值。例如,僅當 ORD 和 JFK 之間的實際距離約為 827 英里時,通過運行在 ORD (Chicago) 和 JFK (NYC) 上運行以下代碼haversine (head $ allAirports) (last $ allAirports)才會回傳
。92.16479615931107知道我在計算中做錯了什么嗎?
type Location = (Double, Double)
data Airport = Airport {
name :: String,
location :: Location
} deriving (Eq)
allAirports :: [Airport]
allAirports = [
Airport { name="ORD", location=(41.9786,-87.9048)},
Airport { name="JFK", location=(40.64505923593942, -73.777106518636)}]
haversine :: Airport -> Airport -> Double
haversine (Airport _ (x1,y1)) (Airport _ (x2,y2)) = radius * c
where radius = 3959.87433
to_rad :: Double -> Double
to_rad x = (x * pi) / 180
r_x1 = to_rad x1
r_x2 = to_rad x2
r_y1 = to_rad y1
r_y2 = to_rad y1
dlat = r_x2 - r_x1
dlon = r_y2 - r_y1
a = sin(dlat/2)**2 cos(r_x1)*cos(r_x2)*sin(dlon/2)**2
c = 2 * asin(sqrt(a))
uj5u.com熱心網友回復:
你這里有一個錯字:
r_y2 = to_rad y1
— 應該y2代替y1. 如果你修復它,你會得到?738這是正確的答案。您想要的數字 827 似乎是 Google 地圖顯示的行駛距離、道路等資訊。如果您搜索“jfk 和 ord 之間的飛行距離”,您會得到 738。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474028.html
