Why does the residue method not work straight out of the box here? I'm trying to evaluate the integral $$I = \int_0^{\infty} \frac{\cos(x)-1}{x^2}\,\mathrm{d}x $$ The way I've done this is by rewriting $\frac{\cos(x)-1}{x^2}$ as $\Re\left[\frac{e^{iz}-1}{z^2}\right]$, and then using the residue method to get $$I = \Re\left[\frac{i\pi}{2}\cdot\mathrm{Res}\left(\frac{e^{iz}-1}{z^2}; 0\right)\right]$$ It's clear from the series expansion of $e^{x}$ that the residue evaluates to $i$, which gives us an answer of $-\frac{\pi}{2}$. My question is, why was it necessary to change $\frac{\cos(x)-1}{x^2}$ to a complex-valued function? I see that if I attempt to jump straight to the residue theorem before converting the function, I end up with a residue of $0$, which is clear from the series expansion of $\cos(x)$, which gives $$\frac{\cos(x)-1}{x^2} = -\frac{1}{2}+\frac{x^2}{24}+\cdots$$ But, still, why doesn't the residue theorem immediately apply in this example?

$\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\on}[1]{\operatorname{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
With $\ds{\epsilon > 0}$, I'll "close" the integration around a quarter circle in the first quadrant of the complex plane:

*

*Its radius $\ds{\to \infty}$

*An indent $\ds{\pars{~\mbox{arc of radius}\ \epsilon~}}$ around the origen the coordinates is included.

*The above mentioned contour doesn't include any pole.

Namely,
\begin{align}
&\bbox[5px,#ffd]{\int_{\epsilon}^{\infty}
{\expo{\ic x} - 1 \over x^{2}}\,\dd x}\ =\
\overbrace{\left.-\
\lim_{R \to \infty}\,\int_{\theta\ =\ 0}^{\theta\ =\ \pi/2}{\expo{\ic z} - 1 \over z^{2}}\,\dd z\,
\right\vert_{\ z\ =\ R\exp\pars{\ic\theta}}}^{\ds{0}}
\\[2mm] &\
-\int_{\infty}^{\epsilon}{\expo{\ic\pars{\ic y}} - 1 \over \pars{\ic y}^{2}}\,\ic\,\dd y -
\int_{\pi/2}^{0}{\expo{\ic\epsilon\expo{\ic\theta}} - 1 \over
\epsilon^{2}\expo{2\ic\theta}}\epsilon\expo{\ic\theta}\ic
\,\dd\theta
\\[5mm] \stackrel{\mrm{as}\ \epsilon\ \to\ 0^{+}}{\sim}\,\,\,&
-\ic\int_{\epsilon}^{\infty}{\expo{-y} - 1 \over  y^{2}}\,\dd y -
\int_{0}^{\pi/2}\,\dd\theta
\\[5mm] = &\
-\ic\int_{\epsilon}^{\infty}{\expo{-y} - 1 \over  y^{2}}\,\dd y -
{\pi \over 2}
\end{align}
Then,
\begin{align}
& \mbox{}
\\ &\ \lim_{\epsilon \to 0^{+}}\,\,\,\Re
\int_{\epsilon}^{\infty}{\expo{\ic x} - 1 \over x^{2}}\,\dd x =
\bbox[5px,#ffd]{\int_{0}^{\infty}{\cos\pars{x} - 1 \over x^{2}}
\,\dd x} =
\bbx{-\,{\pi \over 2}} \\ &
\end{align}
