Click here to Skip to main content
Licence CPOL
First Posted 29 Jul 2005
Views 77,826
Downloads 2,174
Bookmarked 39 times

Math Parser

By | 25 Aug 2005 | Article
Solve mathematical equations

Introduction

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

How To Use It?

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.

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

Or:

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

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)

About the Author

Paratrooper666



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSolution General, thanks for it! PinmemberMember 773069614:13 28 Sep '11  
QuestionRe: Problem with expression PinmemberMember 773069617:36 8 Sep '11  
GeneralMy vote of 1 Pinmembersepehr10145:05 17 Jul '11  
QuestionMultiplying by 0.6 PinmemberMember 427553912:22 22 Jul '10  
General2*8,7-(24,8+1500*0,8*0,00004-0,04*25-2*LOG(150*25)) Pinmemberfdepijper@ziggo.nl19:27 8 Jul '10  
GeneralIgnoring comma when called by SQL Assembly PinmemberLeo Kruz2:59 25 Nov '09  
GeneralThe Parser does not respect presedence Pinmemberher´minator20:20 4 Mar '09  
General(exp(2)) not working PinmemberTominatorBelgium6:59 3 Mar '09  
Generalcos(8) Pinmemberbey0nd8523:08 11 Jan '09  
Question10*20+5*10 - No Result ? PinmemberVictor Vanchesa11:42 15 Nov '07  
Generalgift:binary/hexadecimal and a bug... PinmemberMETRAWARE6:45 5 Nov '07  
GeneralProblem with expression PinmemberAvrin170:12 4 Oct '07  
GeneralRe: Problem with expression PinmemberMember 773069616:24 17 Aug '11  
Questionln(exp(1)) evaluating correctly for you? PinmemberBreakableC1:13 21 Sep '07  
GeneralACOS, ASIN , ATAN error Pinmemberroksy22:53 30 Aug '07  
QuestionMathieuMathParser add to refrence list. Pinmemberjad_jabi1:10 1 Aug '07  
QuestionProblem with EMT64/AMD64 Extensions? PinmemberDocEvil797:51 6 Nov '06  
AnswerRe: Problem with EMT64/AMD64 Extensions? Pinmemberfdafdafdsa15:37 16 Nov '06  
AnswerRe: Problem with EMT64/AMD64 Extensions? PinmemberXynratron2:53 15 Mar '07  
QuestionError In Program Pinmembersis13828:41 12 Sep '06  
AnswerRe: Error In Program PinmemberParatrooper66611:55 12 Sep '06  
AnswerRe: Error In Program PinmemberMember 773069612:36 5 Oct '11  
GeneralLogical operators support PinmemberFelin5:56 4 Aug '06  
GeneralMore problems with floating point PinmemberBreakableC4:08 16 May '06  
QuestionRe: More problems with floating point PinmemberParatrooper66611:23 18 May '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 25 Aug 2005
Article Copyright 2005 by Paratrooper666
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid