A flexible math expression evaluator
npm install math-expression-evaluator📬 Available for contract or other opportunities(full time):
📧 Email Me
npm install math-expression-evaluator
bower install math-expression-evaluator
const mexp = new Mexp()
var value = mexp.eval(exp); // 2 + 2
1. Create mexp object
const mexp = new Mexp
2. Parse an expression and then add additional detail to the tokens using
var lexed = mexp.lex("expression");
which returns an array of token which will be further processed by methods toPostfix and postfixEval
3. Now, that array is needed to be converted to postfix notation using
var postfixed = mexp.toPostfix(lexed);
which converts the array to postfix notation and return new array
4. Now to get the value of expression use postfixEval
var result = mexp.postfixEval(postfixed);
where result contains the result.
1. Defining a token
A token is defined similar way as 1.x version. You may refer to test file on examples on how to add tokens. Since this package is TS compatible, you will get autocomplete on mexp.addToken
2. Adding tokens using addToken method of mexp object
const mexp = new Mexp()
mexp.addToken([token1, token2]) // tokens once added will be preserved in later evaluations
3. Adding tokens using eval method of mexp object
const mexp = new Mexp()
mexp.eval("expression", [token1, token2]) // tokens once added will be preserved in later evaluations
4. Adding token using constituents of eval method of mexp object
const mexp = new Mexp()
const answer = mexp.postfixEval(mexp.toPostfix(mexp.lexed("expression", [token1, token2]))) // tokens once added will be preserved in later evaluations
console.log(answer)
npm test
|Symbol|Explanation|
|:---:|:---:|
|+| Addition Operator eg. 2+3 results 5 |
|-| Subtraction Operator eg. 2-3 results -1 |
|/| Division operator eg 3/2 results 1.5 |
|\| Multiplication Operator eg. 2\3 results 6 |
|Mod| Modulus Operator eg. 3 Mod 2 results 1 |
|(| Opening Parenthesis |
|)| Closing Parenthesis |
|&| Bitwise AND eg. 3&1 results 1 |
|Sigma| Summation eg. Sigma(1,100,n) results 5050 |
|Pi| Product eg. Pi(1,10,n) results 3628800 |
|n| Variable for Summation or Product |
|pi| Math constant pi returns 3.14 |
|e| Math constant e returns 2.71 |
|C| Combination operator eg. 4C2 returns 6 |
|P| Permutation operator eg. 4P2 returns 12 |
|!| factorial operator eg. 4! returns 24 |
|log| logarithmic function with base 10 eg. log 1000 returns 3 |
|ln| natural log function with base e eg. ln 2 returns .3010 |
|pow| power function with two operator pow(2,3) returns 8 |
|^| power operator eg. 2^3 returns 8 |
|root| underroot function root 4 returns 2 |
|sin| Sine function |
|cos| Cosine function |
|tan| Tangent function |
|asin| Inverse Sine function |
|acos| Inverse Cosine function |
|atan| Inverse Tangent function |
|sinh| Hyperbolic Sine function |
|cosh| Hyperbolic Cosine function |
|tanh| Hyperbolic Tangent function |
|asinh| Inverse Hyperbolic Sine function |
|acosh| Inverse Hyperbolic Cosine function |
|atanh| Inverse Hyperbolic Tangent function |
Sigma(1,100,n) will evaluate to 5050 as n is summationed from 1 to 100.##Changelog