This is a math expression parser written in C#, which evaluates mathematical expressions such as for example cos(x)-sin(x)+2 with given values.
Example usage:
// Import the assembly that contains the parser
using info.lundin.Math;
// some other imports
using System;
using System.Collections;
public class Test
{
public static void Main( String[] args )
{
// Instantiate the parser
ExpressionParser parser = new ExpressionParser();
// Create a hashtable to hold values
Hashtable h = new Hashtable();
// Add variables and values to hashtable
h.Add( "x", 1.ToString() );
h.Add( "y", 2.ToString() );
// Parse and write the result
double result = parser.Parse( "xcos(y)", h );
Console.WriteLine( Result: {0}, result );
}
}
The parser supports most common mathematical operators and functions such as:
Operators:
+, -, *, /, ^, %
Functions:
sqrt, sin, cos, tan, atan, acos, asin, acotan, exp, ln, 10log, fac, sinh, cosh, tanh, abs, ceil, floor, sfac, round, fpart.
Logical:
!, ==, !=, ||, &&, >, < , >=, <=
P LPosted Mar 7, 2012, 7:51 AM
Please download from www.lundin.info and not from this article that still has source from the year 2002!
veeresh diggiPosted Mar 7, 2012, 12:02 AM
Hi All, I am using info.lundin.Math.dll for parsing the expression like (X*4)/100 x is added to hash table like h.Add( "x", "100" ); But I am getting the error saying No value associated with x. could you please help me why I am getting this error, how do i need to resolve this Any help is appreciated. waiting for the feedback
Juan CarlosPosted Aug 4, 2011, 11:19 AM
EXCELENTE
YiPosted Jun 3, 2011, 4:35 AM
It's nice! And the parsing speed is super fast!!! As I tested, it is 6 times faster than Flee, another expression evaluator component.
worker dbPosted Oct 20, 2010, 10:32 AM
Really great job. How about string expressions parser. For instance ("Jack" == "Jack") or ("C#" != "c#")
EricPosted Aug 1, 2010, 8:53 PM
I tried about a half dozen math libraries, even pay ones, and this library was the ONLY ONE that worked under all circiumstances! Rather that I was using spaces or not (several choked on spaces), rather I was using multiple variables that did not exist in the actual formula (think blanket mass-variables and adding all to run differnet formulas every time), and the final piece that only your library supported: the ability to re-use variables more than once! Surpisingly, several other libraries did not allow that. I'm using in a C# 4.0 project. I noticed it was targeted for .net 1.0 when it was compiled. That's old! I've snag the code as well. Thank you again! If there was a "Donation" button, I would donate!
cizmeliPosted Dec 25, 2009, 3:37 AM
doesnt work for floating points
evgPosted Jun 20, 2009, 7:38 AM
http://ssss35.blogspot.com/2009/06/math-parse-expressions-c-java.html
P LPosted May 18, 2009, 7:37 AM
Please download the code from the authors website: http://www.lundin.info/mathparser.aspx It contains some updates and also the source code, however it has not been maintained since 2004. There are alternatives such as for example wrapping the JScript eval function using the jsc compiler in the .net framework.
ciptoPosted Jan 24, 2008, 3:13 AM
thank you lundin :D thisis what i need, to change from math expression string to double or the result thanks brother
Martin OrtizeditedPosted Oct 9, 2007, 2:01 PMEdited Oct 9, 2007, 2:02 PM
For example ExpressionParser parser = new ExpressionParser(); // Create a hashtable to hold values Hashtable h = new Hashtable(); // Add variables and values to hashtable h.Add("x", 1.ToString()); // BOOLEAN "TRUE" h.Add("y", 1.ToString()); // BOOLEAN "TRUE" h.Add("z", 0.ToString()); // BOOLEAN "FALSE" // Parse and write the result double result = parser.Parse("((x || y) && !z)", h); // YOUR BOOLEAN EXPRESSION.... IF result is 1.0 than expression evaluated to true, if it is 0.0 than expression evaluated to false
Martin OrtizPosted Oct 9, 2007, 1:55 PM
Clean and simple to use....super great! thanks.
Vitor PereiraPosted Jun 22, 2007, 12:18 PM
Expressions with decimal values are not allowed example: y = 2.0*Sin(x) is processed like y = 20*Sin(x) and y = 2,0*Sin(x) causes exception