-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmathAlgo.c
87 lines (71 loc) · 2.6 KB
/
mathAlgo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "mathAlgo.h"
/*PI calculation *
* the Following formula calculate 2 arcsin (1) given by the Taylor series *
* where arcsin(x) = *
* x // n = 1 *
* + 1/2 (x^3/3) // n = 2 *
* + (1/2)(3/4)(x^5/5) // n = 3 .... etc *
* + [1/2)(3/4)(5/6)(x^7/7) + ... " *
* and x = 1 for this case *
* N.B: THIS SERIES CONVERGERS VERRRRYYY SLOWLY! for 10,000 terms, *
* pi = 3.130309. I checked this with an online reference. */
void calculatePI(threadData_t *t ,double percentage)
{
double tempResultA = 0;
double tempResultB = 1;
double n = 2; //initial m value
double m = 2; //initial m value
int numTermsExecute = 0;
int totalIterations = 0;
int currentIteration = 1;
//printf("In Calculate PI\n");
// set percentage
if(percentage == 100)
{
numTermsExecute = t->totalTerms;
}
else
{
numTermsExecute = t->totalTerms*(percentage/100);
//printf("NTE: %d\n", numTermsExecute);
totalIterations = (t->totalTerms/numTermsExecute) + 1;
//printf("TI: %d\n", totalIterations);
}
if(t->totalTerms == 1)
{
t->currPiValue = 2*1;
return;
}
else
{
t->currPiValue = 2*1;
}
while (n <= t->totalTerms)
{
// Calculate last fraction of a single nth term (i.e (x^5/5)) for x = 1
tempResultA = 2*(1/((2*n)-1));
// calculates the remaining first n-1 fractions of nth term (i.e (1/2)(3/4))
while(m <= n)
{
tempResultB = tempResultB*(((2*m)-3)/((2*m)-2));
//printf("This fract is %f : Nth term = %d \n", tempResultB, (int) n);
m++;
}
// multiplies previous calculations (i.e (x^5/5)*(1/2)(3/4))
// this is the calculation of an nth term
t->currPiValue = t->currPiValue + (tempResultA*tempResultB);
t->currTerm = n;
n++;
// Reset Variables
m = 2;
tempResultB = 1;
// not preemptive code to give control to CPU
if (totalIterations != 0 && (n == (numTermsExecute*currentIteration))) // if not preemptive we go in here
{
currentIteration++;
printf("---Giving Control to Scheduler---\n");
//give control to scheduler; Should eventually return control here
syscall(SIGVTALRM);
}
} //end while totalTerms
}