arcsin is only real for values between -1 & 1.

The earth_longitude_range() function in earth.inc sometimes uses values outside that range.

CommentFileSizeAuthor
#5 location-821628-5.patch2.12 KBrooby
#1 location-821628-1.patch654 bytesrooby
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rooby’s picture

FileSize
654 bytes

Possible solution

rooby’s picture

Status: Active » Needs review
YesCT’s picture

+++ earth.inc	2010-06-08 23:30:20.000000000 +1000
@@ -130,7 +130,15 @@ function earth_longitude_range($longitud
-  $diff = asin(sin($angle)/cos($lat));
+  // arcsin is only real for values between -1 & 1.
+  $diff = sin($angle)/cos($lat);
+  if ($diff > 1) {
+    $diff = 1;
+  }
+  else if ($diff < -1) {
+    $diff = -1;
+  }
+  $diff = asin($diff);

code style looks ok. adds comment. approach makes sense to keep values within -1 to 1 (just cuts off values outside that range).

how big is the world? if a search is for locations within say 10,000 miles, would this find "all" locations?
to finish the review, I think what is needed is to
1) get the latest dev
2) apply this patch
3) try searches that are large? and across datelines?
4) reply here with a summary of what functional tests (searches) you tried and the results, and say what the results were before the patch and after the patch

Powered by Dreditor.

rooby’s picture

Yeah, that was a quick fix without having to think about it. Will have to think it through properly.

The main thing though is that values outside that range cannot be passed into asin because it returns NAN which is not acceptable.

rooby’s picture

FileSize
2.12 KB

I have looked at this problem a bit and have come to the following conclusion.

This method is the only real fix for this function. This patch is the same solution as in #1 but a bit neater and with better commenting.

This will mean we don't get NAN results from the asin function.
It also means that for large values of distance the calculation won't be accurate.
This is not regression because getting NAN means the calculation won't work at all, it just means the function has never worked properly for large distance values.

I think this patch should be committed for now but the function needs to be rewritten using a different formula so it works for larger values.

For reference, these are the around about the maximum values of distance that can be used with this function before you start getting inaccurate results, the max value changes relative to the latitude of the point you are measuring from (if my calculations are correct):

Latitude (degrees) Max distance value (metres)
1/-1 9907423
15/-15 8347076
30/-30 6673549
45/-45 5000957
60/-60 3331176
75/-75 1664567
89/-89 110946

Any thoughts/opposition?
Will look at rewriting those range functions sometime soon.

rooby’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.