Sum of a geometric series whose common ratio might be 1 I want to sum a finite series with a computer but I need to allow the common ratio to be 1 or very close to 1. That means I can't use
$$
\ S_n = \frac{1 - r^n}{1-r}\ 
$$
It needs to work reliably even when r=1.000000000001 or so, so I don't want to define it as a piecewise function.
I tried factorizing the numerator to 
$$
1-r^n = (1-r)(1+r+r^2+...+r^{n-1})
$$
but that just gets back to the original series.
I tried L'Hospital's rule but somehow it ends up with
$$
\ \lim_{r \to a}\frac{1 - r^n}{1-r} = \lim_{r \to a}\frac{-nr^{n-1}}{-1} = \lim_{r \to a} {n r^{n-1}}\ = na^{r-1}
$$
which isn't the same as the sum of the series. EDIT: I see now this is useless because the numerator isn't 0.
Is there a formula for this?
EDIT: The reason I don't want to use the formula is that computers are not good at dividing by very small numbers. Some tiny non-zero values of the denominator will cause divide-by-zero errors.
 A: If I understand correctly: you want to compute the sum of a geometric series $1 + r + \cdots + r^{n-1}$ numerically, using a computer, in a way that works for values of $r$ that are very close to $1$ as well as very different from $1$? In that case, I really don't understand your objection to piecewise functions. Computational implementations of mathematical formulas use piecewise functions all the time, because it's very very common that the best way to numerically evaluate a formula depends on the values of the parameters.
In this case, for $r \approx 1$ I would use the series expansion
$$r^k = [1 + (r - 1)]^k \approx 1 + k(r - 1)\tag{1}$$
Now, you could plug this into the formula for the sum, which gets you
$$\frac{1 - r^n}{1 - r} \approx \frac{1 - [1 + n(r - 1)]}{1 - r} =  n$$
This corresponds to approximating each term of the series as $1$. But if you want something a little more accurate, you could actually apply equation $(1)$ to each term of the series directly:
$$\sum_{k = 0}^{n - 1}r^k \approx \sum_{k = 0}^{n - 1} [1 + k(r - 1)] = n + \frac{n(n - 1)(r - 1)}{2}$$
Whichever approximation you use will only be valid when $r$ is sufficiently close to $1$ that the omitted correction terms are negligible. It's ultimately up to you to decide how close that is, but you can use the magnitude of the first omitted term as a guide to the size of the correction, to decide whether it's acceptable.
For example, if you want to know whether the approximation $\sum_{k = 0}^{n - 1} r^k \approx n$ is accurate enough, you can calculate $n(n - 1)(r - 1)/2$ and see whether it's small enough to neglect. (You decide what "small enough" means.) If so, have your function use the simple approximation of just $n$. If not, then check the next correction term, $n(n - 1)(2n - 1)(r - 1)^2/6$. If that's small enough to neglect, have your function use the approximation $n + n(n - 1)(r - 1)/2$. If not, $r$ is probably far enough from $1$ that dividing by $1 - r$ will not pose a problem, and in that case you can just do the division by $1 - r$ directly.
The only time this might be problematic is when you have $r - 1$ very small, so that dividing by $1 - r$ is inaccurate, but $n$ very large (like, $1/|1 - r|$ or more), so that the correction terms are large. If that's a common case, you can use progressively more and more accurate approximations by adding additional correction terms, beyond the three I mentioned here.
A: Since $r$ is slightly greater than 1, let $x = r-1$, so $x$ is small and positive.  Then your function becomes
$$\frac{1-r^n}{1-r} = \frac{-1}{x} [1-(1+x)^n] = \frac{-1}{x} \left[ 1 - \sum_{i=0}^n \binom{n}{i}x^i \right] = n + \binom{n}{2} x + \binom{n}{3} x^2 + \dots$$
Take a few terms of this series and your computation should be fairly accurate.
A: Are you using floating point arithmetic (float, double), or infinite precision numbers? If the answer is the latter then you don't need any formula other than just doing the math. 
If it's the former, then the idea is you only need to expand the series until the result falls below the precision. 
For example, in IEEE754 double precision numbers, your precision is up to 17 digits, meaning that when you expand the numerator as a series - as you did - you can stop as soon as the next element is 17 powers of ten smaller than the existing result. 
