EquationEvaluator 1.2

A REALbasic plug-in for Runtime Mathematical Expression Evaluation

This is both a PPC and a Carbon/OS X Plug-in.

EquationEvaluator is based on Robert Douglas' Macintosh port of Mark Morley's Expression Evaluator (which can be found in DOS code repositories under the name C-EVAL).

This REALbasic version is a plug-in with just one function that takes a string (a mathematical expression) as an argument and returns a double.

Supported mathematical functions include:

Standard Math Functions
+, -, *, /, ^
and % (modulus)

Standard Trig Functions
sin(x), cos(x), tan(x)
asin(x), acos(x), atan(x)
sinh(x), cosh(x), tanh(x)

Other Functions
exp(x) - e to the power of x
log(x) - natural log of x
log10(x) - base-10 log of x
sqrt(x) - square root of x
floor(x) - x rounded down
ceil(x) - x rounded up
abs(x) - absolute value of x

hypot(x,y) -
same as sqrt(x^2 + y^2)

deg(x) to convert x to degrees
rad(x) to convert x to radians

Built-In Constants
pi = pi (~3.1415)
e = e (~2.7182)

Variable Assignment


You give the function (called JEE) a string with the expression you would like evaluated, and it will return the value as a double.

The variable assignment feature makes it possible to assign up to 50 variables (each with a name up to 15 characters long). You can assign the variables before you give the expression (as in "x=3, y=2, x+y") or in the middle of an expression (as in "3+(x=4) + x" which should return 11). Or you can have an expression evaluated as part of the assignment, and used later on in another expression (as in "x=3, y=x+2, x+y" which returns 8). Note that if you do the assignment before the expression then you need to follow the assignment with a comma (,) so that the parser knows where the assignment stops and the expression begins.

EquationEvaluator follows the standard math rules for determining which operations go first (multiplication/division before addition/subtraction, etc.). You can also group parts of your expression with parentheses ( "(" and ")" ) if you want to be sure of the order of operations.

Warning: EquationEvaluator does not evaluate scientific (powers of ten) notation. If you try this assignment "x=3E5" (meaning "x=300000"), EquationEvaluator will become confused and will not return the correct value (probably it will return 0). The best solution is to pre-process any strings with scientific notation so that they replace the "E" with "*10^" (so our assignment above would be "x=3*10^5").


JEE("7+11*5") returns 62
JEE("sin(rad(45))") returns 0.707 (the sine of 45 degrees)
JEE("hypot(3,4)") returns 5
JEE("x=5.05, y=2, floor(x^y)") returns 25
JEE("theta=pi/3, cos(theta)") returns .5 (the cosine of pi/3)
JEE("x=5, z=log10(x), z") returns 0.69897 (it returns the value of z, which is log10 of 5)


Download EquationEvaluator here


You can see a review and comparison of all of the runtime expression evaluating options available for RB users on my RB Math Stuff page. Or visit REALbasic's web site at www.realbasic.com

Return to Tamara's Homepage