-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
163 lines (156 loc) · 3.31 KB
/
function.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# include "function.h"
struct s_function *init_function(enum e_function *func,float para)
{
struct s_function *fun = malloc(sizeof(struct s_function));
fun->function = func;
fun->param = para;
fun->power = 1;
fun->mult = 1;
return fun;
}
enum e_function *get_function(char *function)
{
enum e_function *name = malloc(sizeof(enum e_function));
if ( strcmp(function,"exp") == 0)
*name = EXP;
else if (strcmp(function,"ln") == 0)
*name = LN;
else if (strcmp(function, "sqrt") == 0)
*name = SQRT;
else if (strcmp(function, "pow") == 0)
*name = POW;
else if (strcmp(function, "cos") == 0)
*name = COS;
else if (strcmp(function, "sin") == 0)
*name = SIN;
else if (strcmp(function, "tan") == 0)
*name = TAN;
else if (strcmp(function, "acos") == 0)
*name = ACOS;
else if (strcmp(function, "asin") == 0)
*name = ASIN;
else if (strcmp(function, "atan") == 0)
*name = ATAN;
else
*name = UNKNOW_F;
return name;
}
float calcul_function(struct s_function *function, float val)
{
float res = 0;
switch (*function->function)
{
case EXP:
res = expf(val);
break;
case LN:
if (val <= 0)
set_erreur(5);
res = logf(val);
break;
case COS:
res = cosf(val);
break;
case ACOS:
if (val < -1 || val > 1)
set_erreur(8);
res = acosf(val);
break;
case SIN:
res = sinf(val);
break;
case ASIN:
if (val < -1 || val > 1)
set_erreur(8);
res = asinf(val);
break;
case TAN:
res = tanf(val);
break;
case ATAN:
if (val < -M_PI || val > M_PI)
set_erreur(9);
res = atanf(val);
break;
case POW:
if (function->param == -1 && val == 0)
set_erreur(4);
res = powf(val,function->param);
break;
case SQRT:
if (val <= 0)
set_erreur(6);
res = powf(val, 1 / function->param);
break;
default:
set_erreur(7);
break;
}
if(*get_erreur() > 0)
return 0;
return pow(res, function->power) / function->mult;
}
float calcul_inverse(struct s_function *function, float val)
{
float res = val / function->mult;
switch (*function->function)
{
case LN:
res = expf(res);
break;
case EXP:
if (val <= 0)
set_erreur(5);
res = logf(res);
break;
case ACOS:
res = cosf(res);
break;
case COS:
if (val < -1 || val > 1)
set_erreur(8);
res = acosf(res);
break;
case ASIN:
res = sinf(res);
break;
case SIN:
if (val < -1 || val > 1)
set_erreur(8);
res = asinf(res);
break;
case ATAN:
res = tanf(res);
break;
case TAN:
if (val < -M_PI || val > M_PI)
set_erreur(9);
res = atanf(res);
break;
case SQRT:
if (1 / function->param == -1)
set_erreur(4);
res = powf(res, 1 / function->param);
break;
case POW:
if (val <= 0)
set_erreur(6);
res = powf(res, 1 / function->param);
break;
default:
set_erreur(7);
break;
}
if(*get_erreur() > 0)
return 0;
return pow(res, 1 / function->power);
}
float calcul(struct s_function *function, float val)
{
return pow(val, 1 / function->power) / function->mult;
}
void free_function(struct s_function *function)
{
free(function->function);
free(function);
}