-
Notifications
You must be signed in to change notification settings - Fork 2
/
The function of pow(x,n) ,exp(10,x),log(n,3).c
87 lines (60 loc) · 1.38 KB
/
The function of pow(x,n) ,exp(10,x),log(n,3).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
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <math.h> /* only used for testing */
float exponential(int n, float x)
{
int i;
float sum = 1.0f; // initialize sum of series
for ( i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
/*float logn(float n,int r)
{
return(n >r - 1 )?1+logn(n/r,r):0;
}*/
float mylog(float y)
{
float x = (y - 1)/(y + 1);
double sum = x, term = x;
/* used for power */
double j = 3;
/* sums terms up to x^1200/1200 */
while(j < 1200)
{
j += 2;
/* starts at term^3 */
term = (term * x * x);
/* calcs term + term^3 to start */
sum = sum + (term / j);
}
/* finishes calc sum by multiplying by 2 */
sum = sum * 2;
/* if y is <= 0.0 return 0 */
if(y <= 0.0)
{
sum = 0;
}
return sum;
}
/* calculates power using previous function */
/*pow(x,p)=e^log(x)*p */
float mypow(float x, float p)
{
float result, k, answer;
k = mylog(x,3);
result = p * k;
answer = exponential(10,result);
return answer;
}
int main()
{
double x, p;
printf("Actual e^x: %lf\n", exp(1));
printf("My e^x: %f\n", exponential(10,1));
printf("Actual log(x): %f\n", log(2.7));
printf("My log(x): %f\n", logn(2.7,3));
printf("Actual pow(x,p): %f\n", pow(2.3,1.5));
printf("My pow(x,p): %f\n", mypow(2.3,1.5));
return 0;
}