I am going through the exercises of this old C programming book, that
doesn't have the answer in the back. Which is good.
However I am not understanding a particular question.
It says:

Write a program that estimates the value of the mathematical constant e by using the formula:

e = 1 + 1/1! + 1/2! + 1/3! + ...

I understand that 1!, 2!, 3! are the factorial of those numbers. But what
throw me off is how far should I go computing. Notice that is + ....
And if it is a constant how come it can be change?.

Anyone mind to show me some light on the matter?. I'm quite sure I can resolve the equation only if I knew what is being ask of me.

Member Avatar for iamthwee

Ideally you would go on until you reach infinity. Since you can't obviously do that you have to reach a compromise.

And remember factorial grows quickly.

The expression you just posted is an infinite expression.

My understanding would be that continue till the limit of your data type is reached. When double is used for factorial computations, the limit is 170. Take a look at this:

#include <iostream>
#include <cmath>

double factorial (double value)
{
    double result = 1.0;
    while (fabs (value) > 0)
    {
        result *= value--;
    }
    return result;
}

int main ()
{
    using namespace std;

    double E = 1.0;

    for (long i = 1; i <= 170; ++i)
    {
        double temp = factorial ((double)i);
        cout << "\nFactorial " << i << ": " << temp;
        E += (1.0 / temp);
    }
    cout << "\n\nThe value of E (approx.) = " << E;

    getchar ();
    return 0;
}

But practically it makes no sense to continue beyond the factorial of 8 since the answer remains more or less the same.

If using the value of e in some scientific calculation, it would make more sense to keep it as a constant value rather than calculating it.

const double E =  2.71828182845904523536028747135266249775724709369995
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.