Question

More specifically, how to test further? I saw another post where the answer used a hexdump. How might I try this, and should I?

Here's my trying in console...

variables: myl is my latitude value from my database. ul is the value from an api. ll is where I hand retyped the value:

ruby-1.9.2-p180 :106 > myl=a.latitude   #=> "42.3471841"
ruby-1.9.2-p180 :107 > ul=@events[30].xpath('venue')[0]['lat']
=> "42.3471836"
ruby-1.9.2-p180 :108 > myl==ul   #=> false
ruby-1.9.2-p180 :109 > myl==ul.to_i   #=> false
ruby-1.9.2-p180 :110 > myl.to_i==ul.to_i   #=> true
ruby-1.9.2-p180 :111 > Venue.find_by_latitude(ul)   #=> nil
ruby-1.9.2-p180 :112 > Venue.find_by_latitude(ul.to_i)   #=> nil
ruby-1.9.2-p180 :113 > Venue.find_by_latitude(ul.to_s)   #=> nil
ruby-1.9.2-p180 :114 > ul   #=> "42.3471836"
ruby-1.9.2-p180 :115 > myl.class   #=> String
ruby-1.9.2-p180 :116 > ul.class   #=> String
ruby-1.9.2-p180 :117 > ll="42.3471836"   #=> "42.3471836"
ruby-1.9.2-p180 :118 > myl==ll   #=> false
ruby-1.9.2-p180 :119 > ul==ll   #=> true

Any suggestions would be most helpful! The goal is to use latitude and longitude to identify a place already stored in my database, since names of a place are not always unique.

Was it helpful?

Solution

Two things:

As I understand it, you're envisioning these things as numbers, not strings, so it's probably best to convert them before you make the comparison.

Second, since the strings aren't really equal, but fairly close as latitudes go, I'm going to go out on a limb and guess that you're really trying to compare them with some allowed margin of error. Like so (assuming myLatitude and apiLatitude are numbers):

(myLatitude - apiLatitude).abs < 0.000001

And (while you're at it) make sure that your longitude convention (-180 to 180 vs. 0 to 360) is consistent when you're comparing those.

OTHER TIPS

myl = "42.3471841" ul = "42.3471836" ll = "42.3471836"

Of course myl is not equal to ul (they contain different string values), but ul and ll are equal (they contain the same string value). I dont understand what your question is? Your code shows why myl isnt equal to ll

You may have a problem with the values stored being different enough that an exact match won't work. This often comes into play when you're dealing with varying levels of precision.

You may want to switch to a convention where all values are forced to a particular level of precision before being saved. For instance, fix them before saving:

def set_precision
  self.latitude = '%.6f' % self.latitude.to_f
  self.longitude = '%.6f' % self.longitude.to_f
end

This will convert values like "42.72" into "42.720000" as well as trimming excessively precise values.

When searching, just apply the same formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top