40 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
41 |
|
#endif |
42 |
|
|
43 |
< |
static double l_if(char *), l_select(char *), l_rand(char *); |
43 |
> |
static double l_if(char *), l_select(char *); |
44 |
> |
static double l_min(char *), l_max(char *); |
45 |
> |
static double l_rand(char *); |
46 |
|
static double l_floor(char *), l_ceil(char *); |
47 |
|
static double l_sqrt(char *); |
48 |
|
static double l_sin(char *), l_cos(char *), l_tan(char *); |
62 |
|
{ "if", 3, ':', l_if }, |
63 |
|
{ "log", 1, ':', l_log }, |
64 |
|
{ "log10", 1, ':', l_log10 }, |
65 |
+ |
{ "max", 1, ':', l_max }, |
66 |
+ |
{ "min", 1, ':', l_min }, |
67 |
|
{ "rand", 1, ':', l_rand }, |
68 |
|
{ "select", 1, ':', l_select }, |
69 |
|
{ "sin", 1, ':', l_sin }, |
71 |
|
{ "tan", 1, ':', l_tan }, |
72 |
|
}; |
73 |
|
|
74 |
< |
static int libsize = 16; |
74 |
> |
static int libsize = 18; |
75 |
|
|
76 |
|
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
77 |
|
|
78 |
|
|
79 |
|
int |
80 |
< |
fundefined( /* return # of arguments for function */ |
80 |
> |
fundefined( /* return # of req'd arguments for function */ |
81 |
|
char *fname |
82 |
|
) |
83 |
|
{ |
145 |
|
; |
146 |
|
if (cp == fname) |
147 |
|
return; |
148 |
< |
if (cp[-1] == CNTXMARK) |
148 |
> |
while (cp[-1] == CNTXMARK) { |
149 |
|
*--cp = '\0'; |
150 |
+ |
if (cp == fname) return; |
151 |
+ |
} |
152 |
|
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
153 |
+ |
if (fptr == NULL) |
154 |
+ |
return; /* nothing! */ |
155 |
|
if (libsize >= MAXLIB) { |
156 |
|
eputs("Too many library functons!\n"); |
157 |
|
quit(1); |
203 |
|
EPNODE *ep = NULL; |
204 |
|
double aval; |
205 |
|
|
206 |
+ |
if (!n) /* asking for # arguments? */ |
207 |
+ |
return((double)nargum()); |
208 |
+ |
|
209 |
|
if (!actp | (--n < 0)) { |
210 |
|
eputs("Bad call to argument!\n"); |
211 |
|
quit(1); |
212 |
|
} |
213 |
< |
if ((n < AFLAGSIZ) & (actp->an >> n)) /* already computed? */ |
213 |
> |
if ((n < AFLAGSIZ) & actp->an >> n) /* already computed? */ |
214 |
|
return(actp->ap[n]); |
215 |
|
|
216 |
|
if (!actp->fun || !(ep = ekid(actp->fun, n+1))) { |
220 |
|
} |
221 |
|
curact = actp->prev; /* previous context */ |
222 |
|
aval = evalue(ep); /* compute argument */ |
223 |
< |
curact = actp; /* push back context */ |
224 |
< |
if (n < ALISTSIZ) { /* save value if we can */ |
223 |
> |
curact = actp; /* put back calling context */ |
224 |
> |
if (n < ALISTSIZ) { /* save value if room */ |
225 |
|
actp->ap[n] = aval; |
226 |
|
actp->an |= 1L<<n; |
227 |
|
} |
392 |
|
static double |
393 |
|
l_select(char *nm) /* return argument #(A1+1) */ |
394 |
|
{ |
395 |
< |
int n; |
395 |
> |
int narg = nargum(); |
396 |
> |
double a1 = argument(1); |
397 |
> |
int n = (int)(a1 + .5); |
398 |
|
|
399 |
< |
n = (int)(argument(1) + .5); |
387 |
< |
if (n == 0) |
388 |
< |
return(nargum()-1); |
389 |
< |
if (n < 1 || n > nargum()-1) { |
399 |
> |
if (a1 < -.5 || n >= narg) { |
400 |
|
errno = EDOM; |
401 |
|
return(0.0); |
402 |
|
} |
403 |
+ |
if (!n) /* asking max index? */ |
404 |
+ |
return(narg-1); |
405 |
|
return(argument(n+1)); |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
|
409 |
+ |
static double |
410 |
+ |
l_max(char *nm) /* general maximum function */ |
411 |
+ |
{ |
412 |
+ |
int n = nargum(); |
413 |
+ |
double vmax = argument(1); |
414 |
+ |
|
415 |
+ |
while (--n) { |
416 |
+ |
double v = argument(n); |
417 |
+ |
if (vmax < v) |
418 |
+ |
vmax = v; |
419 |
+ |
} |
420 |
+ |
return(vmax); |
421 |
+ |
} |
422 |
+ |
|
423 |
+ |
|
424 |
+ |
static double |
425 |
+ |
l_min(char *nm) /* general minimum function */ |
426 |
+ |
{ |
427 |
+ |
int n = nargum(); |
428 |
+ |
double vmin = argument(1); |
429 |
+ |
|
430 |
+ |
while (--n) { |
431 |
+ |
double v = argument(n); |
432 |
+ |
if (vmin > v) |
433 |
+ |
vmin = v; |
434 |
+ |
} |
435 |
+ |
return(vmin); |
436 |
|
} |
437 |
|
|
438 |
|
|