[AccessD] Weird problem with lat/long distance function

Stuart McLachlan stuart at lexacorp.com.pg
Thu Aug 23 16:33:09 CDT 2012


I agree with Lambert. You need to specify an acceptable "margin of error" 

Consider that each of the four variables in the following calculation is itself the result of 
floating poiint calculations using an approximation of PI  (how you derive this approximation 
and how accurate it is is not shown)

CosArc = (Sin(Latitude1) * Sin(Latitude2)) + _   
(Cos(Latitude1) * Cos(Latitude2) * Cos(Longitude1 - Longitude2))

Because of rounding errors in the multiple floating point calculations involved and the 
approximation of PI, it is extremenly unlikely that this will ever return exactly 1.

Computers can be very precise without being very accurate.

-- 
Stuart

On 23 Aug 2012 at 14:34, Heenan, Lambert wrote:

> Random email client sabotage of code caused that I guess.
> 
> Mark,
> 
> Your problem line is
> 
> If Abs(CosArc) = 1 Then 
> 
> As you state.  As we are talking about floating point math here I would immediately thing that the issue is one of precision.
> 
> You are checking if the returned value is 1.0, but that is likely never going to be the exact result because its FP math. I think you will need to define a tolerance that is "close enough" to the target value. Something like
> 
> CONST Tolerance =0.000000001
> 
> If Abs((Abs(CosArc) - 1)) <= Tolerance Then...
> 
> Lambert
> 



More information about the AccessD mailing list