 Ingo W.:

# Any idea why the Ruby one returns incorrect numbers?

1. can you post sample running codes on both? (eg what are the values of
p2_lang, etc)

2. do you have test cases? (so we know what to expect)

kind regards -botp

PeÃ±a wrote:

# a = (Math.sin(dlat/2))**2 + Math.cos(p1_lat) * Math.cos(p2_lat) *

is dlng a typo, the first formula uses dlon?

Turned out that I needed to convert the numbers to radians. Here is the
code that worked:

#Calculate distance between 2 coordinate points (decimal lat/lng),
expressed in km
#This is an approximation since it assumes the earth to be a perfect
sphere
def calculate_distance(p1, p2)

# radius_of_earth = 3963.0 #miles

radius_of_earth = 6373.0 #kilometers

#convert position to decimal and radians:
to_rad = 180/Math::PI
p1_lat = degmin_to_dec(p1.lat) / to_rad
p1_lng = degmin_to_dec(p1.lng) / to_rad
p2_lat = degmin_to_dec(p2.lat) / to_rad
p2_lng = degmin_to_dec(p2.lng) / to_rad

#calculate distance:
distance = radius_of_earth * Math.acos(Math.sin(p1_lat) *
Math.sin(p2_lat) + Math.cos(p1_lat) * Math.cos(p2_lat) * Math.cos(p2_lng

• p1_lng))

end

Ingo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs