integral $\int \frac{1-a\cos x}{1+a^2-2a\cos x}dx$ I'm trying to solve this integral: $$\int \frac{1-a\cos x}{1+a^2-2a\cos x}dx.$$
My trouble: Using software, i found that if $|a|>1$, the integral is equal to $$\arctan \left(\frac{x}{2}\right)+\frac{1-a^2}{|a-1||a+1|}\arctan \left(\frac{|a+1|}{|a-1|}\tan \left( \frac{x}{2} \right) \right )+C.$$ 
If $|a|<1$, then the antiderivate is $$x-\arctan \left(\frac{x}{2}\right)+\frac{1-a^2}{|a-1||a+1|}\arctan \left(\frac{|a+1|}{|a-1|}\tan \left( \frac{x}{2} \right) \right )+C.$$
Integrating over the interval $[0,2\pi]$, the integral is equal to $2\pi$ if $|a|<1$ and $0$ if $|a|>1$. Using some skills, like this:
$$\int \frac{1-a\cos x}{1+a^2-2a\cos x}dx =\frac{1}{2}\int \frac{2-2a\cos x+a^2-a^2}{1+a^2-2a\cos x}dx $$
$$=\frac{1}{2}\int dx+\frac{1}{2}\int \frac{1-a^2}{1+a^2-2a\cos x}dx$$
I found the answer $$\frac{x}{2}+\frac{1-a^2}{|a-1||a+1|}\arctan \left(\frac{|a+1|}{|a-1|}\tan \left( \frac{x}{2} \right) \right )+C.$$
Integrating this over the same interval as before, the result is not correct. So, my question is: Using only real analysis, is it possible to calculate this integral? If the answer is yes, how? What makes $x-\arctan (x/2)$ show up instead of $x/2$ (and the same for only $\arctan (x/2)$)?

Your antiderivative is correct for $\frac{|x|}2\le \pi/2$.  Thus, 
$$\begin{align}
\int_0^{2\pi}\frac{1-a\cos(x)}{a^2+1-2a\cos(x)}\,dx&=\int_{-\pi}^\pi \frac{1-a\cos(x)}{a^2+1-2a\cos(x)}\,dx\\\\
&=\left.\left(\frac x2+\text{sgn}(1+a)\text{sgn}(1-a)\arctan\left(\left|\frac{a+1}{a-1}\right|\tan(x/2)\right)\right)\right|_{-\pi}^\pi\\\\
&=\pi\left(1+\text{sgn}(1+a)\text{sgn}(1-a)\right)\\\\
&=\begin{cases}2\pi&,|a|<1\\\\0&,|a|>1\end{cases}
\end{align}$$
as expected!
