Does $\int^{\infty}_0 \frac{\cos x}{1+x}\,\mathrm dx$ converge? The integral
$$\int^{\infty}_0 \frac{\cos x}{1+x}\,\mathrm dx$$
is shown to be equal to
$$\int^{\infty}_0 \frac{\sin x}{{(1+x)}^2}\,\mathrm dx$$
through integration by parts. The latter one converges absolutely (i.e the integral still exists if you take the absolute value of the function) as the function is dominated by the function $1/{(1+x)}^2$. But the first integral does not converge absolutely as stated in Rudin "Principles of Mathematical Analysis" ch 6 exercise 9.
Why is it so?
 A: By integrating by parts,
$$
\int_0^M \frac{\cos x}{x+1}\:dx=\frac{\sin M}{M+1}+\int_0^M \frac{\sin x}{(x+1)^2}\:dx, \quad M\ge0,
$$ and letting $M \to \infty$, one has
$$
\left|\int_0^\infty \frac{\cos x}{x+1}\:dx\right|=\left|\int_0^\infty \frac{\sin x}{(x+1)^2}\:dx\right|<\int_0^\infty \frac1{(x+1)^2}\:dx<\infty
$$ the given integral is then convergent, there is no contradiction with
$$
\int_0^M \left|\frac{\cos x}{x+1}\right|\:dx \to \infty
$$ as $M \to \infty$, since we have
$$
 \left|\int_0^M \frac{\cos x}{x+1}\:dx\right| <\int_0^M \left|\frac{\cos x}{x+1}\right|\:dx.
$$

Remark. One may observe that
  $$
\int_{(2n-1)\frac{\pi}2}^{(2n+1)\frac{\pi}2} \left|\cos x\right|\:dx=2,\qquad n=1,2,\cdots,
$$ giving
  $$\int_{(2n-1)\large\frac{\pi}2}^{(2n+1)\large\frac{\pi}2}  \left|\frac{\cos x}{x+1}\right|\:dx \ge \frac1{(2n+1)\large\frac{\pi}2+1}\int_{(2n-1)\large\frac{\pi}2}^{(2n+1)\frac{\pi}2}  \left| \cos x \right| dx \ge \frac{2}{(n+1)\pi}$$
  and by summing
  $$\int_{\large \frac{\pi}2}^{(2N+1)\large \frac{\pi}2}  \left|\frac{\cos x}{x+1}\right|\:dx \geq \frac{2}{\pi}\sum_{n = 2}^{N+1} \frac{1}{n}\qquad N\ge1,$$
  which shows that
  $$\int_0^\infty \left|\frac{\cos x}{x+1}\right|dx$$
  is divergent.

A: The first one does converge, actually. The catch is that if an integral does not converge absolutely, it doesn't mean it is not convergent.
The integration by parts technique is one way to show that it actually is convergent! To understand intuitively why it is so, it is helpful to look at the graph of $f(x) = (\cos x){(1+x)}^{-1}$:

As you can see, the function is alternating between positive and negative values. As the Riemann integral takes into account the sign of the function, all these areas will cancel out each other. In this case, the result is approximately $0.343378$, and that's because the area between the function and the $x$ axis is progressively smaller and smaller.
Conversely, being $|f(x)|$ non-negative, if you integrate it you will be summing all those areas, which are all positive, and the sum does not converge because its general term is asymptotically equivalent to $1/x$, whose sum diverges.
In general, it can be proved that the Riemann integral has the property
$$\left | \int_a^b f(x)\,\mathrm dx \right | \leq \int_a^b \left | f(x) \right |\,\mathrm dx$$
and it can be extended to improper integrals as well.
