Haversine formula: Difference between revisions

m
{{out}}
(jq)
m ({{out}})
Line 1:
{{task}}
{{task}}{{Wikipedia}}The '''haversine formula''' is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the '''law of haversines''', relating the sides and angles of spherical "triangles".
{{Wikipedia}}The '''haversine formula''' is an equation important in navigation,
giving great-circle distances between two points on a sphere
from their longitudes and latitudes.
It is a special case of a more general formula in spherical trigonometry,
the '''law of haversines''', relating the sides and angles of spherical "triangles".
 
'''Task:''' Implement a great-circle distance function, or use a library function, to show the great-circle distance between Nashville International Airport (BNA) in Nashville, TN, USA: N 36°7.2', W 86°40.2' (36.12, -86.67) and Los Angeles International Airport (LAX) in Los Angeles, CA, USA: N 33°56.4', W 118°24.0' (33.94, -118.40).
to show the great-circle distance between Nashville International Airport (BNA)
in Nashville, TN, USA: N 36°7.2', W 86°40.2' (36.12, -86.67)
and Los Angeles International Airport (LAX) in Los Angeles, CA, USA: N 33°56.4', W 118°24.0' (33.94, -118.40).
 
<pre>
Line 84 ⟶ 92:
# Americans don't know kilometers #
printf(($"dist: "g(0,1)" km ("g(0,1)" mi.)"l$, d, d / 1.609344))
)</lang>'''Output:'''
{{out}}
<pre>
dist: 2887.3 km (1794.1 mi.)
Line 103 ⟶ 112:
return, Deg * 4 * ATan(1) / 180
}</lang>
{{out}}
'''Output:'''
<pre>2887.259951 km</pre>
 
