RosettaCodeData/Task/Haversine-formula/MiniScript/haversine-formula.mini
2026-04-30 12:34:36 -04:00

17 lines
587 B
Text

// compute the distance between places using the Haversine formula
distance = function( th1Deg, ph1Deg, th2Deg, ph2Deg )
toRadians = pi / 180
ph1 = toRadians * ( ph1Deg - ph2Deg )
th1 = toRadians * th1Deg
th2 = toRadians * th2Deg
dz = sin( th1 ) - sin( th2 )
dx = cos( ph1 ) * cos( th1 ) - cos( th2 )
dy = sin( ph1 ) * cos( th1 )
return asin( sqrt( dx * dx + dy * dy + dz * dz ) / 2 ) * 2 * 6371
end function
d = distance( 36.12, -86.67, 33.94, -118.4 )
km = round( d )
mi = round( d / 1.609344 )
print( "distance: " + km + " km (" + mi + " mi.)" )