15,877,573 members
Articles / Programming Languages / C#
Tip/Trick

# Math Parser .NET C#

Rate me:
28 Feb 2014GPL31 min read 183.4K   13.1K   71   37
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)

## Introduction

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.

## Background

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

## How it work

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:

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

## Using the code

Example:

C#
```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))))))";

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

## Features

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.

Constants:

• 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):

C#
`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.

## History

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

Written By
Kazakstan
The pragmatic software developer who has strong analytical and problem-solving skills aimed at improving the efficiency of the solutions implemented. His motto is 'quality, agile and performance'.

Prefers a 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 a well-structured solution for a poorly defined problem.

 First PrevNext
 AWESOME! Inf0rmatix19-Apr-18 1:36 Inf0rmatix 19-Apr-18 1:36
 Support for comma functions like min(x,y) Member 1358849521-Dec-17 5:51 Member 13588495 21-Dec-17 5:51
 Operators followed by + or - Member 119563136-Dec-17 8:21 Member 11956313 6-Dec-17 8:21
 NewFeatures: ConditionalSupport, CommaSeparatedFunctionArgList, Variables, RPNCaching, PostfixFunctions and more Mathew Sachin10-Oct-15 7:30 Mathew Sachin 10-Oct-15 7:30
 RPN export compsystems6-Sep-15 3:23 compsystems 6-Sep-15 3:23
 it's good chagawo1-Sep-15 1:04 chagawo 1-Sep-15 1:04
 Multi-argument functions lianaent15-Mar-15 5:04 lianaent 15-Mar-15 5:04
 Re: Multi-argument functions lianaent15-Mar-15 8:29 lianaent 15-Mar-15 8:29
 Re: Multi-argument functions lianaent15-Mar-15 11:04 lianaent 15-Mar-15 11:04
 Re: Multi-argument functions Mathew Sachin12-Oct-15 21:42 Mathew Sachin 12-Oct-15 21:42
 Conditional Expressions possible? mbs-systems14-Feb-15 12:54 mbs-systems 14-Feb-15 12:54
 Re: Conditional Expressions possible? Mathew Sachin18-Oct-15 6:44 Mathew Sachin 18-Oct-15 6:44
 Re: Conditional Expressions possible? Member 1162037220-Nov-15 3:31 Member 11620372 20-Nov-15 3:31
 Tests with Log function seem wrong Lord of Scripts5-Feb-15 10:00 Lord of Scripts 5-Feb-15 10:00
 Now supporting Complex Numbers Lord of Scripts2-Feb-15 8:39 Lord of Scripts 2-Feb-15 8:39
 Symbol transformation Lord of Scripts29-Jan-15 8:31 Lord of Scripts 29-Jan-15 8:31
 Re: Symbol transformation Mathew Sachin18-Oct-15 6:51 Mathew Sachin 18-Oct-15 6:51
 Excellent job jigoch19-Dec-14 5:28 jigoch 19-Dec-14 5:28
 Great Job! miro125-Mar-14 3:03 miro12 5-Mar-14 3:03
 Re: Great Job! Yerzhan Kalzhani24-Mar-14 5:35 Yerzhan Kalzhani 24-Mar-14 5:35
 My vote of 4 Florian Rappl29-Aug-12 8:50 Florian Rappl 29-Aug-12 8:50
 Re: My vote of 4 Yerzhan Kalzhani29-Aug-12 19:34 Yerzhan Kalzhani 29-Aug-12 19:34
 Re: My vote of 4 Florian Rappl29-Aug-12 21:27 Florian Rappl 29-Aug-12 21:27
 Re: My vote of 4 Andreas Gieriet27-Jan-13 8:19 Andreas Gieriet 27-Jan-13 8:19
 Valid Expression Ixus929-Feb-16 17:48 Ixus9 29-Feb-16 17:48
 Last Visit: 31-Dec-99 18:00     Last Update: 13-Apr-24 13:02 Refresh 12 Next ᐅ