|Re: Base of Natural Logarithms|
Message #16 Posted by Thomas Okken on 14 Aug 2007, 9:34 a.m.,
in response to message #15 by Vincze
Dealing with infinity can be tricky, and there is no one answer for all situations. In the case of the series for e, the key insight is that the terms get progressively smaller as n increases, and that in fact, they shrink so quickly that you only need to sum a few in order to reach maximum precision. 1/16! = 4.78e-14, which is too small to affect the value of e when working with 12-digit precision -- so after summing only 16 terms, you're done. If you're working with higher precision, you'll need more terms, of course.
You can work out in advance when the terms will become too small to affect the result any further, or you can simply compare each iteration's result to the previous one, and terminate the loop when there is no further change. And if you want the best possible accuracy, you can sum the series from smallest terms to largest (that requires working out which is the smallest term in advance), to minimize the effect of round-off error on the final result.
Note that the series for e (or generally speaking, for e^x, and the related series for sin and cos) have the nice property that the terms shrink very quickly, but this is not true for all series, like the notoriously useless tan(x) = 1 - x/3 + x/5 - x/7... In such cases you'll have to look for a more clever approach.