any existing code for Calculation Engine (define & use formulae) ?

M

Mister B

To avoid reinventing the wheel, I was wondering if anyone knows of existingsoftware (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be definedwith a number of inputs, and then triggered to produce an output.

For example:
formula 1: a + b - c
formula 2: a * ( b + c + d ) / 3
formula 3: a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

TIA
Mark
 
F

Fred

To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.

For example:
formula 1:   a + b - c
formula 2:   a * ( b + c + d ) / 3
formula 3:   a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

What do you mean by "Value A" and "Value B" ?
 
J

Joel C. Salomon

Mark said:
To avoid reinventing the wheel, I was wondering if anyone knows of
existing software (in C or libraries that could be called from C code on
Linux platform) that implements a Calculation Engine, whereby formulae
can be defined with a number of inputs, and then triggered to produce an
output.

See <http://en.wikipedia.org/wiki/Hoc_(programming_language)>, which includes links to several extended versions available.

—Joel
 
R

Roberto Waltman

Mister said:
... existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine
...

I think what you call "Calculation Engine", usually goes by the name
of "Spreadsheet".
I don't know off the top of my head if there is a simple C library
available (I'll let you do the search,I would expect to find a few),
but, for what it is worth, earlier Turbo Pascal version came with a
spreadsheet demo in source form, and there is SIAG. (Scheme)
Neither one should be too difficult to translate to C.
 
B

BartC

Mister B said:
To avoid reinventing the wheel, I was wondering if anyone knows of
existing software (in C or libraries that could be called from C code on
Linux platform) that implements a Calculation Engine, whereby formulae can
be defined with a number of inputs, and then triggered to produce an
output.

For example:
formula 1: a + b - c
formula 2: a * ( b + c + d ) / 3
formula 3: a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

This is not clear; where do the formulae come from? Are they input by the
user, exist in a file (both in text format), or hardcoded in the program
(possibly as text too)?

What determines which formula is applied?

Doing this properly is a lot of work, as you say, but having an attempt
might be a useful exercise, and could make it easier to choose an
appropriate library once the ins and outs are known.

I had a go at something myself; this code has formulae and inputs hardcoded
(this is just user-input and not difficult). I use C for the 'engine', by
generating a short C program to apply the formula. (The new program prints
it's output; normally you'd need a way of getting the result back into the
calling program).

You need to change the line system("run.bat") into something that compiles
and runs the output of this program, on your machine.

In practice, this sort of approach wouldn't use C as the output format, it's
more likely to be a soft language, and one that can be closely integrated
into C (Python or Lua perhaps), so that the result can be picked up via some
interface.

#include <stdio.h>
#include <stdlib.h>

#define outfile "formula.c"

char* formulae[]={
"a+b-c",
"a*(b+c+d)/3",
"a*sqrt(b)"};

double evaluate(char* formula, double* params,int nparams){
FILE* f;
int i;

f=fopen(outfile,"w");

fprintf(f,"#include<stdio.h>\n");
fprintf(f,"#include<stdlib.h>\n");
fprintf(f,"#include<math.h>\n");
fprintf(f,"\n");

fprintf(f,"double eval(");
for (i=0; i<nparams; ++i){
fprintf(f,"double %c",'a'+i);
if (i<(nparams-1)) fprintf(f,",");
}
fprintf(f,") {\n");

fprintf(f,"double x;\n");

fprintf(f,"x=%s;\n",formula);

fprintf(f,"printf(\"Result is: %%f\",x);\n");

fprintf(f,"}\n");
fprintf(f,"\n");

fprintf(f,"int main(void) {\n");
fprintf(f,"eval(");
for (i=0; i<nparams; ++i){
fprintf(f,"%f",params);
if (i<(nparams-1)) fprintf(f,",");
}

fprintf(f,");\n");
fprintf(f,"}\n");

fclose(f);

return 0.0;
}

int main(void){
double args[]={23,34,18};

evaluate(formulae[0],args,3);

system("run.bat");

}
 
D

Dann Corbit

To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.

For example:
formula 1: a + b - c
formula 2: a * ( b + c + d ) / 3
formula 3: a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

Perhaps you are interested in this:
18.14: I need code to parse and evaluate expressions.

A: Two available packages are "defunc," posted to comp.sources.misc
in December, 1993 (V41 i32,33), to alt.sources in January, 1994,
and available from sunsite.unc.edu in
pub/packages/development/libraries/defunc-1.3.tar.Z, and
"parse," at lamont.ldgo.columbia.edu. Other options include the
S-Lang interpreter, available via anonymous ftp from
amy.tch.harvard.edu in pub/slang, and the shareware Cmm ("C-
minus-minus" or "C minus the hard stuff"). See also questions
18.16 and 20.6.

There is also some parsing/evaluation code in _Software
Solutions in C_ (chapter 12, pp. 235-55).

In general, when you want some software to perform a task, a sourceforge
search is a good place to start, along with googlecode, etc.
 
G

Gene

To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.

For example:
formula 1:   a + b - c
formula 2:   a * ( b + c + d ) / 3
formula 3:   a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

TIA
Mark
 
G

Gene

To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.

For example:
formula 1:   a + b - c
formula 2:   a * ( b + c + d ) / 3
formula 3:   a * sqrt (b)

Value A uses formula 1 with inputs X, Y, Z
Value B uses formula 3 with inputs M, N
etc

Mark,

The term for what you're looking for is an expression evaluator. When
I Google for "expression evaluator in c", this pops up very near the
top of the list:

http://www.codeproject.com/KB/library/expreval.aspx

On the other hand, writing a little expression evaluator is a
wonderful task for learning. See any good compiler book and the terms
"operator precedence parser" and/or recursive descent parser.

You can also use a parser generator. See the documentation for GNU
Bison. They use an expression evaluator as a tutorial example. And
Bison generates C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top