Why does $\int_{-\infty}^{+\infty }\arctan\left(\frac{1}{1+x^2}\right)dx$ have a real value when the indefinite integral uses $i$? WolframAlpha gives a real closed form for this definite integral:
$$\int_{-\infty}^{+\infty } \arctan\left(\dfrac{1}{1+x^2}\right)dx = \sqrt{2\left(\sqrt{2}-1\right)}\;\pi$$
Yet, the formula it gives for the indefinite integral uses $i$.
$$\int \tan^{-1}\left(\frac{1}{x^2 + 1}\right) dx = x \tan^{-1}\left(\frac{1}{x^2 + 1}\right) 
+ 2 \left(
\frac{\tan^{-1}\left(\frac{x}{\sqrt{1 - i}}\right)}{(1 - i)^{3/2}} 
+ 
\frac{\tan^{-1}\left(\frac{x}{\sqrt{1 + i}}\right)}{(1 + i)^{3/2}}
\right) + C$$

Why isn't the definite integral non-real? 

 A: WolframAlpha, though handy, is not yet advanced to perform certain integrals satisfactorily. For less familiar integrands, it tends to fall back to complex variables, leading often to uninteresting, perhaps only symbolically useful, results.
The integral in question can be integrated in real variables explicitly
\begin{align}
\int \tan^{-1}\frac{1}{1+x^2}\ dx &
= x\tan^{-1}\frac{1}{1+x^2}\\
&\>\>\>+\sqrt{2(\sqrt{2}-1)}\tan^{-1}\frac{x^2-\sqrt{2}}{x\sqrt{2(\sqrt{2}+1)}} \\
& \>\>\>- \sqrt{2(\sqrt{2}+1)}\tanh^{-1}\frac{x^2+\sqrt{2}}{x\sqrt{2(\sqrt{2}-1)}}+C
\end{align}
which eludes WA due to limitations in its current algorithm.
A: First of all, it is real because it converges and because $\arctan\left(\frac1{1+x^2}\right)$ is a real number for every $x\in \mathbb R$.
On the other hand, that some number can be represented with an expression involving $i$ doesn't mean it is not a real number. For instance,
$$\frac1i+i=0\in \mathbb R.$$
A: A pretty integral, and I'm not sure I would be able to guess the primitive right away.
Still, substitution is always a good way to simplify things:
$$I=\int_a^b \arctan\left(\dfrac{1}{1+x^2}\right)dx, \\ 0 \leq a<b$$

$$x= \tan t$$
$$I=\int_{\arctan a}^{\arctan b} \arctan\left(\cos^2 t\right)\frac{dt}{\cos^2 t}$$
$$u = \cos t$$
$$I=\int_{\cos \arctan b}^{\cos \arctan a} \arctan\left(u^2 \right)\frac{du}{u^2 \sqrt{1-u^2}}$$
$$I=\int_{1/\sqrt{1+b^2}}^{1/\sqrt{1+a^2}} \arctan\left(u^2 \right)\frac{du}{u^2 \sqrt{1-u^2}}$$
$$v=u^2$$
$$I=\frac{1}{2} \int_{1/(1+b^2)}^{1/(1+a^2)} \arctan v \frac{dv}{v^{3/2} \sqrt{1-v}}$$
Now we can use integration by parts:
$$\int \frac{1}{2} \frac{dv}{v^{3/2} \sqrt{1-v}}=-\frac{\sqrt{1-v}}{\sqrt{v}}$$
$$(\arctan v)'= \frac{1}{1+v^2}$$
$$I=-\sqrt{\frac{1}{v}-1} \arctan v \bigg|_{1/(1+b^2)}^{1/(1+a^2)}+ \int_{1/(1+b^2)}^{1/(1+a^2)} \frac{\sqrt{1-v} ~dv}{\sqrt{v} (1+v^2)}$$

$$I=b \arctan \frac{1}{1+b^2}-a \arctan \frac{1}{1+a^2} + 2 \int_{1/\sqrt{1+b^2}}^{1/\sqrt{1+a^2}} \frac{\sqrt{1-u^2} ~du}{1+u^4}$$

For the second part we get back to trigonometric functions again:
$$\int_{1/\sqrt{1+b^2}}^{1/\sqrt{1+a^2}} \frac{\sqrt{1-u^2} ~du}{1+u^4}=\int_{\arcsin(1/\sqrt{1+b^2})}^{\arcsin(1/\sqrt{1+a^2})} \frac{\cos^2 w}{1+\sin^4 w} dw$$
Now we use the tangent half angle substitution:
$$p = \tan \frac{w}{2} \\ \cos w= \frac{1-p^2}{1+p^2} \\ \sin w= \frac{2p}{1+p^2} \\ dw = \frac{2dp}{1+p^2}$$
Denoting:
$$\alpha= \tan \left(\frac{1}{2} \arcsin \frac{1}{\sqrt{1+a^2}} \right) \\ \beta =  \tan \left(\frac{1}{2} \arcsin \frac{1}{\sqrt{1+b^2}} \right)$$

$$\int_{1/\sqrt{1+b^2}}^{1/\sqrt{1+a^2}} \frac{\sqrt{1-u^2} ~du}{1+u^4}=\int_{\beta}^{\alpha} \frac{2 (1-p^2)^2 (1+p^2) }{(1+p^2)^4+16 p^4} dp$$

Now we have a rational function under the integral, which can be integrated using partial fractions.
I will stop here, because the point was, we don't really need CAS to integrate this, and no complex numbers were harmed in the process.
Well, it might be much easier to do the partial fractions in the last integral using complex numbers. But I'm sure we can avoid them.
Note that for the case in the OP, we have:
$$a=0, b=\infty$$
Which means we need to find:
$$\int_0^1 \frac{ (1-p^2)^2 (1+p^2) }{(1+p^2)^4+16 p^4} dp= \frac{1}{4} \sqrt{\frac{\sqrt{2}-1}{2}} ~ \pi$$
I confirmed this value numerically, but Mathematica can't find it from the integral.
Though it does find:
$$\int_0^\infty \frac{ (1-p^2)^2 (1+p^2) }{(1+p^2)^4+16 p^4} dp= \frac{1}{2} \sqrt{\frac{\sqrt{2}-1}{2}} ~ \pi$$
To be honest, complex residues seems like the best method in this case to me.
