Question: How to evaluate this integral using residues$$\int_{0}^{\infty} \frac{x \sin x}{1 + x^2} dx$$
I integrate over the entire real axis and dividing it by 1/2 since the integrand is even, and then I do the thing with the turning it into $$-\mathrm{Im}\bigg(\int f\cdot e^{ix} dx\bigg)$$ and integrate over the upper half plane by finding the residues there, and there's only one, at $i$.
So I put it into the equation $\frac{x e^{iz}}{2x}$ and get $\frac{1}{2e}$, so I get when plugging that in
$$\begin{align}\int_{0}^{\infty} \frac{x \sin x}{1 + x^2} dx &= .5 \int_{-\infty}^{\infty} \frac{x \sin x}{1 + x^2} dx \\&= .5 -\operatorname{Im} \Bigg[2\pi i \cdot \operatorname{Res}\bigg(\frac{x e^{ix}}{ 1 + x^2}; i\bigg)\Bigg]\\&= .5 \cdot \bigg[-\operatorname{Im}\Big(2\pi i\cdot \frac{1}{2e}\Big)\bigg] \\&= -\frac{\pi}{2e}\end{align}$$
But the answer's positive.
What did I mess up on?
First consider $$\int_0^\infty \frac{\cos ax}{1 + x^2} dx $$
with $a\geq 0 $. The function $f(z) = \frac{1}{1+z^2}$ has poles at $\pm i$ then take the contour $C_R$ to be the semicircle of radius $R>1$. Then by the Residue Theorem we have
$$\int_{-R}^R \frac{e^{iax}}{1 + x^2} dx + \int_{C_R}\frac{e^{iaz}}{1 +z^2} dz = 2\pi i\, \mathrm {Res}_{z=i} f(z)e^{iaz}$$
Where $$\mathrm{Res}_{z=i} f(z)e^{iaz} = \frac{e^{iaz}}{z+i} \Bigg|_{z=i} = \frac{e^{-a}}{2i}$$
Thus $$\int_{-R}^R \frac{e^{iax}}{1 + x^2} dx = \pi e^{-a} - \mathfrak {Re} \int_{C_R}\frac{e^{iaz}}{1 +z^2} dz$$
Since $|f(z)| \leq M_R = \frac{1}{R^2 -1 }$ we have that
$$\Bigg|\mathfrak {Re}\int_{C_R}\frac{e^{iaz}}{1 +z^2} dz\Bigg| \leq \Bigg|\int_{C_R}\frac{e^{iaz}}{1 +z^2} dz\Bigg| \leq \frac{\pi R}{R^2-1}$$ Then it follows that $$\int_{-\infty}^\infty \frac{\cos ax}{1 + x^2} dx = \pi e^{-a} \implies \int_{0}^\infty \frac{\cos ax}{1 + x^2} dx = \frac{\pi}{2}e^{-a}$$
From this we take $$I(a) = \int_{0}^\infty \frac{\cos ax}{1 + x^2} dx = \frac{\pi}{2}e^{-a}$$ then
$$I'(a) = -\int_{0}^\infty \frac{x\sin ax}{1 + x^2} dx = -\frac{\pi}{2}e^{-a}$$
Now take $a = 1$ to get $$\int_{0}^\infty \frac{x\sin x}{1 + x^2} dx = \color{red} {\frac{\pi}{2e}}$$