PC_graph/math_functions.c
2024-11-28 17:30:29 +01:00

65 lines
1.5 KiB
C

#include "math_functions.h"
#include <math.h>
#define MAKE_FUNCTION_1_ARG(name) \
static double mf_##name(const double *args) { \
return name(args[0]); \
}
MAKE_FUNCTION_1_ARG(fabs)
MAKE_FUNCTION_1_ARG(exp)
MAKE_FUNCTION_1_ARG(log)
MAKE_FUNCTION_1_ARG(log10)
MAKE_FUNCTION_1_ARG(sin)
MAKE_FUNCTION_1_ARG(cos)
MAKE_FUNCTION_1_ARG(tan)
MAKE_FUNCTION_1_ARG(asin)
MAKE_FUNCTION_1_ARG(acos)
MAKE_FUNCTION_1_ARG(atan)
MAKE_FUNCTION_1_ARG(sinh)
MAKE_FUNCTION_1_ARG(cosh)
MAKE_FUNCTION_1_ARG(tanh)
static double mf_min(const double *args) {
if (args[0] < args[1])
return args[0];
return args[1];
}
static double mf_max(const double *args) {
if (args[0] > args[1])
return args[0];
return args[1];
}
static double mf_mod(const double *args) {
return fmod(args[0], args[1]);
}
const struct math_function *fns_get(void) {
static const struct math_function fns[] = {
{ "abs", 1, mf_fabs },
{ "exp", 1, mf_exp },
{ "ln", 1, mf_log },
{ "log", 1, mf_log10 },
{ "sin", 1, mf_sin },
{ "cos", 1, mf_cos },
{ "tan", 1, mf_tan },
{ "asin", 1, mf_asin },
{ "acos", 1, mf_acos },
{ "atan", 1, mf_atan },
{ "sinh", 1, mf_sinh },
{ "cosh", 1, mf_cosh },
{ "tanh", 1, mf_tanh },
{ "min", 2, mf_min },
{ "max", 2, mf_max },
{ "mod", 2, mf_mod },
{ NULL }
};
return fns;
}