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:


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.

11 Years
Discussion Span
Last Post by unclepauly

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.