Euler's Number/Taylor Series

Hey all, so I have a homework assignment(I don't want the code written for me) in which I have to calculate Euler's Number using the Taylor Series approximation. Here is a link to the assignment on Google Docs

https://docs.google.com/fileview?id=0B638NqgsIFa4NWYwYTVlYTgtYTYwZi00NWNkLWE5OWYtYTM4YzA4OTg1ZTkz&hl=en

I guess I'm having trouble trying to figure out how to control the factorial loop without an upper or lower bound. I've tried passing the accuracy from the console input and having while((1./fact)>Acc) but it isn't working properly. Acc being the accuracy. Any ideas or general nudges in the right direction that anyone can give me? Following code is still a huge work in progress.

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
36
37
38
39
40
41
42
43
44
45
//Steve Osborne, Using the Taylor Series

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

void Input(double& Acc,int& code);
double Factorial(double fact,double Acc);

int main(){
	double Acc;
	int code=1;
	double fact=1;
	cout<<"Steve Osborne, Program 12"
		<<"\nEuler's Number by Maclaurin Series";
	Input(Acc,code);
	while(code==1){
		Factorial(fact,Acc);
		Input(Acc,code);
		cout<<"\n\n\n"<<fact;
	}
	return 0;
}
void Input(double& Acc,int& code){
	cout<<"\n\nPlease enter the precision of e in this format: 1E-9"
		<<"\nEnter the precision of e or 0 to quit: ";
	cin>>Acc;
	if(Acc==0){
		cout<<"\n\nGoodbye\n\n";
		code=0;
	}
	else{
		code=1;
	}
	return;
}
double Factorial(double fact,double Acc){
	int count=1;
	while((1./fact)>Acc){
		fact*=count;
		count++;
	}
	return fact;
}
Last edited on
The accuracy of the Taylor series isn't in the resulting number, but in the number of iterations that you calculate the expansion.

Here is the formula for ex:
http://en.wikipedia.org/wiki/E_%28mathematical_constant%29#Complex_numbers
This is what you are trying to use (where x==1). The accuracy is how high you want n to get (see the Sigma-notation at the end):
  ∞   xn
  Σ  ───
 n=0   n!

Your factorial function should start out with a sum of '1', then recurse using the Taylor expansion.

BTW, "Factorial" is a terrible name for your function. You are not calculating the factorial with it. (It calculates one inside, but this I would put in another function.)

Hope this helps.
Wow, that makes alot of sense now. Thanks alot, I was looking at it from the opposite POV. The function is called Factorial because that's all I have it do, factorial of any integer I passed to it. The rest of it was me just testing my output. I actually have to make a summation function that calls the factorial one. Thanks for the help man
Topic archived. No new replies allowed.