Click here to Skip to main content
15,867,750 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.2K   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

 
Questionwhat is GRAD mode? Pin
Southmountain30-Jun-22 15:36
Southmountain30-Jun-22 15:36 
Questionknown bug Pin
Member 113264231-Dec-15 20:48
Member 113264231-Dec-15 20:48 
QuestionHow to add % Pin
error_5057-Sep-13 23:27
error_5057-Sep-13 23:27 
Question++ | +- | -- | -+ can occur Pin
mirtes_992-Jun-13 4:03
mirtes_992-Jun-13 4:03 
GeneralClaro y sencillo Pin
Member 42650077-Sep-12 11:20
Member 42650077-Sep-12 11:20 
QuestionSolution General, thanks for it! Pin
Juan David Nicholls28-Sep-11 14:13
Juan David Nicholls28-Sep-11 14:13 
QuestionRe: Problem with expression Pin
Juan David Nicholls8-Sep-11 17:36
Juan David Nicholls8-Sep-11 17:36 
GeneralMy vote of 1 Pin
Sepehr Mohammad17-Jul-11 5:05
Sepehr Mohammad17-Jul-11 5:05 
QuestionMultiplying by 0.6 Pin
Member 427553922-Jul-10 12:22
Member 427553922-Jul-10 12:22 
General2*8,7-(24,8+1500*0,8*0,00004-0,04*25-2*LOG(150*25)) Pin
fdepijper@ziggo.nl8-Jul-10 19:27
fdepijper@ziggo.nl8-Jul-10 19:27 
GeneralRe: 2*8,7-(24,8+1500*0,8*0,00004-0,04*25-2*LOG(150*25)) Pin
BiedermannS14-Mar-13 21:26
BiedermannS14-Mar-13 21:26 
GeneralIgnoring comma when called by SQL Assembly Pin
Leo Kruz25-Nov-09 2:59
Leo Kruz25-Nov-09 2:59 
GeneralThe Parser does not respect presedence Pin
her´minator4-Mar-09 20:20
her´minator4-Mar-09 20:20 
General(exp(2)) not working Pin
TominatorBelgium3-Mar-09 6:59
TominatorBelgium3-Mar-09 6:59 
Generalcos(8) Pin
bey0nd8511-Jan-09 23:08
bey0nd8511-Jan-09 23:08 
Question10*20+5*10 - No Result ? Pin
Victor Vanchesa15-Nov-07 11:42
Victor Vanchesa15-Nov-07 11:42 
Generalgift:binary/hexadecimal and a bug... Pin
ppardo5-Nov-07 6:45
ppardo5-Nov-07 6:45 
GeneralRe: gift:binary/hexadecimal and a bug... Pin
error_50528-Aug-13 3:25
error_50528-Aug-13 3:25 
GeneralProblem with expression Pin
Avrin174-Oct-07 0:12
Avrin174-Oct-07 0:12 
GeneralRe: Problem with expression Pin
Juan David Nicholls17-Aug-11 16:24
Juan David Nicholls17-Aug-11 16:24 
Questionln(exp(1)) evaluating correctly for you? Pin
BreakableC21-Sep-07 1:13
BreakableC21-Sep-07 1:13 
GeneralACOS, ASIN , ATAN error Pin
roksy30-Aug-07 22:53
roksy30-Aug-07 22:53 
QuestionMathieuMathParser add to refrence list. Pin
jad_jabi1-Aug-07 1:10
jad_jabi1-Aug-07 1:10 
QuestionProblem with EMT64/AMD64 Extensions? Pin
DocEvil796-Nov-06 7:51
DocEvil796-Nov-06 7:51 
Hi Paratrooper!

I've a big problem with the math parser.

Something about my environment:
PC 1
- Intel Celeron (32 Bit only)
- Windows XP Pro. with SP2 Deutsch/German, 32 Bit Version
- MS .NET Framework 2.0

PC 2
- Virtual Machine on PC 1 using VMware Workstation 5.5.2
- Ubuntu Linux, 32 Bit Version
- Mono Project 1.1.13.8 .NET Runtime

PC 3
- Intel Pentium D 950 CPU (32 Bit, with 64 Bit Extensions)
- Windows XP Pro. with SP2 Deutsch/German, 32 Bit Version
- MS .NET Framework 2.0

PC 4
- AMD Opteron (32 Bit, with 64 Bit Extensions)
- SuSE Linux 10, 64 Bit Version
- Mono Project 1.1.13.8 .NET Runtime

Problem:
I use the math parser as a simple calculator (console application).
When I try to evaluate the expression "(4264 * 0,052) + 800" on PC1 & PC2 the
result is "1021.728" (correct). The same program returns on PC3 & PC4 the value "222528.0"!
Confused | :confused:

Other Samples:
0,5 * 10 = 5 (On PC1 & PC2)
0,5 * 10 = 50 D'Oh! | :doh: (On PC3 & PC4)


I don't understand these "strange" results.
I've tried to solve the problem but I don't know much about RegEx.

Remark: I've tried to compile the program on all pc's above. Makes no differenceCry | :((

Thanks, Daniel

AnswerRe: Problem with EMT64/AMD64 Extensions? Pin
Lucradev16-Nov-06 15:37
Lucradev16-Nov-06 15:37 

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.