-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathfunction.c
66 lines (53 loc) · 1.6 KB
/
mathfunction.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
#include "mathfunction.h"
#include <string.h>
// additional functions
double cot(double a)
{
return 1 / tan(a);
}
double acot(double a)
{
return M_PI / 2 - atan(a);
}
double coth(double a)
{
return 1 / tanh(a);
}
// provided functions
const char* function_names[] = {
"cos", "sin", "tan", "cot", "cosh", "sinh", "tanh", "coth",
"acos", "asin", "atan", "acot", "arccos", "arcsin", "arctan", "arccot",
"exp", "ln", "log", "log10", "sqrt", "abs", "ceil", "floor",
};
double (*function_ptrs[])(double) = {
&cos, &sin, &tan, &cot, &cosh, &sinh, &tanh, &coth,
&acos, &asin, &atan, &acot, &acos, &asin, &atan, &acot,
&exp, &log, &log, &log10, &sqrt, &fabs, &ceil, &floor
};
bool evaluate_function(const char* name, double value, double* result_ptr)
{
// search for function index accroding to the name
int index = -1;
for (int i = 0; i < sizeof(function_names) / sizeof(function_names[0]); i++)
if (strcmp(name, function_names[i]) == 0)
{
index = i;
break;
}
// function was not found
if (index == -1)
return false;
// functions cos, sin, tan, cosh, sinh, tanh expect the input in radians
// convert from degrees to radians
if (index < 8)
value = value * M_PI / 180;
// calculate the result
double result = function_ptrs[index](value);
// functions acos, asin, atan return the result in radians
// convert from radians to degrees
if (index >= 8 && index < 16)
result = result / M_PI * 180;
// return success and assign value
*result_ptr = result;
return true;
}