SIGN UP MEMBER LOGIN:    
ARTICLE

.NET Math Expression Parser

Posted by Patrick Lundin Articles | Current Affairs April 22, 2002
This is a math expression parser written in C#, which evaluates mathematical expressions.
Reader Level:
Download Files:
 

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:

!, ==, !=, ||, &&, >, < , >=, <=

Login to add your contents and source code to this article
Article Extensions
Contents added by P L on Mar 07, 2012
Please download from http://www.lundin.info and not from this article that still has source from the year 2002!
Contents added by chirs on Sep 14, 2009
nice
share this article :
post comment
 

Hii All.. Finally I got it solved by myself. The problem was due to Hash table keyword was in upper case letter but while parsing it was converting to lower case and trying to access the value associated with that keyword but it was not getting the value so it was giving the error. I removed convertion to lower case its working fine.. Happy coding...

Posted by veeresh diggi Mar 08, 2012

Please download from www.lundin.info and not from this article that still has source from the year 2002!

Posted by P L Mar 07, 2012

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

Posted by veeresh diggi Mar 07, 2012

EXCELENTE

Posted by Juan Carlos Aug 04, 2011

It's nice! And the parsing speed is super fast!!! As I tested, it is 6 times faster than Flee, another expression evaluator component.

Posted by Yi Jun 03, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor