Executing Error

Hello! This program is supposed to calculate an estimation of e to the power of x, but as soon as you enter the value of x, nothing happens. The program does not even end, I think it gets stuck in a while-loop, or it is just taking a really long time calculating the answer. What do you think and how do I fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
using namespace std;

int faculty(int z){
    if(z<=0)
        return 1;
    else
        return z * faculty(z-1);
}

double e(int y){
    int k=1;
    double sum = 0;
    while(pow(y,k)/faculty(y)>pow(10,-7)){
        sum += (pow(y,k)/faculty(y));
        k++;
    }
    return sum;
}

int main(){
    int x;
    double answer;
    cout << "-= e to the power of x estimation =-\nEnter x: "; cin >> x;
    answer = e(x);
    cout << answer;
    return(0);
}
Last edited on
This expression

pow(y,k)/faculty(y)

can only increase because faculty( y ) is a constant that is not changed and pow( y, k ) increases together with increasing k when y is positive.
Last edited on
This expression pow(10,-7) can be written simply as the number 1e-7

The faculty (usually called factorial) function is called with the wrong variable.

The function double e(int y) is defined with an integer parameter. Normally ex is calculated where x is a floating-point value rather than an integer.

Something to think about: neither the factorial nor the pow functions are required in this program, as each term of the series may be derived from the previous by one multiply and one divide operation. Often it is a condition of completing this exercise that the cmath library is not to be used.
Last edited on
@ Chervil

I tried redoing the program in the way you described. But it still doesn't work, can someone find what's wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

double e(int y){
    double sum=1;
    int k = 1;
    bool cp = true;
    while(cp){
        int sum1=1, sum2=1;
        for(int z=0;z<k;z++)
            sum1*=y;
        for(int v=1;v<=k;k++)
            sum2*=v;
        if(sum1/sum2<10e-7)
            cp=false;
        else{
            k++;
            sum+=sum1/sum2;
        }
    }
    return sum;
}

int main(){
    int x;
    double answer;
    cout << "-= e to the power of x estimation =-\n\nEnter the value of x: "; cin >> x;
    answer = e(x);
    cout << answer;
}
Never mind, I got it to work now, I still don't get why it didn't work before though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

double e(int y){
    double sum=1;
    int k = 1;
    bool cp = true;
    while(cp){
        double sum1=1, sum2=1, v=1;
        for(int z=0;z<k;z++)
            sum1*=y;
        while(v<=k){
            sum2*=v;
            v++;
        }
        if(sum1/sum2<10e-7)
            cp=false;
        else{
            k++;
            sum+=sum1/sum2;
        }
    }
    return sum;
}

int main(){
    int x;
    double answer;
    cout << "-= e to the power of x estimation =-\n\nEnter the value of x: "; cin >> x;
    answer = e(x);
    cout << answer;
}
A couple of comments. Well done for getting this to work without the use of <cmath>. The reason why this is useful to try is that the functions in that library are themselves written using techniques such as this, though highly optimised and tuned for the purpose.

A small point, you have used 10e-7 as the limit, that is 0.000001 while the original code had pow(10,-7) or 0.0000001. Notice these values are different. Perhaps you are not familiar with the use of scientific notation such as
1.234e3  1234
1e2       100
1e1        10
1e0         1
1e-1        0.1
1e-2        0.01

As for your code, you took some of my suggestion but missed the part where each term can be derived from the previous term in a simple way.

This is my rather quick attempt at the function, notice there is only one loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double e(double y)
{
    double k    = 1;
    double sum  = 0;
    double term = 1;

    while (term > 1e-7)
    {
        sum += term;
        term = term * y / k;
        k++;
    }

    return sum;
}

Last edited on
You did it almost exactly the way my teacher did it, great minds think alike
Last edited on
Topic archived. No new replies allowed.