i am using a couple of methods to calculate the distance between any 2 points on the earths surface. the methods are the law of cosines, and the haversine formula.

the law of cosines formula uses the C++ functions acos, sin and cos, all found in math.h:

acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat1).cos(long2−long1))

the haversine formula also uses sin and cos, but also atan2.

dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))2 + cos(lat1) * cos(lat2) * (sin(dlon/2))2
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c


does anyone know how these functions operate, and if one is more 'effiecient' than the other (ie running time). specifically i am interested in the atan2 api. is this function 'slower' than the cos and sin functions? or is it better?

i have tried to test these 2 methods by calling them in a loop for a set number of times - and they both seem to run in the same time (ish). but i dont think my tests are particularly strong. i would like some conclusive evidence that supports the performance of these maths functions - i cant find any documentation on the net concerning this.

Recommended Answers

All 2 Replies

I think most math operations are performed on the math coprocessor instead of in code. So the functions are probably optimized as much as possible.

ok thats cool, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.