Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!uxc.cso.uiuc.edu!uxc.cso.uiuc.edu!m.cs.uiuc.edu!s.cs.uiuc.edu!mccaugh From: mccaugh@s.cs.uiuc.edu Newsgroups: comp.lang.c Subject: Re: evaluating math w/o recompile Message-ID: <207600043@s.cs.uiuc.edu> Date: 17 Sep 89 00:10:00 GMT References: <11068@smoke.BRL.MIL> Lines: 57 Nf-ID: #R:smoke.BRL.MIL:11068:s.cs.uiuc.edu:207600043:000:1745 Nf-From: s.cs.uiuc.edu!mccaugh Sep 16 19:10:00 1989 In article <72603@yale-celray.yale.UUCP> zador-anthony@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