Forum: Ruby Trouble migrating formula from JavaScript to Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ingo W. (Guest)
on 2006-04-27 14:07
Hi,

I am trying (unsuccessfully) to migrate a formula (for calculating
distances between lat/lng positions) from JavaScript to Ruby:

//JavaScript:
dlng = p2_lng - p1_lng
dlat = p2_lat - p1_lat
a = (sin(dlat/2))^2 + cos(p1_lat) * cos(p2_lat) * (sin(dlon/2))^2
c = 2 * atan2( sqrt(a), sqrt(1-a) )
distance = R * c (where R is the radius of the Earth)


#Ruby:
dlng = p2_lng - p1_lng
dlat = p2_lat - p1_lat
a = (Math.sin(dlat/2))**2 + Math.cos(p1_lat) * Math.cos(p2_lat) *
(Math.sin(dlng/2))**2
c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a) )
distance = 6373 * c #(radius of the Earth: 6373km


Any idea why the Ruby one returns incorrect numbers?

Ingo W.
Chiaro S. (Guest)
on 2006-04-27 14:23
(Received via mailing list)
is it possible that you need to make numbers explicitly float? eg: 2 -->
2.0
This topic is locked and can not be replied to.