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?
 A: The reason is actually that $(\cos{z}-1)/z^2$, the obvious first guess for the complex function, diverges as $|\Im(z)| \to \infty$. (Basically, either $e^{iz}$ or $e^{-iz}$ blows up, depending on the direction you choose.) Therefore you can't use the residue theorem in the usual way, where you persuade the integral over the semicircle to die off as its radius gets large.
On the other hand, $(e^{iz}-1)/z^2$ doesn't have this problem: it is $O(e^{-\Im(z)})$ as $\Im(z)\to\infty$, and Jordan's lemma (or similar) tells us the semicircle integral dies off and we just have to look at the residues.
A: Your principal problem is, as @GEdgar mentioned, is that you failed to refer to a contour.   As a result, you are making assumptions that have no basis in reality.  For example, you don't compute the residue of a pole that lies on your contour.  But since you never defined a contour, you don't know that you have done that.
So let's set up a contour integral that may be appropriate to this problem:
$$\oint_C dz \frac{e^{i z}-1}{z^2} $$
Nominally, we might take $C$ to be a semicircle of radius $R$ in the upper half plane.  However, if we do that, the pole at $z=0$ would lie on the contour, which is not acceptable.  To avoid this, we need to deform the contour to avoid that pole.  We do this by introducing a semicircular detour of radius $\epsilon$ about the origin, into the upper half-plane.  
The next step is to write out the contour integral as a parametrization along each part of the contour.  Along these lines, the contour integral is equal to
$$\int_{-R}^{-\epsilon} dx \frac{e^{i x}-1}{x^2} + i \epsilon \int_{\pi}^0 d\phi \, e^{i \phi} \frac{e^{i \epsilon e^{i \phi}}-1}{\epsilon^2 e^{i 2 \phi}} + \int_{\epsilon}^R dx \frac{e^{i x}-1}{x^2} +i R \int_0^{\pi} d\theta \, e^{i \theta} \frac{e^{i R e^{i \theta}}-1}{R^2 e^{i 2 \theta}}$$
We consider the limit as $R \to \infty$ and $\epsilon \to 0$.  In this limit, the second integral behaves as
$$i \epsilon \int_{\pi}^0 d\phi \, e^{i \phi} \frac{e^{i \epsilon e^{i \phi}}-1}{\epsilon^2 e^{i 2 \phi}} \sim i \epsilon \int_{\pi}^0 d\phi \, e^{i \phi} \frac{1+i \epsilon e^{i \phi}-1}{\epsilon^2 e^{i 2 \phi}} = \pi$$
and the fourth integral vanishes, because its magnitude is
$$ \frac1{R} \int_0^{\pi} d\theta \,  \left |e^{i R e^{i \theta}}-1\right |\sim \frac{\pi}{R}$$
as $R \to \infty$.  This leaves, as the contour integral in this limit, 
$$PV \int_{-\infty}^{\infty} dx \frac{e^{i x}-1}{x^2} + \pi = \int_{-\infty}^{\infty} dx \frac{\cos{x}-1}{x^2} + i PV \int_{-\infty}^{\infty} dx \frac{\sin{x}}{x^2} + \pi $$
where $PV$ denotes the Cauchy principal value.  
Now, note that there are no poles within the contour $C$.  Thus, by Cauchy's theorem, the contour integral is zero.  Equating real and imaginary parts, we find that
$$\int_{-\infty}^{\infty} dx \frac{\cos{x}-1}{x^2} = -\pi$$
A: To actually use the residue theorem, you should write down a contour.  Then check that integral on the part off the real axis goes to zero.
A: $\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}
