My C++ program has trouble calculating this series for Euler's number

2

enter image description here

Here is the C++ program i wrote to solve the above series:

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int factorial(int a)
{
    if (a > 1)
        return a * factorial(a - 1);
    else
        return 1;
}

float series(float x, int n, float b)
{
    if (abs(pow(x, n) / factorial(n)) < pow(10, -6) || abs(pow(x, n) / factorial(n)) == pow(10, -6)) { return b; }
    else return b = (pow(x, n) / factorial(n)) + series(x, n + 1, b);
}

int main()
{
    float x;
    cout << "Enter x: "<<endl;
    cin >> x;
    cout << "E^x = " << series(x,0,0);
    system("pause");
    return 0;
}

It works fine when abs(x) < 2 but when abs(x) >= 2 this error appears:

Unhandled exception at 0x00F02539 in 33b.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00F22FF8). occurred enter image description here

I want to know why does this happen and how can i fix it?

c++
visual-c++
stack-overflow
series
eulers-number
asked on Stack Overflow Jan 29, 2018 by Trần Đức Hiếu • edited Jan 29, 2018 by Trần Đức Hiếu

2 Answers

3

Your problem is too deep recursion. Consider loop instead.

float series(float x)
{
    const float epsilon = 1e-6f;
    double error = 1;
    double res = 1.f;
    int iter = 1;
    while (abs(error) > epsilon) {
        error *= (x / iter++);
        res += error;
        cout << error << endl;
    }
    return res;
}

int main()
{
    cout << "E^x = " << series(3);
    system("pause");
    return 0;
}

To be clearer about what happens:

When you call a function inside another function, the context of the parent function is saved to make room for the new context. When you make millions of inception, the memory stack in charge to save these context is full and overflows.

This is a Stack Overflow.

answered on Stack Overflow Jan 29, 2018 by Yola • edited Jan 29, 2018 by Benjamin Barrois
1
 #include <iostream>
 #include <cmath>
 #include <cstdlib>
 using namespace std;
 int factorial[200];
 int Factorial(int a)
 {    if(a>0){
     factorial[a]=a * factorial[a-1];
         return factorial[a];
    }
    else
    factorial[a]=1;
    return 1;

 }

 double series(double x, int n, double b)
 {   double temp=(abs(pow(x, n)) / Factorial(n));
     if (temp <= 0.000001) { return b; }
     else return (temp + series(x, n + 1, b));
 }

 int main()
 {
     float x;
     cout << "Enter x: "<<endl;
     cin >> x;
     cout << "E^x = " << series(x,0,0);
     system("pause");
     return 0;
 }

umm this solution is working. all i did was i took your code removed abs(pow(x, n) / factorial(n)) wherever its repeating and intialised to a new variable temp. then instead of < || == u can directly put <=. and rather than invoking a a function to calculate .000001 every time you could just give that value to reduce time further. however i believe that the reason why the code may not have worked is too much recursion. so for factorials i used dynamic programming to reduce its complexity. the above code is working perfectly fine.

answered on Stack Overflow Jan 29, 2018 by Varun Krishna

User contributions licensed under CC BY-SA 3.0