Solved: what is wrong with this LatLong->Distance code? A Trigonometry "feature".
10-13-2016, 10:12 AM (This post was last modified: 10-13-2016 10:25 AM by Guenter Schink.)
Post: #4
 Guenter Schink Senior Member Posts: 503 Joined: Dec 2013
RE: Can you see what is wrong with this LatLong->Distance code?
(10-12-2016 09:46 PM)StephenG1CMZ Wrote:  Thank you for taking the time to look at this.
When I first saw your code I was thinking the lower-case Sin was exact and avoiding a rounding error - but it doesn't fix this example, which is still 1 km rather than 54 km.
Sorry the lower case "sin" was not intentionally.

But now I think we're coming closer. Don't know what really happens (a bug?). I added commands to print DLON and DLAT. And much to my surprise they showed up in DMS format. Consequently I removed the transformation to radians et voilá the result comes pretty close to what's expected.
Code:
EXPORT Haversine(LAT1,LON1,LAT2,LON2)  BEGIN   LOCAL RR:=6378.137;   LOCAL DLON:=(LON2-LON1);   LOCAL DLAT:=(LAT2-LAT1);   LOCAL AA:=(SIN(DLAT/2))^2 +     COS(LAT1)*    COS(LAT2)*(SIN(DLON/2))^2;   LOCAL CC:=2*ASIN(MIN(1,√(AA)));//MIN 1 AVOIDS ROUNDING OUTRANGE   LOCAL DD:=RR*CC;   PRINT("DLAT " + DLAT);   PRINT("DLON " + DLON);   RETURN ("DD " + DD);  END;
That's really weird. The "Angle Measure" is set to radians! I guess you have it too. The problem seems not to show up when entering the co-ordinates not in DMS but degrees (with decimal degrees)**. On the other hand, I don't understand, at least in the Haversine formula, why a transformation to radians is needed after all.

It will happen on the command line too. Just enter "12°30' * PI / 180" the expression will stay in DMS mode.

HTH, Günter

** That's obviously the reason why I didn't encounter the problem, as I entered the data in degrees format.
Günter
 « Next Oldest | Next Newest »

 Messages In This Thread Solved: what is wrong with this LatLong->Distance code? A Trigonometry "feature". - StephenG1CMZ - 10-12-2016, 06:20 PM RE: Can you see what is wrong with this LatLong->Distance code? - Guenter Schink - 10-12-2016, 08:14 PM RE: Can you see what is wrong with this LatLong->Distance code? - StephenG1CMZ - 10-12-2016, 09:46 PM RE: Can you see what is wrong with this LatLong->Distance code? - Guenter Schink - 10-13-2016 10:12 AM RE: Can you see what is wrong with this LatLong->Distance code? - StephenG1CMZ - 10-13-2016, 04:52 PM RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug? - Tim Wessman - 10-13-2016, 05:31 PM RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug? - Guenter Schink - 10-14-2016, 09:53 AM RE: Can you see what is wrong with this LatLong->Distance code? Trigonometry bug? - StephenG1CMZ - 10-13-2016, 08:58 PM RE: Can you see what is wrong with this LatLong->Distance code? A Trigonometry &quo... - StephenG1CMZ - 10-17-2016, 10:17 PM RE: Solved: what is wrong with this LatLong->Distance code? A Trigonometry "fe... - StephenG1CMZ - 10-23-2016, 06:41 PM

User(s) browsing this thread: 1 Guest(s)