Click here to Skip to main content
15,888,454 members
Articles / Programming Languages / C#
Article

Math Parser

Rate me:
Please Sign up or sign in to vote.
3.49/5 (24 votes)
25 Aug 2005CPOL 149.7K   5.2K   49   44
Solve mathematical equations

Introduction

This is a parser which uses RegEx to solve mathematical expressions.

How To Use It?

C#
MathParser.Parser p = new MathParser.Parser();
if( p.Evaluate( "-(5-10)^(-1)(3+2(cos(3Pi)+(2+ln(exp(1)))^3))" ) ) 
    Console.WriteLine( p.Result );
else Console.WriteLine( "Error." );

Features

  • Functions
    • abs
    • acos
    • asin
    • atan
    • cos
    • cosh
    • floor
    • ln
    • log
    • sign
    • sin
    • sinh
    • sqrt
    • tan
    • tanh
  • Operators: +, -, *, , ^, !
  • Constants: Pi

Choose between RAD, DEG and GRAD.

C#
p.Mode = MathParser.Mode.RAD;
p.Mode = MathParser.Mode.DEG;
p.Mode = MathParser.Mode.GRAD;

Or:

C#
Parser p = new Parser( MathParser.Mode.DEG );

plain
2(1+3(1e3+1)) = 2*(1+3*(1000+1)) = 2*(1+3(1000+1)) = 2(1+3*(10^3+1)) = 6008

2(1+4(5Pi+2,2e3)(5+2)^3) is the same as 2*(1+4*(5*Pi+2200)*(5+2)^3).

Known Problems

I tested this parser on a German operating system (WinXP).
If I want to convert a string to double (e.g. "3.123") the decimal point must be a "," (Convert.ToDouble("3.123") would throw an Exception).
I'm not sure if this works on an English version of WinXP.

If there are any problems with the decimal point, just report it and I will fix it.

Bug Fixes

  • Fixed some problems with exponents

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Problem with EMT64/AMD64 Extensions? Pin
Lucradev16-Nov-06 15:37
Lucradev16-Nov-06 15:37 
AnswerRe: Problem with EMT64/AMD64 Extensions? Pin
Xynratron15-Mar-07 2:53
Xynratron15-Mar-07 2:53 
QuestionError In Program Pin
sis138212-Sep-06 8:41
sis138212-Sep-06 8:41 
AnswerRe: Error In Program Pin
Paratrooper66612-Sep-06 11:55
Paratrooper66612-Sep-06 11:55 
AnswerRe: Error In Program Pin
Juan David Nicholls5-Oct-11 12:36
Juan David Nicholls5-Oct-11 12:36 
GeneralLogical operators support Pin
Michal Brylka4-Aug-06 5:56
Michal Brylka4-Aug-06 5:56 
GeneralMore problems with floating point Pin
BreakableC16-May-06 4:08
BreakableC16-May-06 4:08 
QuestionRe: More problems with floating point Pin
Paratrooper66618-May-06 11:23
Paratrooper66618-May-06 11:23 
Hello,

I wrote this parser on a german operating system.
It searches for double values like "1,53E-2" or "123,56" etc.
Values like "4.7534" will be ignored.

Replace the code below and the parser should work on your non-german system.

// Solve Sin, Cos, Tan...
Regex regEx = new Regex( @"([a-z]{2,})([\+-]?\d+\.*\d*[eE][\+-]*\d+|[\+-]?\d+\.*\d*)", RegexOptions.IgnoreCase );

// Solve Factorial.
...
regEx = new Regex( @"(\d+\.*\d*[eE][\+-]?\d+|\d+\.*\d*)!" ); // Search for patterns like 5!

// Solve power.
regEx = new Regex( @"\{(.+)\}\^(-?\d+\.*\d*[eE][\+-]?\d+|-?\d+\.*\d*)" ); // Search for patterns like {-5}^-1
regEx = new Regex( @"(\d+\.*\d*e[\+-]?\d+|\d+\.*\d*)\^(-?\d+\.*\d*[eE][\+-]?\d+|-?\d+\.*\d*)" ); // Search for patterns like 5^-1

// Solve multiplication / division.
regEx = new Regex( @"([\+-]?\d+\.*\d*[eE][\+-]?\d+|[\-\+]?\d+\.*\d*)([\/\*])(-?\d+\.*\d*[eE][\+-]?\d+|-?\d+\.*\d*)" );

// Solve addition / subtraction.
regEx = new Regex( @"([\+-]?\d+\.*\d*[eE][\+-]?\d+|[\+-]?\d+\.*\d*)([\+-])(-?\d+\.*\d*[eE][\+-]?\d+|-?\d+\.*\d*)" );

Servus...
Paratrooper.
AnswerRe: More problems with floating point Pin
BreakableC18-May-06 23:05
BreakableC18-May-06 23:05 
NewsRe: More problems with floating point Pin
Lmello7-Jun-06 16:09
Lmello7-Jun-06 16:09 
GeneralRe: More problems with floating point Pin
schubb4-Sep-06 6:54
schubb4-Sep-06 6:54 
AnswerRe: More problems with floating point Pin
schubb4-Sep-06 7:26
schubb4-Sep-06 7:26 
GeneralRe: More problems with floating point Pin
Paratrooper6664-Sep-06 11:21
Paratrooper6664-Sep-06 11:21 
GeneralRe: More problems with floating point Pin
schubb4-Sep-06 22:31
schubb4-Sep-06 22:31 
GeneralRe: More problems with floating point Pin
Paratrooper6665-Sep-06 10:26
Paratrooper6665-Sep-06 10:26 
Questionhow to use the dll? Pin
ewighell28-Apr-06 21:56
ewighell28-Apr-06 21:56 
AnswerRe: how to use the dll? Pin
Paratrooper66630-Apr-06 11:59
Paratrooper66630-Apr-06 11:59 
GeneralUse CultureInfo.InvariantCulture. Pin
wout de zeeuw25-Aug-05 8:41
wout de zeeuw25-Aug-05 8:41 
GeneralError Pin
stenflo24-Aug-05 20:41
stenflo24-Aug-05 20:41 
GeneralRe: Error Pin
Paratrooper66625-Aug-05 9:10
Paratrooper66625-Aug-05 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.