Click here to Skip to main content
Click here to Skip to main content

Math Parser

, 25 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Solve mathematical equations


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." );


  • 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;


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


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


About the Author


Germany Germany
No Biography provided

Comments and Discussions

QuestionHow to add % Pinmembererror_5057-Sep-13 23:27 
Question++ | +- | -- | -+ can occur Pinmembermirtes_992-Jun-13 4:03 
GeneralClaro y sencillo PinmemberMember 42650077-Sep-12 11:20 
QuestionSolution General, thanks for it! PinmemberMember 773069628-Sep-11 14:13 
QuestionRe: Problem with expression PinmemberMember 77306968-Sep-11 17:36 
GeneralMy vote of 1 Pinmembersepehr101417-Jul-11 5:05 
QuestionMultiplying by 0.6 PinmemberMember 427553922-Jul-10 12:22 
General2*8,7-(24,8+1500*0,8*0,00004-0,04*25-2*LOG(150*25)) Pinmemberfdepijper@ziggo.nl8-Jul-10 19:27 
GeneralRe: 2*8,7-(24,8+1500*0,8*0,00004-0,04*25-2*LOG(150*25)) PinmemberBiedermannS14-Mar-13 21:26 
GeneralIgnoring comma when called by SQL Assembly PinmemberLeo Kruz25-Nov-09 2:59 
GeneralThe Parser does not respect presedence Pinmemberher´minator4-Mar-09 20:20 
General(exp(2)) not working PinmemberTominatorBelgium3-Mar-09 6:59 
Generalcos(8) Pinmemberbey0nd8511-Jan-09 23:08 
Question10*20+5*10 - No Result ? PinmemberVictor Vanchesa15-Nov-07 11:42 
Generalgift:binary/hexadecimal and a bug... PinmemberMETRAWARE5-Nov-07 6:45 
GeneralRe: gift:binary/hexadecimal and a bug... Pinmembererror_50528-Aug-13 3:25 
GeneralProblem with expression PinmemberAvrin174-Oct-07 0:12 
GeneralRe: Problem with expression PinmemberMember 773069617-Aug-11 16:24 
Questionln(exp(1)) evaluating correctly for you? PinmemberBreakableC21-Sep-07 1:13 
GeneralACOS, ASIN , ATAN error Pinmemberroksy30-Aug-07 22:53 
QuestionMathieuMathParser add to refrence list. Pinmemberjad_jabi1-Aug-07 1:10 
QuestionProblem with EMT64/AMD64 Extensions? PinmemberDocEvil796-Nov-06 7:51 
AnswerRe: Problem with EMT64/AMD64 Extensions? Pinmemberfdafdafdsa16-Nov-06 15:37 
AnswerRe: Problem with EMT64/AMD64 Extensions? PinmemberXynratron15-Mar-07 2:53 
QuestionError In Program Pinmembersis138212-Sep-06 8:41 
AnswerRe: Error In Program PinmemberParatrooper66612-Sep-06 11:55 
AnswerRe: Error In Program PinmemberMember 77306965-Oct-11 12:36 
GeneralLogical operators support PinmemberFelin4-Aug-06 5:56 
GeneralMore problems with floating point PinmemberBreakableC16-May-06 4:08 
QuestionRe: More problems with floating point PinmemberParatrooper66618-May-06 11:23 
AnswerRe: More problems with floating point PinmemberBreakableC18-May-06 23:05 
NewsRe: More problems with floating point PinmemberLmello7-Jun-06 16:09 
GeneralRe: More problems with floating point Pinmemberschubb4-Sep-06 6:54 
AnswerRe: More problems with floating point Pinmemberschubb4-Sep-06 7:26 
GeneralRe: More problems with floating point PinmemberParatrooper6664-Sep-06 11:21 
GeneralRe: More problems with floating point Pinmemberschubb4-Sep-06 22:31 
GeneralRe: More problems with floating point PinmemberParatrooper6665-Sep-06 10:26 
Questionhow to use the dll? Pinmemberewighell28-Apr-06 21:56 
AnswerRe: how to use the dll? PinmemberParatrooper66630-Apr-06 11:59 
GeneralUse CultureInfo.InvariantCulture. Pinmemberwout de zeeuw25-Aug-05 8:41 
GeneralError Pinmemberstenflo24-Aug-05 20:41 
GeneralRe: Error PinmemberParatrooper66625-Aug-05 9:10 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 25 Aug 2005
Article Copyright 2005 by Paratrooper666
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid