Calculating an improper integral as a limit of a sum. This question arose from a solution I saw yesterday: 
Suppose f is continuous on (0,1] and has an infinite discontinuity at 0.
If the improper integral $\int_0^1 f(x) dx$ converges, is it always the case that:
$$\int_0^1 f(x) dx = \displaystyle\lim_{n\to\infty}\sum_{i=1}^{n}f\left(\frac{i}{n}\right)\frac{1}{n}$$ and, if so, how does one justify this equality?
 A: To build on David Mitra comment above, imagine a function $f$, continuous on $(0,1]$, such that for any $n\geq 2$ (say) there is a very sharp triangular "spike" centered at $\frac{1}{n}$}: $$
f\left(\frac{1}{n}\right) = 2n^2 \qquad f\left(\frac{1}{n}\pm\frac{1}{2n^4}\right) = 0
$$
and affine between each $\frac{1}{n}$ and $\frac{1}{2n^4}$. Outside these spikes, $f$ is identically $0$.
Then it is easy to see that $f$ is continuous on $(0,1]$, takes arbitrary large values on any neighborhood of $0$ (that is, $\limsup_{0^+}f = \infty$), yet $\int_{\downarrow0}^1 f$ exists and is equal to the area beneath thoses spikes, which happens to be $\sum_{n=2}^\infty \frac{2n^2}{2n^4} = \frac{\pi^2}{6}-\frac{3}{2}\simeq 0.145$:
$$\int_\varepsilon^1 f \xrightarrow[\varepsilon\to 0^+]{} \frac{\pi^2}{6}-\frac{3}{2} < \infty $$
however,
$$
\frac{1}{n} \sum_{k=1}^n f\left(\frac{k}{n}\right) \geq \frac{1}{n} f\left(\frac{1}{n}\right) = 2n \xrightarrow[n\to\infty]{}\infty
$$
A: According to Polya and Szego "Problems and Theorems in Analysis, Vol. I, Ch. 1, $\S$ 3, Pr. 20"

Let $f$ be monotone on the interval $(0,1)$. It need not be bounded on $x=0$, $x=1$. If the improper integral $$\int_0^1 f$$ exists then $$\lim_{n\to\infty}\frac 1n \sum_{k=1}^{n-1}f\left(\frac kn\right)=\int_0^1f$$

P Assume $f$ is increasing. Then $$ \int_{0}^{1-\frac 1n}f\leq \lim_{n\to\infty}\frac 1n \sum_{k=1}^{n-1}f\left(\frac kn\right)\leq \int_{\frac 1 n}^1f$$
The condition that the function be monotonic is essential only in a neighborhood of the singularities.
A: This answer is to show (as requested by the OP in a comment) that, if $f$ is assumed to be monotone decreasing, then in fact the equality holds between the integral and the limit of the sum. First note we may assume $f(x)>0$ on $(0,1]$ since adding a constant to $f$ adds the same constant to both the integral and to each of the sums. (This assumption might be avoided, but makes the proof run smoother at a certain point.)
Because $f$ is decreasing on $[1/n,1]$ a diagram shows that
$$(1/n)\sum_{k=2}^n f(k/n) \le \int_{1/n}^1 f(x) dx \le (1/n)\sum_{k=1}^{n-1} f(k/n).$$
(The left side is the area of rectangles underneath $f$, while the right side is the area of rectangles containing the area under $f$.) Define $A_n=\int_0^{1/n}f(n),$ and note that the assumption that $\int_0^1 f(x)dx$ is finite means that $A_n \to 0$ as $n \to \infty.$ We now add $A_n$ to all  three terms, at the same time adding and subtracting a term to the two sums so as to compare them to $S_n=(1/n)\sum_{k=1}^nf(k/n),$ obtaining
$$ A_n+S_n-(1/n)f(1/n) \le \int_0^1 f(x)dx \le A_n+S_n-(1/n)f(1).$$ Using $I$ for the value of the integral in the middle here, we may rearrange to obtain bounds on $S_n$ as
$$I-A_n+(1/n)f(1) \le S_n \le I - A_n + (1/n)f(1/n). \tag{1}$$
Note here that the left term of this inequality is smaller than the right since $f$ is monotone decreasing; if we show that each side approaches $I$ then our proof finishes via the "sandwich theorem". 
It's clear the left side of (1) tends to $I$, and since $A_n$ tends to $0$ we are left with showing that $(1/n)f(1/n) \to 0$ as $n \to \infty$ But since $f$ is monotone decreasing the term $(1/n)f(1/n)$ is the area of a rectangle lying below $f$ on the interval $[0,1/n]$, so that in fact $(1/n)f(1/n) \le A_n$, and the latter approaches $0$ as $n \to \infty.$ This finishes the proof on use of the sandwich theorem as mentioned above.
NOTE: Peter Tamaroff's answer references a proof by Polya and Szego of the result without the extra assumption $xf(x)$ monotone decreasing, which I had made in a previous version. The now adjusted form of the argument here is essentially the same as the Polya/Szego proof.
