-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Taylor_Series.cpp
More file actions
41 lines (37 loc) · 797 Bytes
/
Copy path06_Taylor_Series.cpp
File metadata and controls
41 lines (37 loc) · 797 Bytes
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
#include <iostream>
using namespace std;
// Write a Recursive Function for Taylor Series
double taylor_rec(int x, int n)
{
static double fact = 1.0, power = 1.0;
double result;
if (n == 0)
return 1;
else
{
result = taylor_rec(x, n - 1);
power *= x;
fact *= n;
return result + (power / fact);
}
}
// Write a Iterative Function for Taylor Series
double taylor_iter(int x, int n)
{
double fact = 1.0, power = 1.0;
double result = 1.0;
for (int i = 1; i <= n; i++)
{
power *= x;
fact *= i;
result += (power / fact);
}
return result;
}
int main()
{
double eX_Rec = taylor_rec(1, 10);
double eX_Iter = taylor_iter(1, 10);
cout << eX_Rec << endl
<< eX_Iter << endl;
}