For valid probability densities $p(x)$ and $q(x), \int p(x)\ln(q(x))~dx = 0$. Can you catch my mistake? I was trying to solve the following integral by parts, $$ \int p(x)\ln(q(x))dx$$ for valid probability density functions $p(x)$ and $q(x)$,
$$ \int p(x)\ln(q(x))dx = \ln(q(x)) - \int\frac{q'(x)}{q(x)}dx,$$ since $p(x)$ is assumed second function in the product and $\int p(x)dx = 1$.
$$ \int p(x)\ln(q(x))dx = \ln(q(x)) - \int\frac{q'(x)}{q(x)}dx = \ln(q(x)) - \ln(q(x)) = 0.$$
Is this wrong even if the limits are assumed to be entire $\mathbb{R}$. I'm sure this is very elementary and I'm missing something very fundamental here.
 A: It would help to specify some bounds here, say $a$ and $b$. Note that assuming the existence of $\ln(q(x))$ requires $q(x)$ to be positive on the interval (while there may be probability densities on the interval that don't have this property, we don't care about them right now).
Let $P$ be an antiderivative of $p$ on $[a,b]$, i.e. a function on $[a,b]$ with $P'(x) = p(x)$ for all $x \in [a,b]$.
By integration by parts with "$u$" taken to be $\ln(q(x))$ and "$dv$" taken to be $p(x)$ we have $du = q'(x)/q(x)$ and $v = P(x)$, so
$$
\begin{align*}
\int_a^b p(x) \ln(q(x)) \, dx & = \ln(q(x)) P(x) \Big \vert_a^b - \int_a^b P(x) \frac{q'(x)}{q(x)} \, dx \\
& = \ln(q(b)) P(b) - \ln(q(a)) P(a) - \int_a^b P(x) \frac{q'(x)}{q(x)} \, dx.
\end{align*}
$$
which looks fine to me, but no cancellation takes place.
The issue is that the integration by parts formula requires an antiderivative of $p$, which is a function, and which is definitely not the number $\int_a^b p(x) \, dx$.  (If this number were an antiderivative of $p$, then $p$ would be constant and not a valid probability distribution.)  The distinction is blurred if you omit bounds everywhere and write $\int p(x) dx$ for both objects.
