Integrals like $\int_{-\infty}^\infty\frac{\cos x}{x^2 + 1}dx$ appear in Fourier analysis and in physics. The half-disk method handles them too, with one change: replace $\cos(ax)$ by the exponential $e^{iaz}$, which stays small in the upper half-plane.
🔑 Key idea
For $a > 0$ and a rational $R(x)$ with no real poles that tends to $0$ at $\pm\infty$:
$$\int_{-\infty}^{\infty}R(x)e^{iax}\,dx = 2\pi i\sum_{\operatorname{Im} z_j > 0}\operatorname{Res}\left(R(z)e^{iaz}, z_j\right),$$
and the cosine and sine integrals are its real and imaginary parts (for real $R$). For example, for $k, b > 0$,
$$\int_{-\infty}^{\infty}\frac{\cos(kx)}{x^2 + b^2}\,dx = \frac{\pi e^{-kb}}{b}.$$
💡 Why it works
On the upper half-plane, $z = x + iy$ with $y \ge 0$, so $|e^{iaz}| = |e^{iax}e^{-ay}| = e^{-ay} \le 1$: the exponential does not grow, and when $\deg Q \ge \deg P + 2$ the ML bound kills the arc as before. (When $R$ decays only like $\frac{1}{x}$, a sharper estimate called Jordan's lemma still kills it.) For the example: the upper pole is $bi$, with residue $\frac{e^{ik(bi)}}{2bi} = \frac{e^{-kb}}{2bi}$, and $2\pi i$ times that is $\frac{\pi e^{-kb}}{b}$, a real number, which is the cosine integral (the sine part is $0$ by symmetry).
$\frac{e^{iz}}{z^2 + 1}$ around the half-disk: the arc's contribution fades as $R$ grows, and the total is $\frac{\pi}{e} \approx 1.156$.The same picture with $\cos z$: now the arc grows as you drag the handle out, because $\cos(iy) = \cosh y$ blows up. That is why $\cos z$ is the wrong function to integrate.
Replacing $\cos x$ by $\cos z$. On the real line they agree, but in the upper half-plane $\cos z = \frac{e^{iz} + e^{-iz}}{2}$ contains $e^{-iz}$, whose size is $e^{y}$: it explodes on the arc, and the arc integral does not vanish. Integrate $R(z)e^{iaz}$ (with $a > 0$, so the exponential decays upward) and take the real or imaginary part at the end. If $a < 0$, close in the lower half-plane instead.
Practice
Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.