** Next:** User defined functions
** Up:** Expression evaluator
** Previous:** Variables
** Contents**
** Index**

##

Functions

The mathematical functions that the expression evaluator knows are in the
array `Funcs[]` (it is an array of structures `FUNCTION`). This array
must be defined by the user. The total number of functions is
`_NBFONC`, which must be set by the user.
An array of functions is predefined in the library,
it is called `Func_interp`, and the number of functions defined in
`Func_interp` is contained in the integer `_NBFONC0`.

The structure `FUNCTION` is defined as follows

typedef struct
{
char* name; /* Function name */
int args; /* Number of arguments to expect */
double (*func)(double *); /* Pointer to function */
} FUNCTION;

The first member of the structure
`FUNCTION` is the character string that will be used to call the function.
The second member is the number of parameters of the function. It must be
between 1 and the constant `MAX_F_ARGS` defined in `interp.h` (it is
10). The third member is the name of the function. It must be a function
returning a double value, having only one argument : an array of double
precision real numbers. In the array `Func_interp` the usual
mathematical functions are used. It is also possible to put other functions
found in some libraries of special functions, like `cephes` for instance,
or defined by the user. The array `Func_interp` looks like this

FUNCTION Funcs_interp[] =
{
/* name, funtion to call */
{ "sin", 1, _I_sin }, /* 0 */
{ "cos", 1, _I_cos }, /* 1 */
{ "tan", 1, _I_tan }, /* 2 */
{ "asin", 1, _I_asin }, /* 3 */
{ "acos", 1, _I_acos }, /* 4 */
{ "atan", 1, _I_atan }, /* 5 */
{ "sinh", 1, _I_sinh }, /* 6 */
{ "cosh", 1, _I_cosh }, /* 7 */
{ "tanh", 1, _I_tanh }, /* 8 */
{ "exp", 1, _I_exp }, /* 9 */
{ "log", 1, _I_log }, /* 10 */
{ "log10", 1, _I_log10 }, /* 11 */
{ "sqrt", 1, _I_sqrt }, /* 12 */
{ "floor", 1, _I_floor }, /* 13 */
{ "ceil", 1, _I_ceil }, /* 14 */
{ "abs", 1, _I_fabs }, /* 15 */
{ "deg", 1, _I_deg }, /* 16 */
{ "rad", 1, _I_rad }, /* 17 */
{ 0 } /* 18 */
};
int _NBFONC0=18;

The function `_I_sin` is defined as follows :

double
_I_sin(double *x)
{
return sin(*x);
}

and the other functions in the array are defined in the same way.
For instance
if the program uses only the predefined array of functions, the main source
file would look as follows :

......
FUNCTION *Funcs;
int main(int argc, char *argv[])
{
Funcs = Funcs_interp;
_NBFONC = _NBFONC0;
......
}

** Next:** User defined functions
** Up:** Expression evaluator
** Previous:** Variables
** Contents**
** Index**
2009-11-12