Heenan, Lambert
Lambert.Heenan at chartisinsurance.com
Thu Aug 23 13:34:17 CDT 2012
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 -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jack drawbridge Sent: Thursday, August 23, 2012 2:18 PM To: Access Developers discussion and problem solving Subject: Re: [AccessD] Weird problem with lat/long distance function Mark there is no formatting in your text. There are samples in vba at http://www.cpearson.com/excel/LatLong.aspx There may be issues with results where points are very close to each other. What sort of difference in coords do you have? How do you represent coordinates? jack On Thu, Aug 23, 2012 at 1:04 PM, Mark A Matte <markamatte at hotmail.com>wrote: > > Hello All, > I have been using the following for some time...today I am comparing > coordinates that are very close...and I am getting 'weird' > results/errors.(A2007) > I entered the lat/long numbers in the code below to show the behavior > I am seeing. > With these coordinates...the CosArc = 1...but the IF statement below > executes the ELSE. > Unless the absolute value of 1 is NOT 1...why would this not work? > Thanks,,, Mark > 'If Abs(CosArc) = 1 Then' Arc = PI'Else > Public Function Distance( _ ByVal Latitude1 As Double, _ ByVal > Longitude1 As Double, _ ByVal Latitude2 As Double, _ ByVal Longitude2 > As Double, _ Optional Miles As Boolean) As Double'courtesy Lyle > Fairfield'assumes we are getting latitude-longitude'as degrees with > fractions expresed as decimals'if minutes-seconds then use Sexagesimal > function to convertDim CosArc As DoubleDim Arc As DoubleLatitude1 = > Radians(41.70900431)Longitude1 = Radians(-83.585444)Latitude2 = > Radians(41.7090043053031)Longitude2 = > Radians(-83.5854439996183)'Latitude1 > = Radians(Latitude1)'Longitude1 = Radians(Longitude1)'Latitude2 = > Radians(Latitude2)'Longitude2 = Radians(Longitude2) > CosArc = (Sin(Latitude1) * Sin(Latitude2)) + _ (Cos(Latitude1) * > Cos(Latitude2) * Cos(Longitude1 - Longitude2)) > '***PROBLEM HERE***If Abs(CosArc) = 1 Then Arc = PIElse Arc = > Atn(-CosArc / Sqr(-CosArc * CosArc + 1)) + 2 * Atn(1)End IfDistance = > Arc / PI / 2 * CircumferenceIf Miles = True Then Distance = Distance * > MilesPerKilometerDistance = Round(Distance, 2)End Function > Private Function Radians(ByVal degrees As Double) As Double Radians = > PI * degrees / 180End Function > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com > -- AccessD mailing list AccessD at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/accessd Website: http://www.databaseadvisors.com