Well, let f(i) is i-th member without sign (it's not C).
f(i) = i/i! = i/(1*2*...*(i-1)*i) =
i/(i*(1*2...*(i-1)) = 1/(i-1)!
f(i+1) = 1/i! = 1/((i-1)!*i) = f(i)/i
i = 1, 2,... (0! = 1 by definition)
In other words, if we know i-th member of series then the next member is equal to this member / i (ignore sign).
if we know that i-th member is positive then the next member is negative and vice versa.
We know that 2-nd member is equal to -1.0 (negative). We know that the partial sum of the series is equal to 1.0 at this moment: f(1) == 1/1!...
Now we must define calculation loop condition. Obviously no sense to continue calculations when the next member of series is too small: double type precision is ~16 decimal digits. We want to stop calculations when f(i) < eps, where eps == 1e-16 (for example).
Yet another tip: no need in int variables at all!
double Series()
{
const double eps = 1e-16;
double sum = 1.0; /* sum(1) */
double f, i;
int neg = 1;/* the 2nd member < 0 */
for (f = 1.0, i = 2.0; f > eps; f /= i, i += 1.0) {
/* cout << i << '\t' << f << '\t'; */
sum += (neg? -f: f);
/* cout << setprecision(15) << sum << '\t'
<< (sum?1/sum:0) << '\n'; */
neg ^= 1;
}
/* cout << sum << endl; */
return sum;
}
The answer: 0.367879441171442
It's equal to 1/e constant. Exactly.