Newer
Older
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
65
66
#include <stdio.h>
#include <time.h>
#define NUM_STEPS 1e9
static double
e_func (void)
{
int i;
double e = 1;
double factorial = 1;
printf ("e started\n");
for (i = 1; i < NUM_STEPS; i++)
{
factorial *= i;
e += 1.0 / factorial;
}
printf ("e done\n");
return e;
}
static double
pi_func (void)
{
int i;
double pi = 0;
printf ("pi started\n");
for (i = 0; i < NUM_STEPS * 10; i++)
{
pi += 1.0 / (i * 4.0 + 1.0);
pi -= 1.0 / (i * 4.0 + 3.0);
}
pi = pi * 4.0;
printf ("pi done\n");
return pi;
}
int
main (void)
{
double start, stop; /* times of beginning and end of procedure */
double e, pi, product;
/* start the timer */
start = clock ();
/* First we calculate e from its taylor expansion */
e = e_func ();
/* Then we calculate pi from its taylor expansion */
pi = pi_func ();
product = e * pi;
stop = clock ();
printf ("Reached result %f in %.3f seconds\n", product, (stop-start) / CLOCKS_PER_SEC);
return 0;
}