/Math-expression-parser

An algorithm that is able to fully interpret and solve any mathematical expression.

Primary LanguageDMIT LicenseMIT

math-expression-parser

What we have here is an algorithm that is able to fully interpret and solve any mathematical expression. It works by parsing the mathematical expression which is received as a string, it does it recursively.

It first organizes the expression by removing spaces, doubled signs (e.g. "--") and also by replacing the constants of euler and pi. Then it solves all logarithms, then it finds all nested parenthesis and solves them recursively, then it solves all trigonometric functions, then it solves all factorials, then it solves all powers and roots, then it solves all products and quotients and finally it solves all sums and differences.

You just need to make sure the expression was typed correctly, these are the rules:

1- trigs and logs must be written in lower case, as in log(2.0, 10.0), in which 2.0 is the base, or as in sin(4.5), in which 4.5 is in degrees, not radians.

2- use a . to write decimal numbers, meaning you should write 1.2 and not 1,2.

3- powers must be written using the ^ operator, as in 2.0^3.0, which means 2.0 to the power of 3.0.

4- roots must be written using the r operator, as in 8.0r3.0, which means the cubic root of 8.0.

5- you can use parentheses as you wish, just make sure you don't forget to open or close a pair of parentheses, as in ((2 + x), which is missing the closing parentheses.

6- it may work most times if you type values as integers, such as 3, but it is safer to always type them as floats, such as 3.0, just in case.

Compile it with "dmd mathexpressionparser.d -m64 -O -g". Then just run it and play with it, if you do anything that results in NaN (not-a-number) then it will display a message saying you've mistyped something.

Here are some examples:

1: sec(sin(3.0) * cos(x) / tan(2.0 * x - 6)) - csc(x) + cot((x - 2.0) * 3.0)

2: (x * 100.0 - 550.0)!

3: 3.0 * log(2.0, x) - 4.5

4: 3.0 ^ x - e / (x ^ e)

5: (8.0 - x + pi) * x - ((x + 7.0) * 2.0)

6: (-x)r2.0