Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Work in Progress / Evaluation plugin

Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 5th Nov 2009 01:33 Edited at: 5th Nov 2009 01:46
I've noticed that it actually seems there's no eval() plugin available around here. Granted, it's not that hard to write such functionality yourself, but it can always be nice to have a set of commands ready to go, so I figured I'd make a plugin out of this.

So, what is it?
This plugin lets you type in a mathematical expression as a string and get the resulting value as easily as this:

There is no real limit to the number of terms you may use; using brackets as well as boolean arithmetics you can feed it an arbitrarily complex expression and (hopefully ) get the correct result rerurned.
For the sake of accuracy, all values are considered as double floats. Therefore, for example, evaluate("5 / 2") will yield 2.5, and not drop the 5 due to integer division. In other words, there is no need to write the expression as "5 / 2.0" or similar for these situations.

The plugin seems to be pretty stable in its current state, and does indeed evaluate standard and boolean expressions correctly - you can download the current "demo" version a bit further down in this post. Except for poor error handling (when for example passing an incomprehendable expression), I would say that the plugin is in working order in its current state, and is mainly in line for new / extended features; most notably being the possibility to use mathematical functions in expressions.

Commands in current version
init eval - lets the plugin create the objects it needs to operate.
free eval - frees the memory previously allocated by init eval
evaluate - evaluates an expression string, that might contain custom variables set by set variable, and returns the resulting value as a double float.
set variable - sets a variable for use within the evaluation system.
purge variables - removes all previously declared variable entries.

It should be noted that undeclared variable names in expressions are ignored by the evaluate function. They are not replaced with default values of zero.

A note on the boolean operators
I figured that, at a later point, the evaluation function should allow variable declarations / value setting within expressions themselves, using the '=' sign (it already supports variables, but they must be set using the set variable command explicitly as of current). Therefore, I decided to use the C++ (and most other languages as well, for that matter) equality operator '==' instead of the single equality sign from DB. Thus, it seemed reasonable to use those notations for the other boolean operators as well.
Here's a list:

It should also be noted that spaces / tabs are removed from the expression during parsing, so one can format the input string as desired.

Example code

Download plugin
Comes with a keyword and help files.
Click here to download.

Well, thanks for your time.
Would appreciate some feedback

Years of Service
User Offline
Joined: 28th Mar 2009
Posted: 5th Nov 2009 10:47
I'm probably just displaying my ignorance here, but what is the difference between:

result = evaluate("5 * 9")


result = 5 * 9

Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 5th Nov 2009 11:08
None, but evaluating a string let's you calculate things that aren't decided at compilation time, such as input from the user.

Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 5th Nov 2009 12:27
Evaluating from a string is also alot slower. So only use it with scripting and user input.

Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 5th Nov 2009 13:55
Pretty cool.

If you're using Reverse Polish Notation, it would be possible to make '=' an operator just as any other one. That means you don't have to use the 'SetVariable' command. You can simply evaluate the expression "MyVar = 42". If MyVar exists, reset it. If it doesn't, create it, and set it.

"everyone forgets a semi-colon sometimes." - Phaelax
Alfa x
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 5th Nov 2009 17:02
Very nice. Congrats.
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 5th Nov 2009 17:08
Does it have operator precedence? And if so, what is the order of precedence?

French gui
Years of Service
User Offline
Joined: 11th May 2004
Location: France
Posted: 5th Nov 2009 17:57
Wow, this is a must have for scripting! Congrats!
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 5th Nov 2009 19:10
Thanks for the appreciation

Quote: "If you're using Reverse Polish Notation..."

Haha, what a weird name... it's postfix, no?
Anyway, yes, I was thinking of allowing variables to be set from within the evaluation strings.

Quote: "Does it have operator precedence? And if so, what is the order of precedence?"

Yes, it wouldn't work well without that.
The order is brackets -> multiplication/division -> addition/subtraction -> boolean operators.
Variables are substituted for their numeric values in the first parsing step (before starting to solve the expression).

Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 6th Nov 2009 00:04 Edited at: 6th Nov 2009 00:08
Download updated version.

Declare / set variables directly in the evaluate command:

Improved error handling. Does not display error messages whenever an expression cannot be properly parsed by default ('debug mode'), but rather lets you take control over this.

New commands:
evaluation failed() - returns true (1) if the previously provided expression could not be properly evaluated.
get eval error() - returns an error code indicating why the previous call to evaluate failed. *
get eval error message - returns a string containing a brief explanation of what failed if get eval error is true.
set eval debug mode on - activates the 'debug mode', in which the plugin automatically pops up a messagebox containing the failure description (as otherwise given by get eval error message) whenever an expression cannot be interpreted.
The 'debug mode' is inactivated by default, contrary to in the previous demo edition.
set eval debug mode off - deactivates 'debug mode'.
get eval debug state() - returns true (1) if 'debug mode' is active, otherwise false is returned.

Well, that's about it for now.
Updated documentation etc. is available in the download.

* There's currently no list of those values available.
If you really need it already, here's the current list:

Years of Service
User Offline
Joined: 4th May 2005
Location: Croatia
Posted: 6th Nov 2009 00:51
I wrote the same thing in DBPro a year ago. I also made an interpretator for a scripting language. And a game where you program your little vehicle robot with 8 sensors. Don't know why I didn't post it here Jut felt the need to say that, sorry Really, sorry.

Anyway, it looks good, and I'm sure it's a hell faster than the one written in DBPro. How fast is it anyway?
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 8th Nov 2009 20:48 Edited at: 8th Nov 2009 20:49
Sorry about what?
I'm sure a lot of people have done this kind of thing before; just thought I'd put it up for "free and easy use" for those who don't want to spend time on this particular thing.

The current implementation is pretty much unoptimized; it can handle about 1500 moderately complex formulas (something like ((2745.2 + 12 - 49) > (241.1 / 0.096)) * (4.24 / (8 + 12) * 51)) per second in its current state.
What takes the most time would be parsing the strings; something you can do beforehand in a scripting language, hence making it run quite a bit faster.[u][/u]

Login to post a reply

Server time is: 2022-11-29 09:06:06
Your offset time is: 2022-11-29 09:06:06