Getting "inf" and don't know why

I am trying to compute e^x and have made a factorial function that I verified is correct. The factorial function is not shown. I suspect the error is in the for statement with the **.
The main is just a check, the main outputs "E_to_X(0) is inf should be 1" and "E_to_X(1) is inf should be 2.718"
Much thanks for any help.

double E_to_X(double X) {

double x = X;
double sum = 1 + X;


** for (int i = 0; i <= 20; ++i){
** sum += ((pow(x, i)) / (factorial(i)));
}

return sum;
}

// You can change main() in anyway you want
int main() {

cout << fixed << "E_to_X(0) is " << E_to_X(0) << " should be 1" << endl;
cout << fixed << "E_to_X(1) is " << E_to_X(1) << " should be 2.718" << endl;
Post the definition of factorial.
My guess is their factorial is returning int.

Which is a problem since 20! = 2.432902e+18, which blows away the range of a 32-bit integer.

> have made a factorial function that I verified is correct. The factorial function is not shown.
http://www.catb.org/esr/faqs/smart-questions.html#symptoms


double factorial(int N) {
double return_val = N;

for (int i = 1; i <= N-1; ++i)
return_val *= i;

return return_val;
}

A factorial is a function that multiplies all integers below it, here is the code I used.
Most often shown as a '!' so 5! = 5*4*3*2*1 = 120
factorial() is incorrect. factorial(0) returns 0, but 0! is 1.
> // You can change main() in anyway you want
Yes, and you can change anything else you want if it helps you understand WTF is going on.

It's called debugging!!!
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
33
34
35
#include <iostream>
#include <cmath>
using namespace std;
double factorial(int N)
{
  double return_val = N;

  for (int i = 1; i <= N - 1; ++i)
    return_val *= i;

  return return_val;
}

double E_to_X(double X)
{
  double x = X;
  double sum = 1 + X;

  for (int i = 0; i <= 20; ++i) {
    double powTerm = pow(x, i);
    double factTerm = factorial(i);
    cout << "powTerm=" << powTerm << ", factTerm=" << factTerm << endl;
    sum += powTerm / factTerm;
    cout << "Pass=" << i << ", sum=" << sum << endl;
  }

  return sum;
}

// You can change main() in anyway you want
int main()
{
  cout << fixed << "E_to_X(0) is " << E_to_X(0) << " should be 1" << endl;
  cout << fixed << "E_to_X(1) is " << E_to_X(1) << " should be 2.718" << endl;
}


It's pretty obvious to see where it all goes pear shaped.
My bad about the factorial code, should've posted.
Here is the full code.
I tried changing the factorial interval lower to 5 that it was definitely still in the range of int and still got a inf output.

// Test and develop a version of the exp() function - main() has test
// code to check and see if this version of exp() function works properly.

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

double factorial(int N) {
double return_val = N;

for (int i = 1; i <= N-1; ++i)
return_val *= i;

return return_val;
}

double E_to_X(double X) {

double x = X;
double sum = 1 + X;


for (int i = 0; i <= 5; ++i){
sum += ((pow(x, i)) / (factorial(i)));
}
return sum;
}

int main() {

cout << fixed << "E_to_X(0) is " << E_to_X(0) << " should be 1" << endl;
cout << fixed << "E_to_X(1) is " << E_to_X(1) << " should be 2.718" << endl;
cout << fixed << "E_to_X(2) is " << E_to_X(2) << " should be 7.38" << endl;
cout << fixed << "E_to_X(5) is " << E_to_X(5) << " should be 120" << endl;
cout << fixed << "E_to_X(10) is " << E_to_X(10) << " should be 22026" << endl;

return 0;
}
The way you've written factorial, you'll get a return value of 0 for an input of 0, but the factorial of 0 should be 1.
With a return of 0 you end up dividing by 0 in the exponential code, giving inf.

Also, you should just start sum at 0.
And you don't need the extra x variable. Just use the input parameter.

It's a very inefficient implementation since you could easily keep a running factorial going in the exponential function instead of calculating it each time from scratch in a separate factorial function. You could do the same thing for the power.
Last edited on
Thank you all very much for the help here.

I know that these functions are not optimal, but this is just a learning exercise for me. So just understanding it was the main focus. And I understand it much better now, so thank you again.

Here is the code for anyone's future viewing.

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

double factorial(int N) {
double return_val = N;
if (N == 0){
return_val = 1;
exit;
}
for (int i = 1; i <= N-1; ++i)
return_val *= i;

return return_val;
}

double E_to_X(double X) {

double sum = 0;

for (int i = 0; i <= 20; ++i){
double power = pow(X, i);
double fact = factorial(i);
cout << "pow=" << power << endl <<
" fact=" << fact << endl;
sum += (power / fact);
cout << "i= " << i << ", sum= " << sum << endl;
}
return sum;
}

int main() {

cout << fixed << "E_to_X(0) is " << endl << E_to_X(0) << " should be 1" << endl;
cout << fixed << "E_to_X(1) is " << endl <<E_to_X(1) << " should be 2.718" << endl;
cout << fixed << "E_to_X(2) is " << endl <<E_to_X(2) << " should be 7.38" << endl;
cout << fixed << "E_to_X(5) is " << endl <<E_to_X(5) << " should be 120" << endl;
cout << fixed << "E_to_X(10) is " << endl <<E_to_X(10) << " should be 22026" << endl;

return 0;
}
Last edited on
Please use code tags.

If you want exp(x) - or most other power series for that matter - then the last thing you should do is have a factorial function. As @dutch pointed out, you should keep a running product for each term as each one is a very simple multiple of the previous.

Here,
(rth term) = (previous term) * x / r

Absolutely no need for either a factorial or a power of x.
Topic archived. No new replies allowed.