Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Free mathematical expression parser

Started by DaWanderer Apr 11, 2006 at 3:52 PM 9 replies 4.4k views
Original Post
DaWanderer
DaWanderer
Anyone know of a good, free .NET library that will parse and evaluate mathematical expressions like "cos(x) + 3.14"? I remember using one a year or so back, but I can't remember the name and my Google queries don't get me anything.
DaWanderer
DaWanderer
If anyone's interested, I finally found the one I was looking for: .Math. It could really use an update for .NET 2.0, but it works just fine. Supports variables and custom functions as well as having a nice amount of built-in functions (including an "if-then-else" construct).
Kambiz
Kambiz
Interesting Libraray [smile]
MButchers
MButchers
If your using a database and speed isn't a vital issue you can also do eval calculations using SQL statements

In Oracle

SELECT 10 * 40 FROM dual

In MS SQL Server and Access

SELECT 10 * 40

You can then use a object which implements IDbCommand to do an ExecuteScalar to return the value.

Mark
DaWanderer
DaWanderer
Most of the libraries that Kambiz suggested used the C# compiler to do expression evaluation. While this is a very smart and expandable approach, it was CPU intensive (to generate the assembly) and memory intensive (to store the assembly). Since there is no way to unload an assembly from an AppDomain, each evaluation would require more memory.

.Math could really benefit from generics and anonymous methods instead of relying on an inherited class for custom functions. I imagine that you could do something like this:
EqCompiler compiler = new EqCompiler("x + y");//Parameters: name, min # of params, max # of params, methodcompiler.AddFunction("pythag", 2, 2, delegate(List<CValue> values){    double a = values[0].GetValue(), b = values[1].GetValue();    return( Math.Sqrt((a * a) + (b * b)) );});


Would anyone be interested in me adding this kind of functionality?
Arild Fines
Arild Fines
Quote:
Original post by DaWanderer
Most of the libraries that Kambiz suggested used the C# compiler to do expression evaluation. While this is a very smart and expandable approach, it was CPU intensive (to generate the assembly) and memory intensive (to store the assembly). Since there is no way to unload an assembly from an AppDomain, each evaluation would require more memory.

Check out the new 2.0 DynamicMethod class. It lets you generate and execute IL code at runtime without the overhead of having to create a whole new assembly. It is, I believe, what Ironpython uses to implement Python's eval() functionality (and probably some other things as well).

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Bob Janova
Bob Janova
Sweet! Shame I'm using 1.1 ...

With reference to the original question, there are two steps. Turning an expression into a collection of tokens (tokenisation) is fairly easy (split at white space and non-alphanumeric characters, with allowances for quoted strings and comments if necessary). Executing an expression is somewhat harder and involves finding (or writing) an interpreter.

I have written an array-processing language in C# which I'm supposed to be releasing a beta of kind of now, which would easily cope with an equivalent statement to this, although not written in quite the same way. I imagine IronPython is probably usable as a script language, too.

If you want something to use with 'correct' mathematical notation (whatever that is) then it might be harder.

Edit: I'd forgotten I'd posted this, but for anyone who's interested: Rowan.

[Edited by - Bob Janova on May 22, 2006 1:33:24 PM]
alex_myrpg
alex_myrpg
I believe these methods (using RPN) are pretty efficient. Plenty of examples:
http://www.codeproject.com/cpp/rpnexpressionevaluator.asp
http://www.codeproject.com/csharp/custexpreval.asp
http://www.codeproject.com/csharp/rpn_expressionparser.asp
http://www.codeproject.com/cpp/RPNcalculator.asp

Hope they help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.