-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaylor-series.c
64 lines (54 loc) · 1.61 KB
/
taylor-series.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Question 1: Taylor Series //
// 7 February 2020 //
// Author: Anna DeVries //
/* Libraries */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Defs */
#define NTAYLOR 30
/* Main */
int main( int argc, char *argv[] ){
// Local variables
float x_1;
float fx_1;
float intermediate_1;
float factorial_1 = 1;
double x_2;
double fx_2;
double intermediate_2;
double factorial_2 = 1;
// Check to ensure proper program execution
if ( argc < 2 || argc > 3){
printf("Usage: ./<filename> <initial value of x>\n");
return 1;
}
// Define local variables
x_1 = atoi(argv[1]);
x_2 = atoi(argv[1]);
// Compute as float
fx_1 = x_1 + 1;
for(int i = 1; i <= NTAYLOR; i++){
intermediate_1 = pow(x_1,i);
for(int j = 1; j <= i; j++){
factorial_1 = factorial_1 * j;
}
intermediate_1 = intermediate_1 / factorial_1;
fx_1 += intermediate_1;
}
// Print off float values
printf("Computed Float Value: %.20f Initial x value: %f\n", fx_1, x_1);
// Compute as double
fx_2 = x_2 + 1;
for(int i = 1; i <= NTAYLOR; i++){
intermediate_2 = pow(x_2,i);
for(int j = 1; j <= i; j++){
factorial_2 = factorial_2 * j;
}
intermediate_2 = intermediate_2 / factorial_2;
fx_2 += intermediate_2;
}
// Print off double values
printf("Computed Double Value: %.60lf Initial x value: %lf\n", fx_2, x_2);
return 0;
}