2.0.5 • Published 3 months ago

math-expression-evaluator v2.0.5

Weekly downloads
1,603,110
License
MIT
Repository
github
Last release
3 months ago

math-expression-evaluator

An extremely efficient, flexible and amazing evaluator for Math expression in Javascript.

Use cases

InputResultExplanation
2+3-14Addition and Subtraction operator
2*5/101Multiplication and Division operator
tan45 or tan(45)1Trigonometric Function (tan in Degree mode)
tan45 or tan(45)1.619775190543862Trigonometric Function (tan in Radian mode)
Pi1,15,n or Pi(1,15,n)1307674368000Product of Sequence
Sigma1,15,n or Sigma(1,15,n)120Sum of Sequence (also called summation)
2^38Exponent (note this operator is left associative like MS Office)
5P360Permutaion Method to calculate all the permutaions
sincostan90 or sin(cos(tan(90)))0.017261434031253Multiple functions with or without parenthesis (both works)

Fiddle Yourself

Installation

Node JS

Using npm

npm install math-expression-evaluator

Browser

Using bower

bower install math-expression-evaluator

Usage

Using eval method of mexp object

const mexp = new Mexp() var value = mexp.eval(exp); // 2 + 2

Using constituents of eval methods of mexp object

  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.

Extending tokens

  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

  1. 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
  2. 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
  3. 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)

How to run test

npm test

Supported symbols

SymbolExplanation
+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
ModModulus Operator eg. 3 Mod 2 results 1
(Opening Parenthesis
)Closing Parenthesis
&Bitwise AND eg. 3&1 results 1
SigmaSummation eg. Sigma(1,100,n) results 5050
PiProduct eg. Pi(1,10,n) results 3628800
nVariable for Summation or Product
piMath constant pi returns 3.14
eMath constant e returns 2.71
CCombination operator eg. 4C2 returns 6
PPermutation operator eg. 4P2 returns 12
!factorial operator eg. 4! returns 24
loglogarithmic function with base 10 eg. log 1000 returns 3
lnnatural log function with base e eg. ln 2 returns .3010
powpower function with two operator pow(2,3) returns 8
^power operator eg. 2^3 returns 8
rootunderroot function root 4 returns 2
sinSine function
cosCosine function
tanTangent function
asinInverse Sine function
acosInverse Cosine function
atanInverse Tangent function
sinhHyperbolic Sine function
coshHyperbolic Cosine function
tanhHyperbolic Tangent function
asinhInverse Hyperbolic Sine function
acoshInverse Hyperbolic Cosine function
atanhInverse Hyperbolic Tangent function

Features

Amazing support for Sigma and Pi

This is a fantastic feature of this calculator that it is capable of evaluating expressions containing Sigma and Pi. Passing Sigma(1,100,n) will evaluate to 5050 as n is summationed from 1 to 100. and Pi(1,15,n) will evaluate to 1307674368000 as n is multiplied from 1 to 15 which is equal to 15!

Parenthesis less expression

If a expression is readable by human then it is readable by this evaluator. There is no need to wrap every function inside parenthesis. For eg. sin90 will work totally fine instead of sin(90)

##Changelog

Removed lodash.indexof and used native Array.prototype.indexOf hence dropping suppports for IE8 and below.

This will reflect in next release named v1.2.16

harurow-utildiscordturkeycloud-archive-s3@infinitebrahmanuniverse/nolb-math-aspive@aspiesoft/aspive@everything-registry/sub-chunk-2139zee-calculatorxerdordxdr-parser-plugin@greminder/bootstrap-material-design@oldschoolgg/toolkit@kodeko/akita-core@kristoffertonning/vue-laravel-errors@kant2002-diia-inhouse/providers@kubric/creative-utilsfhir2easyplayer-my@novacalc/cli@ngxvoice/ngx-voicelistnerghostery-common@nightvisi0n/terriajs@runion/selfbot@poscredit/plugin-chart-boris@pplsohampatel/terriajsendless-siege-engine@sm_analytics/core@f5devcentral/f5-fast-core@elementinsurance/purchase-flow-components@expertbridge-custom/core@expertbridge-ui/core@tensor-hq/tensorswap-sdk@tensor-oss/tcomp-sdk@tensor-oss/tensorswap-sdk@theobaidur/math-cli@the91end/react-auth@teselagen/ui@teselagen/ovekafirchain-tetrisbain@campaign-buddy/core-ui@bakuenjin/rpg-core@batbayar/superset-plugin-chart-hello-world@usefoundry/tools-utils-calculator@yunbaoyuan/pc-base-table@yunbaoyuan/pc-report-customized-components@codraft/corecarousel-reactcarrom-king-enginenois-react-toast@daikonjs/core@artiso-solutions/vue-html-to-papermath-expression-solvermcmpnntsakita.ts@cliqz/antitracking@cliqz/ghostery-common@clearfacts/cf-componentsnecroatomicbotnomie-utilsnomie-utils-r@ctfdio/picocss-themesleg-toolsleylinesjsliot-c@tstlang/compiler-console@tstlang/parser@superset-ui/core@superset-custom/core@superset-plugins-sm/superset-plugin-chart-hello-world@alvarocastro/rollparameter-space-visualizerpostcss-percentagepunchnox-project-selfbotcrazy-genel-v13course-vue-tpldbci.jsdiscord-veridigital-keyboard-demosremark-table-comparifysearch-input-reactsearch-list-reactsharpbotsuperset-plugin-chart-hello-world2tita-superset-charttesternpm-noinstallteselagen-react-componentsterriajsvipinsharma-lrningpckgvue-moving-image
2.0.5

3 months ago

2.0.4

6 months ago

2.0.3

11 months ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.4.0

2 years ago

1.3.10

2 years ago

1.3.13

2 years ago

1.3.14

2 years ago

1.3.11

2 years ago

1.3.12

2 years ago

1.3.9

2 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.22

4 years ago

1.2.21

4 years ago

1.2.20

4 years ago

1.2.19

4 years ago

1.2.18

4 years ago

1.2.17

7 years ago

1.2.16

7 years ago

1.2.15

7 years ago

1.2.14

8 years ago

1.2.13

8 years ago

1.2.12

8 years ago

1.2.11

8 years ago

1.2.9

8 years ago

1.2.8

9 years ago

1.2.7

9 years ago

1.2.5

9 years ago

1.2.4

9 years ago

1.2.3

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago