I have seen similar questions about specific cases (see here for $f(x) = \frac{1}{\sqrt{x}}$ and here for $f(x) = \frac{1}{x^a}$). I would like to find when $$\lim_{n \to \infty} \left(\sum_{k=1}^{n}f(k)-\int_{1}^{n+1}f\left(x\right)dx\right)$$
converges. I know that one possible way for this to converge is if $\int_1^{\infty}f(x)dx \leftrightarrow \sum_{k=1}^{\infty} f(k)$ converges. However, this is not the only way for it to converge such as with $f(x)= \frac{1}{x^a}, 0 \le a \le 1$. We can split the integral into parts so that the limit becomes $$\sum_{k=1}^{\infty}\left(f(k)-\int_k^{k+1} f(x)dx \right)$$
The summand must approach $0$ for the sum to converge. I believe this only happens if $f(x)$ is a function approaching $0$. However, this probably is not sufficient for the sum to converge. This brings me to my main question:
What conditions on $f(x)$ are necessary and sufficient for $\lim_{n \to \infty} \left(\sum_{k=1}^{n}f(k)-\int_{1}^{n+1}f\left(x\right)dx\right)$ to exist?
Edit: as jvc pointed in the comments, it is not necessary for $f(x)$ to approach zero, such as with $f(x)$ a constant. I now think it is necessary (perhaps even sufficient) for $f'(x)$ to approach $0$, not necessarily $f(x)$.