Sun 3 Aug 2008
If you ever needed proof to see that the world we live in a complex, and at the same time, that it is possible to draw extraordinarily elegant conclusions from math, you don’t have to go any further than one of fundamental elements of geometry, angles. In primary school, you are told about angles. In high school, you learn basic trigonometry and vector math. As an (engineering) undergraduate, you learn to analyze and apply these concepts to problems which incorporate change of angle with calculus. But at no time, do I remember having anyone offer a mathematical explanation for why the angles 2.n.pi+a , … , -2.pi+a, 0+a, 2pi+a, …, 2.n.pi+a (for all n, in the set of integers, and for all a in the set of real numbers) are equivalent.
It turns out that there is a (reasonably) simple explanation, and a branch of mathematics which can be used to describe such problems - group theory. In particular, the types of effects we see when working with angles can be explained by using the circle group. I’m going to leave most of the math to wikipedia pages mentioned at the bottom of the article.
I’m a programmer at heart, and so the circle group is of interest to me when I need to consider operations on elements of the group (addition and subtraction), or in particular, testing the equivalence of two values in the group, that is, answering the question - when are two angles effectively equal?
One possible approach is to bring both angles into a subset of the circle group, in which we can directly compare angles as real numbers. Typically, a reasonable subset of the circle group is either [0,2pi) or [-pi,pi), but any continuous range of length 2pi is fine. For testing equivalence, the simplest range is likely to be [0,2pi).
From a mathematical perspective, conversion of an angle to a value in the range [0,2pi) can be achieved by taking the modulo of an angle.
a’ = a mod 2pi
In (c-style) psuedocode, the same operation can be stated as:
angle angularModulo(angle a) { return a % (2 * M_PI); }
There are a few caveats about this approach. The first is that the modulus operator in c is not defined for floating point types (angle cannot be double or float), instead it is necessary to use the fmod functions. The behavior of the fmod function is described in Annex F (F.9.7) of the c-standard (ISO/IEC 9899:1999) - and the results for negative angles are somewhat counter-intuitive.
As far as I can tell, a similar definition to the mathematical one can be archived by the following function.
double angularModulo(double a) { if(a >=0) return ::fmod(a,2*M_PI); else return 2*M_PI + ::fmod(a,2*M_PI); }
References
http://en.wikipedia.org/wiki/Angle
http://en.wikipedia.org/wiki/Euclidean_space
http://en.wikipedia.org/wiki/Orthogonal_group
http://en.wikipedia.org/wiki/Circle_group


