# Converting a polar coord to the range $0\le\theta\le2\pi?$

I know that you can keep adding/subtracting numbers to a polar coord, but what if I want to be able to take a number and just convert it to its positive equivalent?

-

I will assume you want $0 \leq \theta < 2\pi$, since $2\pi$ and $0$ are the same angle. This is the modulo operation.

You can write it as $x \operatorname{mod} 2\pi$, or express it via floor function $x-2 \pi \lfloor \frac{x}{2\pi} \rfloor$, which works for both positive and negative $x$. Warning: Some programming languages with floating point modulo operators will give you negative result for negative angle. You can fix that by a single check if (result < 0) then result += 2*pi.

Theorem: For any real $a,b$ such that $b>0$ there exists exactly one integer $q$ and real $r \in [0,b)$ such that $a=qb+r$.

-

If your system has an Atan2 function, it will be done for you.

-
atan2(theta) = new theta? – John Smith Jan 26 '12 at 19:20
I'm not sure this function does what I need it to. For instance, say I have theta = 6*pi. I should be able to extract 0 as an answer. If I have -7pi/2, I should be able to extract 3*pi/2 as an answer or something to that extent – John Smith Jan 26 '12 at 19:35
@JohnSmith: I was assuming you had $(x,y)$ coordinates and wanted the angle. If you already have an angle, some systems allow the mod function on real arguments: theta%(2pi) will give something in the range [0,2pi). Otherwise you can use integer division: c=int(theta/(2pi)); theta=theta-c*2pi – Ross Millikan Jan 26 '12 at 22:00

If I'm right you want a function to get an input of any real number and convert it to it's equivalent angle between $0<\theta< 2\pi$? here is pseudo code how I do it: $$\begin{eqnarray}&\text{while}(\theta<0\text{ or }\theta > 2\pi\text{)} \\ &\text{if(}\theta > 0)\\ &&\theta := \theta - 2\pi\\ &else\\ &&\theta := \theta + 2\pi\\ \end{eqnarray}$$

-