Line 127 ⟶ 136:
}
</lang>
{{out}}
<p>output:
</p>
<pre>
distance: 2887.2599 km
Line 135 ⟶ 143:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Uses BBC BASIC's '''MOD(array())''' function which calculates the square-root of the sum of the squares of the elements of an array.
the square-root of the sum of the squares of the elements of an array.
<lang bbcbasic> PRINT "Distance = " ; FNhaversine(36.12, -86.67, 33.94, -118.4) " km"
END
Line 145 ⟶ 154:
\ SINRAD(n1) - SINRAD(n2)
= ASN(MOD(d()) / 2) * 6372.8 * 2</lang>
{{out}}
'''Output:'''
<pre>
Distance = 2887.25995 km
Line 244 ⟶ 253:
(deg->rad lat2)
(deg->rad lng2)))</lang>
{{out}}
Output:
<pre>CL-USER> (format t "~%The distance between BNA and LAX is about ~$ km.~%"
(dist-deg 36.12 -86.67 33.94 -118.40))
Line 388 ⟶ 397:
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0</lang>
{{out}}
Output
<pre>2887.259951</pre>
 
Line 426 ⟶ 435:
END FUNCTION
</lang>
{{out}}
'''Output:'''
Distance = 2887.25995060711 km
Press any key to continue...
Line 446 ⟶ 455:
 
36.12e -86.67e 33.94e -118.40e haversine cr f.</lang>
{{out}}
Output:
<pre>
2887.25995060711
Line 561 ⟶ 570:
fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40)))
}</lang>
{{out}}
Output:
<pre>
2887.2599506071097
Line 604 ⟶ 613:
d2r t = t * pi / 180
 
-- The approximate distance, in kilometers, between two points on Earth. The
-- The latitude and longtitude are assumed to be in degrees.
earthDist = distDeg 6372.8
 
Line 613 ⟶ 622:
dst = earthDist bna lax :: Double
printf "The distance between BNA and LAX is about %0.f km.\n" dst</lang>
 
Output:
{{out}}
<pre>The distance between BNA and LAX is about 2887 km.</pre>
 
Line 637 ⟶ 647:
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
 
{{out}}
Output:<pre>BNA to LAX is 2886 km (1793 miles)</pre>
 
=={{header|J}}==
Line 644 ⟶ 655:
haversin=: 0.5 * 1 - cos
Rearth=: 6372.8
haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd</lang>
</plang>
Note: J derives the inverse haversin ( <code>haversin^:_1</code> ) from the definition of haversin.
from the definition of haversin.
 
'''Example Use:'''
Line 669 ⟶ 682:
}
}</lang>
{{out}}
Output
<pre>2887.2599506071106</pre>
 
Line 718 ⟶ 731:
Usage:
<pre>distance[{36.12, -86.67}, {33.94, -118.4}]</pre>
{{out}}
Output:
<pre>2889.68</pre>
 
Line 740 ⟶ 753:
 
[a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40); % BNA to LAX</lang>
{{out}}
Output:
<pre>distance: 2887.2600 km</pre>
 
Line 797 ⟶ 810:
 
echo haversine(36.12, -86.67, 33.94, -118.40)</lang>
{{out}}
Output:
<pre>2.8872599506071115e+03</pre>
 
Line 823 ⟶ 836:
}
</lang>
{{out}}
Output
<pre>
distance: 2886.44
Line 849 ⟶ 862:
=={{header|OCaml}}==
 
The core calculation is fairly straightforward, but with an eye toward generality and reuse, this is how I might start:
but with an eye toward generality and reuse,
this is how I might start:
<lang ocaml>(* Preamble -- some math, and an "angle" type which might be part of a common library. *)
let pi = 4. *. atan 1.
Line 899 ⟶ 914:
};
distEarth(36.12, -86.67, 33.94, -118.4)</lang>
{{out}}
Output:
<pre>%1 = 2886.44444</pre>
 
Line 928 ⟶ 943:
writeln ('Haversine distance: ', haversineDist(36.12, -86.67, 33.94, -118.4):7:2, ' km.');
end.</lang>
{{out}}
Output:
<pre>Haversine distance: 2887.26 km.
</pre>
Line 1,013 ⟶ 1,028:
(round (haversine 36.12 -86.67 33.94 -118.4))
" km" )</lang>
{{out}}
Output:
<pre>Haversine distance: 2,886.444 km</pre>
 
Line 1,046 ⟶ 1,061:
 
end test;</lang>
{{out}}
Output:
<pre>
distance: 2887.260 km
Line 1,113 ⟶ 1,128:
(deg-to-rad 33 56.4 0) (deg-to-rad 118 24.0 0))
</lang>
{{out}}
Output:
<pre>
2886.444442837984
Line 1,238 ⟶ 1,253:
│ One bad side effect is that, like a automobile without a hood, you see│
│ all the dirty stuff going on. Also, don't visit a sausage factory. │
└───────────────────────────────────────────────────────────────────────┘ */</lang>
</lang>
'''output'''
{{out}}
<pre style="overflow:scroll">
Nashville: north 36º 7.2', west 86º 40.2' = 36.12º, -86.67º
Line 1,269 ⟶ 1,285:
puts "%.1f" % spherical_distance(bna, lax)</lang>
 
{{out}}
outputs
<pre>2886.4</pre>
 
=={{header|Run BASIC}}==
<lang runbasic> D2R = atn(1)/45
Line 1,346 ⟶ 1,363:
%haver(36.12, -86.67, 33.94, -118.40);
</lang>
{{out}}
Output:
<pre>Distance is 2887.2599506 K</pre>
 
Line 1,368 ⟶ 1,385:
}
}</lang>
{{out}}
Output:
<pre>2887.2599506071106</pre>
 
Line 1,413 ⟶ 1,430:
end func;</lang>
 
{{out}}
Output:
<pre>
2887.26
Line 1,437 ⟶ 1,454:
# Don't bother with too much inappropriate accuracy!
puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]</lang>
{{out}}
Output:
<pre>distance=2887.3 km</pre>
 
=={{header|UBASIC}}==
<lang basic>
10 Point 7 'Sets decimal display to 32 places (0+.1^56)
20 Rf=#pi/180 'Degree -> Radian Conversion
Line 1,454 ⟶ 1,472:
2887.2599506 km
OK
</lang>
 
=={{header|X86 Assembly}}==
Line 1,541 ⟶ 1,560:
019D 46472666 Radius2 dd 12745.6 ;6372.8 average radius of Earth (km) times 2
;(TASM isn't smart enough to do floating point constant calculations)
end start</lang>
</lang>
 
{{out}}
Output:
<pre>
2887.25
Line 1,564 ⟶ 1,583:
RlOut(0, Dist(36.12*D2R, 33.94*D2R, -86.67*D2R, -118.40*D2R ));</lang>
 
{{out}}
Output:
<pre>
2887.25995
Anonymous user