Click here to Skip to main content
15,072,134 members
Articles / Programming Languages / C#
Posted 14 May 2012


71 bookmarked

Math Parser .NET C#

Rate me:
Please Sign up or sign in to vote.
4.77/5 (39 votes)
28 Feb 2014GPL31 min read
This is light, fast and simple to understand mathematical parser designed in one class, which receives as input a mathematical expression (System.String) and returns the output value (System.Double)

Image 1


This is light, fast and simple to understand mathematical parser designed in one class, which receives as input a mathematical expression (System.String) and returns the output value (System.Double). For example, if your input string is "√(625)+25*(3/3)" then parser returns double value — 50. 


The idea was to create a string calculator for educational goals.  

How it work   

 Image 2

For more please look at the code, I tried to explain the work of the parser in the comments and how it can be changed and extended.  

Convert to RPN:

Image 3

* Input operators are replaced by the corresponding token (its necessary to distinguish unary from binary). 

Using the code  


public static void Main()
        MathParser parser = new MathParser();
    string s1 = "pi+5*5+5*3-5*5-5*3+1E1";
    string s2 = "sin(cos(tg(sh(ch(th(100))))))";
        bool isRadians = false;
        double d1 = parser.Parse(s1, isRadians);
    double d2 = parser.Parse(s2, isRadians);

        Console.WriteLine(d1); // 13.141592...
    Console.WriteLine(d2); // 0.0174524023974442


Ariphmetical operators :

  • () — parentheses;  
  • + —  plus (a + b); 
  • - — minus (a - b); 
  • * — multiplycation symbol (a * b); 
  • / — divide symbol (a / b); 
  • ^ — degree symbol (a ^ b).  

Functions :    

Trigonometric functions: 

  • sin(x);
  • cos(x);
  • tg(x);  
  • ctg(x). 

Hyperbolic functions:

  • sh(x);
  • ch(x);
  • th(x).  

Other functions: 
  • √(a), sqrt(a) — square root of a number; 
  • exp(x)  — exponential function (or just use e^x);  
  • (a)log(b) — logarithm; 
  • ln(x) — natural logarithm; 
  • abs(x) — absolute value.  


  • pi — 3.14159265...;  
  • e — 2.71828183....  

Arguments of the trigonometric functions can be expressed as radians (true) or degrees (false) by sending appropriate bool value to parse method. Example (as radians):   

parser.Parse("sin(90)", true);

Work with any char decimal separator in real number (regional settings).

New operators, functions and constants can be easily added to the code. 

This parser is simple (special designed in single class), convenient for the further implementation, expansion and modification.      

Points of Interest     

I better understand how parsers work and learned about the reverse-polish notation.


  • 2012/05/09: released the source code (1_0); 
  • 2012/06/07: optimized parser (1_2);    
  • 2014/02/28: rewritten version (1_4). 


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Kazakstan Kazakstan
His name is Yerzhan Kalzhani. He's a pragmatic software developer who loves tasks that are interesting and challenging. His motto in software developing: "Quality, Performance and Agile".

He prefers systematic approach when solving a problem, so he thinks that every good developer, first of all, should be able to manage the complexity of a software system and produce the well-structured solution for a poorly defined problem.

Comments and Discussions

PraiseAWESOME! Pin
Inf0rmatix19-Apr-18 1:36
MemberInf0rmatix19-Apr-18 1:36 
QuestionSupport for comma functions like min(x,y) Pin
Member 1358849521-Dec-17 5:51
MemberMember 1358849521-Dec-17 5:51 
QuestionOperators followed by + or - Pin
Member 119563136-Dec-17 8:21
MemberMember 119563136-Dec-17 8:21 
NewsNewFeatures: ConditionalSupport, CommaSeparatedFunctionArgList, Variables, RPNCaching, PostfixFunctions and more Pin
Mathew Sachin10-Oct-15 7:30
MemberMathew Sachin10-Oct-15 7:30 
QuestionRPN export Pin
compsystems6-Sep-15 3:23
Membercompsystems6-Sep-15 3:23 
Questionit's good Pin
chagawo1-Sep-15 1:04
Memberchagawo1-Sep-15 1:04 
QuestionMulti-argument functions Pin
lianaent15-Mar-15 5:04
Memberlianaent15-Mar-15 5:04 
AnswerRe: Multi-argument functions Pin
lianaent15-Mar-15 8:29
Memberlianaent15-Mar-15 8:29 
GeneralRe: Multi-argument functions Pin
lianaent15-Mar-15 11:04
Memberlianaent15-Mar-15 11:04 
GeneralRe: Multi-argument functions Pin
Mathew Sachin12-Oct-15 21:42
MemberMathew Sachin12-Oct-15 21:42 
QuestionConditional Expressions possible? Pin
mbs-systems14-Feb-15 12:54
Membermbs-systems14-Feb-15 12:54 
AnswerRe: Conditional Expressions possible? Pin
Mathew Sachin18-Oct-15 6:44
MemberMathew Sachin18-Oct-15 6:44 
AnswerRe: Conditional Expressions possible? Pin
Member 1162037220-Nov-15 3:31
MemberMember 1162037220-Nov-15 3:31 
BugTests with Log function seem wrong Pin
Lord of Scripts5-Feb-15 10:00
MemberLord of Scripts5-Feb-15 10:00 
NewsNow supporting Complex Numbers Pin
Lord of Scripts2-Feb-15 8:39
MemberLord of Scripts2-Feb-15 8:39 
QuestionSymbol transformation Pin
Lord of Scripts29-Jan-15 8:31
MemberLord of Scripts29-Jan-15 8:31 
AnswerRe: Symbol transformation Pin
Mathew Sachin18-Oct-15 6:51
MemberMathew Sachin18-Oct-15 6:51 
QuestionExcellent job Pin
jigoch19-Dec-14 5:28
Memberjigoch19-Dec-14 5:28 
GeneralGreat Job! Pin
miro125-Mar-14 3:03
Membermiro125-Mar-14 3:03 
GeneralRe: Great Job! Pin
kirnbas24-Mar-14 5:35
Memberkirnbas24-Mar-14 5:35 
GeneralMy vote of 4 Pin
Florian Rappl29-Aug-12 8:50
professionalFlorian Rappl29-Aug-12 8:50 
GeneralRe: My vote of 4 Pin
kirnbas29-Aug-12 19:34
Memberkirnbas29-Aug-12 19:34 
GeneralRe: My vote of 4 Pin
Florian Rappl29-Aug-12 21:27
professionalFlorian Rappl29-Aug-12 21:27 
GeneralRe: My vote of 4 Pin
Andreas Gieriet27-Jan-13 8:19
professionalAndreas Gieriet27-Jan-13 8:19 
GeneralValid Expression Pin
Ixus929-Feb-16 17:48
MemberIxus929-Feb-16 17:48 

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.