evaluating math w/o recompile
mccaugh at s.cs.uiuc.edu
mccaugh at s.cs.uiuc.edu
Sun Sep 17 10:10:00 AEST 1989
In article <72603 at yale-celray.yale.UUCP> zador-anthony at CS.YALE.EDU
(Anthony Zador) writes:
> I would like to be able to specify the form of the function f
> at *run time* (no recompiles). That is, i would like
> to place a line of math
> in some file and have the program read it in and evaluate it.
> .....
As rightly noted by the preceding respondents, the function definition would
have to be parsed into some internal form suitable for digestion by an inter-
preter. But with a utility as nice as BC (the arbitrary-precision desk calcu-
lator), it may not be necessary to "re-invent the wheel" writing your own.
Suppose file 'f1' contained your function definitions, say:
define u(x) { /* signum function */
if(x<0) return(-1)
if(x>0) return(1)
return(0)
}
define t(x) { /* tangent function */
auto z
if(x==90) return(572957.8)
if(x==270) return(572957.8)
/* else convert degrees to radians */
z = 0.0174532*x /* and return tan(x) */
return(s(z)/c(z))
}
and your calls to these functions were included in another file, 'f2':
/* some sample values */
f(-13.57) /* s/b: -1 */
t(45) /* s/b: +1 */
quit
Then your C program could issue the system call:
system("cat f1 f2 | bc -l > f3");
and you have your results in file 'f3':
-1
.99999167323977094494
This is certainly not an elegant solution, but it does appear feasible
and takes advantage of BC's arbitrary-precision capability to boot
(and hence is slooow). But it would appear to spare one the need to tool
their own interpreter along the lines of 'hoc' (I recall the source
for 'hoc' runs to 13 pages in Kernighan & Pike).
Scott McCaughrin
Dept. of Computer Science
University of Illinois
More information about the Comp.lang.c
mailing list