How can we say that if $a+bi=c+di$ then $a=c$ and $b=d$, where $a,b,c,d$ are one-variable functions? I have been reading Paul Nahin's book and in the last chapter (page number 213), he writes
$$\frac{\pi}{b}e^{-ab}=\int_{-\infty}^{\infty}\frac{\cos(ax)}{b^2+x^2}\mathrm{d}x+i\int_{-\infty}^{\infty}\frac{\sin(ax)}{b^2+x^2}\mathrm{d}x$$
implies that
$$\int_{-\infty}^{\infty}\frac{\cos(ax)}{b^2+x^2}\mathrm{d}x=\frac{\pi}{b}e^{-ab}$$ and $$\int_{-\infty}^{\infty}\frac{\sin(ax)}{b^2+x^2}\mathrm{d}x=0$$
But earlier in the same chapter he shows that $$\int_{-1}^{1}\frac{\mathrm{d}x}{x}=i\pi$$
So, how can we compare the real and imaginary parts in the first equation, when we don't know whether the integrals are real or imaginary?
 A: The integrals in the first equation are integrals of real functions over the real axis. So (if they converge) they converge to real numbers.
(That the second integral is $0$ is clear since it's the integral of an odd function.)
A: I've just had a look at the book, it says

if we change variables to $x = -e^{i\theta}$, with $0\leq \theta \leq \pi$ radians, then the integral becomes
$$
I=\int_{0}^{\pi} \frac{-i e^{i \theta} d \theta}{-e^{i \theta}}=\int_{0}^{\pi} i d \theta=i \pi
$$

And I doubt the correctness of the variable substitution. Say, the substitution can lead to the extra imaginary part, and it doesn't really equal to the originial one. For example, we take $\theta=\dfrac \pi4$, then $e^{-i\frac{ \pi}{4} }=\dfrac{1-i}{\sqrt{2}}$ is not a real number, while $x$ in the originial integral must be real ($x\in[-1,-1]$).
Generally speaking, the integral $$\int_{-1}^{1}\frac{\mathrm{d}x}{x}$$ does not converge. If we talk about cauchy principal value, we can have it as
$$
V.P. \int_{-1}^1\dfrac1xdx=0
$$
Return to your question,

when can we compare the real and imaginary parts

The answer is when all the integrals converge, that is, integrals of real functions over the real axis converges to real numbers.
