Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

math-expression-evaluator

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

math-expression-evaluator - npm Package Compare versions

Comparing version 1.2.17 to 1.2.18

.eslintrc.json

937

dist/browser/math-expression-evaluator.js
/** math-expression-evaluator version 1.2.17
Dated:2017-04-28 */
Dated:2017-05-05 */

@@ -47,433 +47,522 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mexp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

},{"./postfix_evaluator.js":5}],2:[function(require,module,exports){
var Mexp=require('./math_function.js');
function inc(arr,val){
for(var i=0;i<arr.length;i++)
arr[i]+=val;
return arr;
}
var token=['sin','cos','tan','pi','(',')','P','C',
'asin','acos','atan','7','8','9','int',
'cosh','acosh','ln','^','root','4','5','6','/','!',
'tanh','atanh','Mod','1','2','3','*',
'sinh','asinh','e','log','0','.','+','-',',','Sigma','n','Pi','pow'];
var show=['sin','cos','tan','&pi;','(',')','P','C',
'asin','acos','atan','7','8','9','Int',
'cosh','acosh',' ln','^','root','4','5','6','&divide;','!',
'tanh','atanh',' Mod ','1','2','3','&times;',
'sinh','asinh','e',' log','0','.','+','-',',','&Sigma;','n','&Pi;','pow'];
var eva=[Mexp.math.sin,Mexp.math.cos,Mexp.math.tan,'PI','(',')',Mexp.math.P,Mexp.math.C,
Mexp.math.asin,Mexp.math.acos,Mexp.math.atan,'7','8','9',Math.floor,
Mexp.math.cosh,Mexp.math.acosh,Math.log,Math.pow,Math.sqrt,'4','5','6',Mexp.math.div,Mexp.math.fact,
Mexp.math.tanh,Mexp.math.atanh,Mexp.math.mod,'1','2','3',Mexp.math.mul,
Mexp.math.sinh,Mexp.math.asinh,'E',Mexp.math.log,'0','.',Mexp.math.add,Mexp.math.sub,',',Mexp.math.sigma,'n',Mexp.math.Pi,Math.pow];
var preced={0:11,1:0,2:3,3:0,4:0,5:0,6:0,7:11,8:11,9:1,10:10,11:0,12:11,13:0};
var type=[0,0,0,3,4,5,10,10,
0,0,0,1,1,1,0,
0,0,0,10,0,1,1,1,2,7,
0,0,2,1,1,1,2,
0,0,3,0,1,6,9,9,11,12,13,12,8];
/*
0 : function with syntax function_name(Maths_exp)
1 : numbers
2 : binary operators like * / Mod left associate and same precedence
3 : Math constant values like e,pi,Cruncher ans
4 : opening bracket
5 : closing bracket
6 : decimal
7 : function with syntax (Math_exp)function_name
8: function with syntax function_name(Math_exp1,Math_exp2)
9 : binary operator like +,-
10: binary operator like P C or ^
11: ,
12: function with , seperated three parameters
13: variable of Sigma function
*/
var type0={0:true,1:true,3:true,4:true,6:true,8:true,9:true,12:true,13:true},//type2:true,type4:true,type9:true,type11:true,type21:true,type22
type1={0:true,1:true,2:true,3:true,4:true,5:true,6:true,7:true,8:true,9:true,10:true,11:true,12:true,13:true},//type3:true,type5:true,type7:true,type23
type_1={0:true,3:true,4:true,8:true,12:true,13:true},
empty={},
type_3={0:true,1:true,3:true,4:true,6:true,8:true,12:true,13:true},//type_5:true,type_7:true,type_23
type6={1:true},
newAr=[[],
["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","e","0",".",",","n"],
["pi","ln","Pi"],
["sin","cos","tan","Del","int","Mod","log","pow"],
["asin","acos","atan","cosh","root","tanh","sinh"],
["acosh","atanh","asinh","Sigma"]];
function match(str1,str2,i,x){
for(var f=0;f<x;f++){
if (str1[i+f]!==str2[f])
return false;
}
return true;
}
Mexp.addToken=function(tokens){
for(i=0;i<tokens.length;i++){
x=tokens[i].token.length;
var temp=-1;
var Mexp = require('./math_function.js')
function inc (arr, val) {
for (var i = 0; i < arr.length; i++) {
arr[i] += val
}
return arr
}
var token = ['sin', 'cos', 'tan', 'pi', '(', ')', 'P', 'C',
'asin', 'acos', 'atan', '7', '8', '9', 'int',
'cosh', 'acosh', 'ln', '^', 'root', '4', '5', '6', '/', '!',
'tanh', 'atanh', 'Mod', '1', '2', '3', '*',
'sinh', 'asinh', 'e', 'log', '0', '.', '+', '-', ',', 'Sigma', 'n', 'Pi', 'pow']
var show = ['sin', 'cos', 'tan', '&pi;', '(', ')', 'P', 'C',
'asin', 'acos', 'atan', '7', '8', '9', 'Int',
'cosh', 'acosh', ' ln', '^', 'root', '4', '5', '6', '&divide;', '!',
'tanh', 'atanh', ' Mod ', '1', '2', '3', '&times;',
'sinh', 'asinh', 'e', ' log', '0', '.', '+', '-', ',', '&Sigma;', 'n', '&Pi;', 'pow']
var eva = [Mexp.math.sin, Mexp.math.cos, Mexp.math.tan, 'PI', '(', ')', Mexp.math.P, Mexp.math.C,
Mexp.math.asin, Mexp.math.acos, Mexp.math.atan, '7', '8', '9', Math.floor,
Mexp.math.cosh, Mexp.math.acosh, Math.log, Math.pow, Math.sqrt, '4', '5', '6', Mexp.math.div, Mexp.math.fact,
Mexp.math.tanh, Mexp.math.atanh, Mexp.math.mod, '1', '2', '3', Mexp.math.mul,
Mexp.math.sinh, Mexp.math.asinh, 'E', Mexp.math.log, '0', '.', Mexp.math.add, Mexp.math.sub, ',', Mexp.math.sigma, 'n', Mexp.math.Pi, Math.pow]
var preced = {
0: 11,
1: 0,
2: 3,
3: 0,
4: 0,
5: 0,
6: 0,
7: 11,
8: 11,
9: 1,
10: 10,
11: 0,
12: 11,
13: 0
}
var type = [0, 0, 0, 3, 4, 5, 10, 10,
0, 0, 0, 1, 1, 1, 0,
0, 0, 0, 10, 0, 1, 1, 1, 2, 7,
0, 0, 2, 1, 1, 1, 2,
0, 0, 3, 0, 1, 6, 9, 9, 11, 12, 13, 12, 8]
/*
0 : function with syntax function_name(Maths_exp)
1 : numbers
2 : binary operators like * / Mod left associate and same precedence
3 : Math constant values like e,pi,Cruncher ans
4 : opening bracket
5 : closing bracket
6 : decimal
7 : function with syntax (Math_exp)function_name
8: function with syntax function_name(Math_exp1,Math_exp2)
9 : binary operator like +,-
10: binary operator like P C or ^
11: ,
12: function with , seperated three parameters
13: variable of Sigma function
*/
var type0 = {
0: true,
1: true,
3: true,
4: true,
6: true,
8: true,
9: true,
12: true,
13: true
} // type2:true,type4:true,type9:true,type11:true,type21:true,type22
var type1 = {
0: true,
1: true,
2: true,
3: true,
4: true,
5: true,
6: true,
7: true,
8: true,
9: true,
10: true,
11: true,
12: true,
13: true
} // type3:true,type5:true,type7:true,type23
var type1Asterick = {
0: true,
3: true,
4: true,
8: true,
12: true,
13: true
}
var empty = {}
var type3Asterick = {
0: true,
1: true,
3: true,
4: true,
6: true,
8: true,
12: true,
13: true
} // type_5:true,type_7:true,type_23
var type6 = {
1: true
}
var newAr = [
[],
['1', '2', '3', '7', '8', '9', '4', '5', '6', '+', '-', '*', '/', '(', ')', '^', '!', 'P', 'C', 'e', '0', '.', ',', 'n'],
['pi', 'ln', 'Pi'],
['sin', 'cos', 'tan', 'Del', 'int', 'Mod', 'log', 'pow'],
['asin', 'acos', 'atan', 'cosh', 'root', 'tanh', 'sinh'],
['acosh', 'atanh', 'asinh', 'Sigma']
]
//newAr is a specially designed data structure in which 1D array at location one of 2d array has all string with length 1 2 with 2 and so on
function match (str1, str2, i, x) {
for (var f = 0; f < x; f++) {
if (str1[i + f] !== str2[f]) {
return false
}
}
return true
}
Mexp.addToken = function (tokens) {
for (var i = 0; i < tokens.length; i++) {
var x = tokens[i].token.length
var temp = -1
if (x<newAr.length) //match to check if token is really huge and not existing
//if not checked it will break in next line as undefined index
for(y=0;y<newAr[x].length;y++){
if (tokens[i].token===newAr[x][y]){
temp=token.indexOf(newAr[x][y]);
break;
}
}
if (temp===-1) {
token.push(tokens[i].token);
type.push(tokens[i].type);
if(newAr.length<=tokens[i].token.length)
newAr[tokens[i].token.length]=[];
newAr[tokens[i].token.length].push(tokens[i].token);
eva.push(tokens[i].value);
show.push(tokens[i].show);
}
else {
token[temp]=tokens[i].token;
type[temp]=tokens[i].type;
eva[temp]=tokens[i].value;
show[temp]=tokens[i].show;
}
}
};
Mexp.lex=function(inp,tokens){
'use strict';
var str=[{type:4,value:"(",show:"(",pre:0}];
var ptc=[]; //Parenthesis to close at the beginning is after one token
var inpStr=inp;
var key;
var pcounter=0;
var allowed=type0;
var bracToClose=0;
var asterick=empty;
var prevKey='';
var i,x,y;
if(typeof tokens!=="undefined")
Mexp.addToken(tokens);
var obj={};
for(i=0;i<inpStr.length;i++){
if (inpStr[i]==' ') {
continue;
}
key='';
sec:for(x=(inpStr.length-i>(newAr.length-2)?newAr.length-1:inpStr.length-i);x>0;x--){
for(y=0;y<newAr[x].length;y++){
if (match(inpStr,newAr[x][y],i,x)){
key=newAr[x][y];
break sec;
}
}
}
i+=key.length-1;
if(key===''){
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}
var index=token.indexOf(key);
var cToken=key;
var cType=type[index];
var cEv=eva[index];
var cPre=preced[cType];
var cShow=show[index];
var pre=str[str.length-1];
for(j=ptc.length;j--;){ //loop over ptc
if(ptc[j]===0){
if([0,2,3,5,9,11,12,13].indexOf(cType)!==-1){
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
str.push({value:")",type:5,pre:0,show:")"});
allowed=type1;
asterick=type_3;
inc(ptc,-1).pop();
}
}
}
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
if(asterick[cType]===true){
cType=2;
cEv=Mexp.math.mul;
cShow="&times;";
cPre=3;
i=i-key.length;
}
obj={value:cEv,type:cType,pre:cPre,show:cShow};
if(cType===0){
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push(obj);
str.push({value:"(",type:4,pre:0,show:"("});
}
else if(cType===1){
if(pre.type===1){
pre.value+=cEv;
inc(ptc,1);
}
else {
str.push(obj);
}
allowed=type1;
asterick=type_1;
}
else if(cType===2){
allowed=type0;
asterick=empty;
inc(ptc,2);
str.push(obj);
}
else if(cType===3){//constant
str.push(obj);
allowed=type1;
asterick=type_3;
}
else if(cType===4){
pcounter+=ptc.length;
ptc=[];
bracToClose++;
allowed=type0;
asterick=empty;
str.push(obj);
}
else if(cType===5){
if(!bracToClose){
throw(new Mexp.exception("Closing parenthesis are more than opening one, wait What!!!"));
}
while(pcounter--){ //loop over ptc
str.push({value:")",type:5,pre:0,show:")"});
}
pcounter=0;
bracToClose--;
allowed=type1;
asterick=type_3;
str.push(obj);
}
else if(cType===6){
if(pre.hasDec){
throw(new Mexp.exception("Two decimals are not allowed in one number"));
}
if(pre.type!==1){
pre={value:0,type:1,pre:0}; //pre needs to be changed as it will the last value now to be safe in later code
str.push(pre);
inc(ptc,-1);
}
allowed=type6;
inc(ptc,1);
asterick=empty;
pre.value+=cEv;
pre.hasDec=true;
}
else if(cType===7){
allowed=type1;
asterick=type_3;
inc(ptc,1);
str.push(obj);
}
if(cType===8){
allowed=type0;
asterick=empty;
inc(ptc,4).push(4);
str.push(obj);
str.push({value:"(",type:4,pre:0,show:"("});
}
else if(cType===9){
if(pre.type===9){
if(pre.value===Mexp.math.add){
pre.value=cEv;
pre.show=cShow;
inc(ptc,1);
}
else if(pre.value===Mexp.math.sub&&cShow==='-'){
pre.value=Mexp.math.add;
pre.show='+';
inc(ptc,1);
}
}
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){//changesign only when negative is found
if(cToken==='-'){//do nothing for + token
//don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
}
}
else{
str.push(obj);
inc(ptc,2);
}
allowed=type0;
asterick=empty;
}
else if(cType===10){
allowed=type0;
asterick=empty;
inc(ptc,2);
str.push(obj);
}
else if(cType===11){
allowed=type0;
asterick=empty;
str.push(obj);
}
else if(cType===12){
allowed=type0;
asterick=empty;
inc(ptc,6).push(6);
str.push(obj);
str.push({value:"(",type:4,pre:0});
}
else if(cType===13){
allowed=type1;
asterick=type_3;
str.push(obj);
}
inc(ptc,-1);
prevKey=key;
}
for(var j=ptc.length;j--;){ //loop over ptc
if(ptc[j]===0){
// newAr is a specially designed data structure in which 1D array at location one of 2d array has all string with length 1 2 with 2 and so on
str.push({value:")",show:")",type:5,pre:3});
inc(ptc,-1).pop();
}
}
if (allowed[5]!==true) {
throw(new Mexp.exception("complete the expression"));
}
while(bracToClose--)
str.push({value:")",show:")",type:5,pre:3});
if (x < newAr.length) { // match to check if token is really huge and not existing
// if not checked it will break in next line as undefined index
for (var y = 0; y < newAr[x].length; y++) {
if (tokens[i].token === newAr[x][y]) {
temp = token.indexOf(newAr[x][y])
break
}
}
}
if (temp === -1) {
token.push(tokens[i].token)
type.push(tokens[i].type)
if (newAr.length <= tokens[i].token.length) {
newAr[tokens[i].token.length] = []
}
newAr[tokens[i].token.length].push(tokens[i].token)
eva.push(tokens[i].value)
show.push(tokens[i].show)
} else {
token[temp] = tokens[i].token
type[temp] = tokens[i].type
eva[temp] = tokens[i].value
show[temp] = tokens[i].show
}
}
}
Mexp.lex = function (inp, tokens) {
'use strict'
var changeSignObj = {
value: Mexp.math.changeSign,
type: 0,
pre: 21,
show: '-'
}
var closingParObj = {
value: ')',
show: ')',
type: 5,
pre: 0
}
var openingParObj = {
value: '(',
type: 4,
pre: 0,
show: '('
}
var str = [openingParObj]
var ptc = [] // Parenthesis to close at the beginning is after one token
var inpStr = inp
var key
var pcounter = 0
var allowed = type0
var bracToClose = 0
var asterick = empty
var prevKey = ''
var i, x, y
if (typeof tokens !== 'undefined') {
Mexp.addToken(tokens)
}
var obj = {}
for (i = 0; i < inpStr.length; i++) {
if (inpStr[i] === ' ') {
continue
}
key = ''
for (x = (inpStr.length - i > (newAr.length - 2) ? newAr.length - 1 : inpStr.length - i); x > 0; x--) {
for (y = 0; y < newAr[x].length; y++) {
if (match(inpStr, newAr[x][y], i, x)) {
key = newAr[x][y]
y = newAr[x].length
x = 0
}
}
}
i += key.length - 1
if (key === '') {
throw (new Mexp.Exception('Can\'t understand after ' + inpStr.slice(i)))
}
var index = token.indexOf(key)
var cToken = key
var cType = type[index]
var cEv = eva[index]
var cPre = preced[cType]
var cShow = show[index]
var pre = str[str.length - 1]
var j
for (j = ptc.length; j--;) { // loop over ptc
if (ptc[j] === 0) {
if ([0, 2, 3, 5, 9, 11, 12, 13].indexOf(cType) !== -1) {
if (allowed[cType] !== true) {
throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
}
str.push(closingParObj)
allowed = type1
asterick = type3Asterick
inc(ptc, -1).pop()
}
} else break
}
if (allowed[cType] !== true) {
throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
}
if (asterick[cType] === true) {
cType = 2
cEv = Mexp.math.mul
cShow = '&times;'
cPre = 3
i = i - key.length
}
obj = {
value: cEv,
type: cType,
pre: cPre,
show: cShow
}
if (cType === 0) {
allowed = type0
asterick = empty
inc(ptc, 2).push(2)
str.push(obj)
str.push(openingParObj)
} else if (cType === 1) {
if (pre.type === 1) {
pre.value += cEv
inc(ptc, 1)
} else {
str.push(obj)
}
allowed = type1
asterick = type1Asterick
} else if (cType === 2) {
allowed = type0
asterick = empty
inc(ptc, 2)
str.push(obj)
} else if (cType === 3) { // constant
str.push(obj)
allowed = type1
asterick = type3Asterick
} else if (cType === 4) {
pcounter += ptc.length
ptc = []
bracToClose++
allowed = type0
asterick = empty
str.push(obj)
} else if (cType === 5) {
if (!bracToClose) {
throw (new Mexp.Exception('Closing parenthesis are more than opening one, wait What!!!'))
}
while (pcounter--) { // loop over ptc
str.push(closingParObj)
}
pcounter = 0
bracToClose--
allowed = type1
asterick = type3Asterick
str.push(obj)
} else if (cType === 6) {
if (pre.hasDec) {
throw (new Mexp.Exception('Two decimals are not allowed in one number'))
}
if (pre.type !== 1) {
pre = {
value: 0,
type: 1,
pre: 0
} // pre needs to be changed as it will the last value now to be safe in later code
str.push(pre)
inc(ptc, -1)
}
allowed = type6
inc(ptc, 1)
asterick = empty
pre.value += cEv
pre.hasDec = true
} else if (cType === 7) {
allowed = type1
asterick = type3Asterick
inc(ptc, 1)
str.push(obj)
}
if (cType === 8) {
allowed = type0
asterick = empty
inc(ptc, 4).push(4)
str.push(obj)
str.push(openingParObj)
} else if (cType === 9) {
if (pre.type === 9) {
if (pre.value === Mexp.math.add) {
pre.value = cEv
pre.show = cShow
inc(ptc, 1)
} else if (pre.value === Mexp.math.sub && cShow === '-') {
pre.value = Mexp.math.add
pre.show = '+'
inc(ptc, 1)
}
} else if (pre.type !== 5 && pre.type !== 7 && pre.type !== 1 && pre.type !== 3 && pre.type !== 13) { // changesign only when negative is found
if (cToken === '-') { // do nothing for + token
// don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed = type0
asterick = empty
inc(ptc, 2).push(2)
str.push(changeSignObj)
str.push(openingParObj)
}
} else {
str.push(obj)
inc(ptc, 2)
}
allowed = type0
asterick = empty
} else if (cType === 10) {
allowed = type0
asterick = empty
inc(ptc, 2)
str.push(obj)
} else if (cType === 11) {
allowed = type0
asterick = empty
str.push(obj)
} else if (cType === 12) {
allowed = type0
asterick = empty
inc(ptc, 6).push(6)
str.push(obj)
str.push(openingParObj)
} else if (cType === 13) {
allowed = type1
asterick = type3Asterick
str.push(obj)
}
inc(ptc, -1)
prevKey = key
}
for (j = ptc.length; j--;) { // loop over ptc
if (ptc[j] === 0) {
str.push(closingParObj)
inc(ptc, -1).pop()
} else break // if it is not zero so before ptc also cant be zero
}
if (allowed[5] !== true) {
throw (new Mexp.Exception('complete the expression'))
}
while (bracToClose--) {
str.push(closingParObj)
}
str.push({type:5,value:")",show:")",pre:0});
// console.log(str);
return new Mexp(str);
};
module.exports=Mexp;
str.push(closingParObj)
// console.log(str);
return new Mexp(str)
}
module.exports = Mexp
},{"./math_function.js":3}],3:[function(require,module,exports){
var Mexp=function(parsed){
this.value=parsed;
var Mexp = function (parsed) {
this.value = parsed
}
};
Mexp.math = {
isDegree: true, // mode of calculator
acos: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x))
},
add: function (a, b) {
return a + b
},
asin: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x))
},
atan: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x))
},
acosh: function (x) {
return Math.log(x + Math.sqrt(x * x - 1))
},
asinh: function (x) {
return Math.log(x + Math.sqrt(x * x + 1))
},
atanh: function (x) {
return Math.log((1 + x) / (1 - x))
},
C: function (n, r) {
var pro = 1
var other = n - r
var choice = r
if (choice < other) {
choice = other
other = r
}
for (var i = choice + 1; i <= n; i++) {
pro *= i
}
return pro / Mexp.math.fact(other)
},
changeSign: function (x) {
return -x
},
cos: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.cos(x)
},
cosh: function (x) {
return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2
},
div: function (a, b) {
return a / b
},
fact: function (n) {
if (n % 1 !== 0) return 'NaN'
var pro = 1
for (var i = 2; i <= n; i++) {
pro *= i
}
return pro
},
inverse: function (x) {
return 1 / x
},
log: function (i) {
return Math.log(i) / Math.log(10)
},
mod: function (a, b) {
return a % b
},
mul: function (a, b) {
return a * b
},
P: function (n, r) {
var pro = 1
for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) {
pro *= i
}
return pro
},
Pi: function (low, high, ex) {
var pro = 1
for (var i = low; i <= high; i++) {
pro *= Number(ex.postfixEval({
n: i
}))
}
return pro
},
pow10x: function (e) {
var x = 1
while (e--) {
x *= 10
}
return x
},
sigma: function (low, high, ex) {
var sum = 0
for (var i = low; i <= high; i++) {
sum += Number(ex.postfixEval({
n: i
}))
}
return sum
},
sin: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.sin(x)
},
sinh: function (x) {
return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2
},
sub: function (a, b) {
return a - b
},
tan: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.tan(x)
},
tanh: function (x) {
return Mexp.sinha(x) / Mexp.cosha(x)
},
toRadian: function (x) {
return x * Math.PI / 180
}
}
Mexp.Exception = function (message) {
this.message = message
}
module.exports = Mexp
Mexp.math={
isDegree:true, //mode of calculator
acos:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.acos(x):Math.acos(x));
},
add:function(a,b){
return a+b;
},
asin:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.asin(x):Math.asin(x));
},
atan:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.atan(x):Math.atan(x));
},
acosh:function(x){
return Math.log(x+Math.sqrt(x*x-1));
},
asinh:function(x){
return Math.log(x+Math.sqrt(x*x+1));
},
atanh:function(x){
return Math.log((1+x)/(1-x));
},
C:function(n,r){
var pro=1,other=n-r,choice=r;
if(choice<other){
choice=other;
other=r;
}
for(var i=choice+1;i<=n;i++)
pro*=i;
return pro/Mexp.math.fact(other);
},
changeSign:function(x){
return -x;
},
cos:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.cos(x);
},
cosh:function(x){
return (Math.pow(Math.E,x)+Math.pow(Math.E,-1*x))/2;
},
div:function(a,b){
return a/b;
},
fact:function(n) {
if(n%1!==0)return "NAN";
var pro=1;
for(var i=2;i<=n;i++)
pro*=i;
return pro;
},
inverse:function(x){
return 1/x;
},
log:function(i){
return Math.log(i)/Math.log(10);
},
mod:function(a,b){
return a%b;
},
mul:function(a,b){
return a*b;
},
P:function(n,r){var pro=1;
for(var i=Math.floor(n)-Math.floor(r)+1;i<=Math.floor(n);i++)
pro*=i;
return pro;
},
Pi:function(low,high,ex){
var pro=1;
for(var i=low;i<=high;i++){
pro*=Number(ex.postfixEval({n:i}));
}
return pro;
},
pow10x:function(e){
var x=1;
while(e--){x*=10;}
return x;
},
sigma:function(low,high,ex){
var sum=0;
for(var i=low;i<=high;i++){
sum+=Number(ex.postfixEval({n:i}));
}
return sum;
},
sin:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.sin(x);
},
sinh:function(x){
return (Math.pow(Math.E,x)-Math.pow(Math.E,-1*x))/2;
},
sub:function(a,b){
return a-b;
},
tan:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.tan(x);
},
tanh:function(x){
return Mexp.sinha(x)/Mexp.cosha(x);
},
toRadian:function(x){
return x*Math.PI/180;
}
};
Mexp.exception=function(message){
this.message=message;
};
module.exports=Mexp;
},{}],4:[function(require,module,exports){

@@ -480,0 +569,0 @@

@@ -25,3 +25,3 @@ /*

/** math-expression-evaluator version 1.2.17
Dated:2017-04-28 */
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.mexp=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d=a("./postfix_evaluator.js");d.prototype.formulaEval=function(){"use strict";for(var a,b,c,d=[],e=this.value,f=0;f<e.length;f++)1===e[f].type||3===e[f].type?d.push({value:3===e[f].type?e[f].show:e[f].value,type:1}):13===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value:e[f].show+("-"!=e[f].show?"(":"")+d[d.length-1].value+("-"!=e[f].show?")":""),type:0}:7===e[f].type?d[d.length-1]={value:(1!=d[d.length-1].type?"(":"")+d[d.length-1].value+(1!=d[d.length-1].type?")":"")+e[f].show,type:7}:10===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b.value+"</sup>"+e[f].show+"<sub>"+a.value+"</sub>",type:10}):d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+"<sup>"+a.value+"</sup>",type:1})):2===e[f].type||9===e[f].type?(a=d.pop(),b=d.pop(),d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+e[f].show+(1!=a.type?"(":"")+a.value+(1!=a.type?")":""),type:e[f].type})):12===e[f].type&&(a=d.pop(),b=d.pop(),c=d.pop(),d.push({value:e[f].show+"("+c.value+","+b.value+","+a.value+")",type:12}));return d[0].value},b.exports=d},{"./postfix_evaluator.js":5}],2:[function(a,b,c){function d(a,b){for(var c=0;c<a.length;c++)a[c]+=b;return a}function e(a,b,c,d){for(var e=0;e<d;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","P","C","asin","acos","atan","7","8","9","int","cosh","acosh","ln","^","root","4","5","6","/","!","tanh","atanh","Mod","1","2","3","*","sinh","asinh","e","log","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin","cos","tan","&pi;","(",")","P","C","asin","acos","atan","7","8","9","Int","cosh","acosh"," ln","^","root","4","5","6","&divide;","!","tanh","atanh"," Mod ","1","2","3","&times;","sinh","asinh","e"," log","0",".","+","-",",","&Sigma;","n","&Pi;","pow"],j=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")",f.math.P,f.math.C,f.math.asin,f.math.acos,f.math.atan,"7","8","9",Math.floor,f.math.cosh,f.math.acosh,Math.log,Math.pow,Math.sqrt,"4","5","6",f.math.div,f.math.fact,f.math.tanh,f.math.atanh,f.math.mod,"1","2","3",f.math.mul,f.math.sinh,f.math.asinh,"E",f.math.log,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],k={0:11,1:0,2:3,3:0,4:0,5:0,6:0,7:11,8:11,9:1,10:10,11:0,12:11,13:0},l=[0,0,0,3,4,5,10,10,0,0,0,1,1,1,0,0,0,0,10,0,1,1,1,2,7,0,0,2,1,1,1,2,0,0,3,0,1,6,9,9,11,12,13,12,8],m={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,12:!0,13:!0},n={0:!0,1:!0,2:!0,3:!0,4:!0,5:!0,6:!0,7:!0,8:!0,9:!0,10:!0,11:!0,12:!0,13:!0},o={0:!0,3:!0,4:!0,8:!0,12:!0,13:!0},p={},q={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,12:!0,13:!0},r={1:!0},s=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","e","0",".",",","n"],["pi","ln","Pi"],["sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){for(i=0;i<a.length;i++){x=a[i].token.length;var b=-1;if(x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y]){b=g.indexOf(s[x][y]);break}b===-1?(g.push(a[i].token),l.push(a[i].type),s.length<=a[i].token.length&&(s[a[i].token.length]=[]),s[a[i].token.length].push(a[i].token),j.push(a[i].value),h.push(a[i].show)):(g[b]=a[i].token,l[b]=a[i].type,j[b]=a[i].value,h[b]=a[i].show)}},f.lex=function(a,b){"use strict";var c,i,t,u,v=[{type:4,value:"(",show:"(",pre:0}],w=[],x=a,y=0,z=m,A=0,B=p,C="";"undefined"!=typeof b&&f.addToken(b);var D={};for(i=0;i<x.length;i++)if(" "!=x[i]){c="";a:for(t=x.length-i>s.length-2?s.length-1:x.length-i;t>0;t--)for(u=0;u<s[t].length;u++)if(e(x,s[t][u],i,t)){c=s[t][u];break a}if(i+=c.length-1,""===c)throw new f.exception("Can't understand after "+x.slice(i));var E=g.indexOf(c),F=c,G=l[E],H=j[E],I=k[G],J=h[E],K=v[v.length-1];for(L=w.length;L--;)if(0===w[L]&&[0,2,3,5,9,11,12,13].indexOf(G)!==-1){if(z[G]!==!0)throw new f.exception(c+" is not allowed after "+C);v.push({value:")",type:5,pre:0,show:")"}),z=n,B=q,d(w,-1).pop()}if(z[G]!==!0)throw new f.exception(c+" is not allowed after "+C);if(B[G]===!0&&(G=2,H=f.math.mul,J="&times;",I=3,i-=c.length),D={value:H,type:G,pre:I,show:J},0===G)z=m,B=p,d(w,2).push(2),v.push(D),v.push({value:"(",type:4,pre:0,show:"("});else if(1===G)1===K.type?(K.value+=H,d(w,1)):v.push(D),z=n,B=o;else if(2===G)z=m,B=p,d(w,2),v.push(D);else if(3===G)v.push(D),z=n,B=q;else if(4===G)y+=w.length,w=[],A++,z=m,B=p,v.push(D);else if(5===G){if(!A)throw new f.exception("Closing parenthesis are more than opening one, wait What!!!");for(;y--;)v.push({value:")",type:5,pre:0,show:")"});y=0,A--,z=n,B=q,v.push(D)}else if(6===G){if(K.hasDec)throw new f.exception("Two decimals are not allowed in one number");1!==K.type&&(K={value:0,type:1,pre:0},v.push(K),d(w,-1)),z=r,d(w,1),B=p,K.value+=H,K.hasDec=!0}else 7===G&&(z=n,B=q,d(w,1),v.push(D));8===G?(z=m,B=p,d(w,4).push(4),v.push(D),v.push({value:"(",type:4,pre:0,show:"("})):9===G?(9===K.type?K.value===f.math.add?(K.value=H,K.show=J,d(w,1)):K.value===f.math.sub&&"-"===J&&(K.value=f.math.add,K.show="+",d(w,1)):5!==K.type&&7!==K.type&&1!==K.type&&3!==K.type&&13!==K.type?"-"===F&&(z=m,B=p,d(w,2).push(2),v.push({value:f.math.changeSign,type:0,pre:21,show:"-"}),v.push({value:"(",type:4,pre:0,show:"("})):(v.push(D),d(w,2)),z=m,B=p):10===G?(z=m,B=p,d(w,2),v.push(D)):11===G?(z=m,B=p,v.push(D)):12===G?(z=m,B=p,d(w,6).push(6),v.push(D),v.push({value:"(",type:4,pre:0})):13===G&&(z=n,B=q,v.push(D)),d(w,-1),C=c}for(var L=w.length;L--;)0===w[L]&&(v.push({value:")",show:")",type:5,pre:3}),d(w,-1).pop());if(z[5]!==!0)throw new f.exception("complete the expression");for(;A--;)v.push({value:")",show:")",type:5,pre:3});return v.push({type:5,value:")",show:")",pre:0}),new f(v)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a){this.value=a};d.math={isDegree:!0,acos:function(a){return d.math.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.math.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.math.isDegree?180/Math.PI*Math.atan(a):Math.atan(a)},acosh:function(a){return Math.log(a+Math.sqrt(a*a-1))},asinh:function(a){return Math.log(a+Math.sqrt(a*a+1))},atanh:function(a){return Math.log((1+a)/(1-a))},C:function(a,b){var c=1,e=a-b,f=b;f<e&&(f=e,e=b);for(var g=f+1;g<=a;g++)c*=g;return c/d.math.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.cos(a)},cosh:function(a){return(Math.pow(Math.E,a)+Math.pow(Math.E,-1*a))/2},div:function(a,b){return a/b},fact:function(a){if(a%1!==0)return"NAN";for(var b=1,c=2;c<=a;c++)b*=c;return b},inverse:function(a){return 1/a},log:function(a){return Math.log(a)/Math.log(10)},mod:function(a,b){return a%b},mul:function(a,b){return a*b},P:function(a,b){for(var c=1,d=Math.floor(a)-Math.floor(b)+1;d<=Math.floor(a);d++)c*=d;return c},Pi:function(a,b,c){for(var d=1,e=a;e<=b;e++)d*=Number(c.postfixEval({n:e}));return d},pow10x:function(a){for(var b=1;a--;)b*=10;return b},sigma:function(a,b,c){for(var d=0,e=a;e<=b;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.sin(a)},sinh:function(a){return(Math.pow(Math.E,a)-Math.pow(Math.E,-1*a))/2},sub:function(a,b){return a-b},tan:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.tan(a)},tanh:function(a){return d.sinha(a)/d.cosha(a)},toRadian:function(a){return a*Math.PI/180}},d.exception=function(a){this.message=a},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");d.prototype.toPostfix=function(){"use strict";for(var a,b,c,e,f,g=[],h=[{value:"(",type:4,pre:0}],i=this.value,j=1;j<i.length;j++)if(1===i[j].type||3===i[j].type||13===i[j].type)1===i[j].type&&(i[j].value=Number(i[j].value)),g.push(i[j]);else if(4===i[j].type)h.push(i[j]);else if(5===i[j].type)for(;4!==(b=h.pop()).type;)g.push(b);else if(11===i[j].type){for(;4!==(b=h.pop()).type;)g.push(b);h.push(b)}else{a=i[j],e=a.pre,f=h[h.length-1],c=f.pre;var k="Math.pow"==f.value&&"Math.pow"==a.value;if(e>c)h.push(a);else{for(;c>=e&&!k||k&&e<c;)b=h.pop(),f=h[h.length-1],g.push(b),c=f.pre,k="Math.pow"==a.value&&"Math.pow"==f.value;h.push(a)}}return new d(g)},b.exports=d},{"./lexer.js":2}],5:[function(a,b,c){var d=a("./postfix.js");d.prototype.postfixEval=function(a){"use strict";a=a||{},a.PI=Math.PI,a.E=Math.E;for(var b,c,e,f=[],g=this.value,h="undefined"!=typeof a.n,i=0;i<g.length;i++)1===g[i].type?f.push({value:g[i].value,type:1}):3===g[i].type?f.push({value:a[g[i].value],type:1}):0===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].value.push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):7===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].value.push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):8===g[i].type?(b=f.pop(),c=f.pop(),f.push({type:1,value:g[i].value(c.value,b.value)})):10===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c.value=c.concat(b),c.value.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):2===g[i].type||9===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(console.log(c),c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):12===g[i].type?(b=f.pop(),"undefined"!=typeof b.type&&(b=[b]),c=f.pop(),e=f.pop(),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):13===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));if(f.length>1)throw new d.exception("Uncaught Syntax error");return f[0].value>1e15?"Infinity":parseFloat(f[0].value.toFixed(15))},d.eval=function(a,b,c){return"undefined"==typeof b?this.lex(a).toPostfix().postfixEval():"undefined"==typeof c?"undefined"!=typeof b.length?this.lex(a,b).toPostfix().postfixEval():this.lex(a).toPostfix().postfixEval(b):this.lex(a,b).toPostfix().postfixEval(c)},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
Dated:2017-05-05 */
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.mexp=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d=a("./postfix_evaluator.js");d.prototype.formulaEval=function(){"use strict";for(var a,b,c,d=[],e=this.value,f=0;f<e.length;f++)1===e[f].type||3===e[f].type?d.push({value:3===e[f].type?e[f].show:e[f].value,type:1}):13===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value:e[f].show+("-"!=e[f].show?"(":"")+d[d.length-1].value+("-"!=e[f].show?")":""),type:0}:7===e[f].type?d[d.length-1]={value:(1!=d[d.length-1].type?"(":"")+d[d.length-1].value+(1!=d[d.length-1].type?")":"")+e[f].show,type:7}:10===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b.value+"</sup>"+e[f].show+"<sub>"+a.value+"</sub>",type:10}):d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+"<sup>"+a.value+"</sup>",type:1})):2===e[f].type||9===e[f].type?(a=d.pop(),b=d.pop(),d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+e[f].show+(1!=a.type?"(":"")+a.value+(1!=a.type?")":""),type:e[f].type})):12===e[f].type&&(a=d.pop(),b=d.pop(),c=d.pop(),d.push({value:e[f].show+"("+c.value+","+b.value+","+a.value+")",type:12}));return d[0].value},b.exports=d},{"./postfix_evaluator.js":5}],2:[function(a,b,c){function d(a,b){for(var c=0;c<a.length;c++)a[c]+=b;return a}function e(a,b,c,d){for(var e=0;e<d;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","P","C","asin","acos","atan","7","8","9","int","cosh","acosh","ln","^","root","4","5","6","/","!","tanh","atanh","Mod","1","2","3","*","sinh","asinh","e","log","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin","cos","tan","&pi;","(",")","P","C","asin","acos","atan","7","8","9","Int","cosh","acosh"," ln","^","root","4","5","6","&divide;","!","tanh","atanh"," Mod ","1","2","3","&times;","sinh","asinh","e"," log","0",".","+","-",",","&Sigma;","n","&Pi;","pow"],i=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")",f.math.P,f.math.C,f.math.asin,f.math.acos,f.math.atan,"7","8","9",Math.floor,f.math.cosh,f.math.acosh,Math.log,Math.pow,Math.sqrt,"4","5","6",f.math.div,f.math.fact,f.math.tanh,f.math.atanh,f.math.mod,"1","2","3",f.math.mul,f.math.sinh,f.math.asinh,"E",f.math.log,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],j={0:11,1:0,2:3,3:0,4:0,5:0,6:0,7:11,8:11,9:1,10:10,11:0,12:11,13:0},k=[0,0,0,3,4,5,10,10,0,0,0,1,1,1,0,0,0,0,10,0,1,1,1,2,7,0,0,2,1,1,1,2,0,0,3,0,1,6,9,9,11,12,13,12,8],l={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,12:!0,13:!0},m={0:!0,1:!0,2:!0,3:!0,4:!0,5:!0,6:!0,7:!0,8:!0,9:!0,10:!0,11:!0,12:!0,13:!0},n={0:!0,3:!0,4:!0,8:!0,12:!0,13:!0},o={},p={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,12:!0,13:!0},q={1:!0},r=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","e","0",".",",","n"],["pi","ln","Pi"],["sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){for(var b=0;b<a.length;b++){var c=a[b].token.length,d=-1;if(c<r.length)for(var e=0;e<r[c].length;e++)if(a[b].token===r[c][e]){d=g.indexOf(r[c][e]);break}d===-1?(g.push(a[b].token),k.push(a[b].type),r.length<=a[b].token.length&&(r[a[b].token.length]=[]),r[a[b].token.length].push(a[b].token),i.push(a[b].value),h.push(a[b].show)):(g[d]=a[b].token,k[d]=a[b].type,i[d]=a[b].value,h[d]=a[b].show)}},f.lex=function(a,b){"use strict";var c,s,t,u,v={value:f.math.changeSign,type:0,pre:21,show:"-"},w={value:")",show:")",type:5,pre:0},x={value:"(",type:4,pre:0,show:"("},y=[x],z=[],A=a,B=0,C=l,D=0,E=o,F="";"undefined"!=typeof b&&f.addToken(b);var G={};for(s=0;s<A.length;s++)if(" "!==A[s]){for(c="",t=A.length-s>r.length-2?r.length-1:A.length-s;t>0;t--)for(u=0;u<r[t].length;u++)e(A,r[t][u],s,t)&&(c=r[t][u],u=r[t].length,t=0);if(s+=c.length-1,""===c)throw new f.Exception("Can't understand after "+A.slice(s));var H,I=g.indexOf(c),J=c,K=k[I],L=i[I],M=j[K],N=h[I],O=y[y.length-1];for(H=z.length;H--&&0===z[H];)if([0,2,3,5,9,11,12,13].indexOf(K)!==-1){if(C[K]!==!0)throw new f.Exception(c+" is not allowed after "+F);y.push(w),C=m,E=p,d(z,-1).pop()}if(C[K]!==!0)throw new f.Exception(c+" is not allowed after "+F);if(E[K]===!0&&(K=2,L=f.math.mul,N="&times;",M=3,s-=c.length),G={value:L,type:K,pre:M,show:N},0===K)C=l,E=o,d(z,2).push(2),y.push(G),y.push(x);else if(1===K)1===O.type?(O.value+=L,d(z,1)):y.push(G),C=m,E=n;else if(2===K)C=l,E=o,d(z,2),y.push(G);else if(3===K)y.push(G),C=m,E=p;else if(4===K)B+=z.length,z=[],D++,C=l,E=o,y.push(G);else if(5===K){if(!D)throw new f.Exception("Closing parenthesis are more than opening one, wait What!!!");for(;B--;)y.push(w);B=0,D--,C=m,E=p,y.push(G)}else if(6===K){if(O.hasDec)throw new f.Exception("Two decimals are not allowed in one number");1!==O.type&&(O={value:0,type:1,pre:0},y.push(O),d(z,-1)),C=q,d(z,1),E=o,O.value+=L,O.hasDec=!0}else 7===K&&(C=m,E=p,d(z,1),y.push(G));8===K?(C=l,E=o,d(z,4).push(4),y.push(G),y.push(x)):9===K?(9===O.type?O.value===f.math.add?(O.value=L,O.show=N,d(z,1)):O.value===f.math.sub&&"-"===N&&(O.value=f.math.add,O.show="+",d(z,1)):5!==O.type&&7!==O.type&&1!==O.type&&3!==O.type&&13!==O.type?"-"===J&&(C=l,E=o,d(z,2).push(2),y.push(v),y.push(x)):(y.push(G),d(z,2)),C=l,E=o):10===K?(C=l,E=o,d(z,2),y.push(G)):11===K?(C=l,E=o,y.push(G)):12===K?(C=l,E=o,d(z,6).push(6),y.push(G),y.push(x)):13===K&&(C=m,E=p,y.push(G)),d(z,-1),F=c}for(H=z.length;H--&&0===z[H];)y.push(w),d(z,-1).pop();if(C[5]!==!0)throw new f.Exception("complete the expression");for(;D--;)y.push(w);return y.push(w),new f(y)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a){this.value=a};d.math={isDegree:!0,acos:function(a){return d.math.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.math.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.math.isDegree?180/Math.PI*Math.atan(a):Math.atan(a)},acosh:function(a){return Math.log(a+Math.sqrt(a*a-1))},asinh:function(a){return Math.log(a+Math.sqrt(a*a+1))},atanh:function(a){return Math.log((1+a)/(1-a))},C:function(a,b){var c=1,e=a-b,f=b;f<e&&(f=e,e=b);for(var g=f+1;g<=a;g++)c*=g;return c/d.math.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.cos(a)},cosh:function(a){return(Math.pow(Math.E,a)+Math.pow(Math.E,-1*a))/2},div:function(a,b){return a/b},fact:function(a){if(a%1!==0)return"NaN";for(var b=1,c=2;c<=a;c++)b*=c;return b},inverse:function(a){return 1/a},log:function(a){return Math.log(a)/Math.log(10)},mod:function(a,b){return a%b},mul:function(a,b){return a*b},P:function(a,b){for(var c=1,d=Math.floor(a)-Math.floor(b)+1;d<=Math.floor(a);d++)c*=d;return c},Pi:function(a,b,c){for(var d=1,e=a;e<=b;e++)d*=Number(c.postfixEval({n:e}));return d},pow10x:function(a){for(var b=1;a--;)b*=10;return b},sigma:function(a,b,c){for(var d=0,e=a;e<=b;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.sin(a)},sinh:function(a){return(Math.pow(Math.E,a)-Math.pow(Math.E,-1*a))/2},sub:function(a,b){return a-b},tan:function(a){return d.math.isDegree&&(a=d.math.toRadian(a)),Math.tan(a)},tanh:function(a){return d.sinha(a)/d.cosha(a)},toRadian:function(a){return a*Math.PI/180}},d.Exception=function(a){this.message=a},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");d.prototype.toPostfix=function(){"use strict";for(var a,b,c,e,f,g=[],h=[{value:"(",type:4,pre:0}],i=this.value,j=1;j<i.length;j++)if(1===i[j].type||3===i[j].type||13===i[j].type)1===i[j].type&&(i[j].value=Number(i[j].value)),g.push(i[j]);else if(4===i[j].type)h.push(i[j]);else if(5===i[j].type)for(;4!==(b=h.pop()).type;)g.push(b);else if(11===i[j].type){for(;4!==(b=h.pop()).type;)g.push(b);h.push(b)}else{a=i[j],e=a.pre,f=h[h.length-1],c=f.pre;var k="Math.pow"==f.value&&"Math.pow"==a.value;if(e>c)h.push(a);else{for(;c>=e&&!k||k&&e<c;)b=h.pop(),f=h[h.length-1],g.push(b),c=f.pre,k="Math.pow"==a.value&&"Math.pow"==f.value;h.push(a)}}return new d(g)},b.exports=d},{"./lexer.js":2}],5:[function(a,b,c){var d=a("./postfix.js");d.prototype.postfixEval=function(a){"use strict";a=a||{},a.PI=Math.PI,a.E=Math.E;for(var b,c,e,f=[],g=this.value,h="undefined"!=typeof a.n,i=0;i<g.length;i++)1===g[i].type?f.push({value:g[i].value,type:1}):3===g[i].type?f.push({value:a[g[i].value],type:1}):0===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].value.push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):7===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].value.push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):8===g[i].type?(b=f.pop(),c=f.pop(),f.push({type:1,value:g[i].value(c.value,b.value)})):10===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c.value=c.concat(b),c.value.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):2===g[i].type||9===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(console.log(c),c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):12===g[i].type?(b=f.pop(),"undefined"!=typeof b.type&&(b=[b]),c=f.pop(),e=f.pop(),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):13===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));if(f.length>1)throw new d.exception("Uncaught Syntax error");return f[0].value>1e15?"Infinity":parseFloat(f[0].value.toFixed(15))},d.eval=function(a,b,c){return"undefined"==typeof b?this.lex(a).toPostfix().postfixEval():"undefined"==typeof c?"undefined"!=typeof b.length?this.lex(a,b).toPostfix().postfixEval():this.lex(a).toPostfix().postfixEval(b):this.lex(a,b).toPostfix().postfixEval(c)},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
{
"name": "math-expression-evaluator",
"version": "1.2.17",
"version": "1.2.18",
"description": "A flexible math expression evaluator",

@@ -11,3 +11,3 @@ "main": "src/formula_evaluator.js",

"type": "git",
"url": "git+https://github.com/redhivesoftware/math-expression-evaluator#readme"
"url": "https://github.com/redhivesoftware/math-expression-evaluator.git"
},

@@ -27,8 +27,15 @@ "keywords": [

"devDependencies": {
"eslint": "^6.6.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"grunt": "^0.4.5",
"grunt-browserify": "^3.8.0",
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^1.0.0",
"grunt-eslint": "^19.0.0",
"mocha": "^2.2.5"
}
}

@@ -1,3 +0,1 @@

# math-expression-evaluator

@@ -23,3 +21,3 @@ An extremely efficient, flexible and amazing evaluator for Math expression in Javascript.([Documentation](http://redhivesoftware.github.io/math-expression-evaluator/))

### Node JS
**Using npm**
**Using npm**

@@ -60,20 +58,20 @@ npm install math-expression-evaluator

|**^**| power operator eg. 2^3 returns 8 |
|**root**| underroot function root 4 returns 2 |
|**root**| underroot function root 4 returns 2 |
|**sin**| Sine function |
|**cos**| Cosine function |
|**tan**| Tangent function |
|**asin**| Inverse Sine funtion |
|**acos**| Inverse Cosine funtion |
|**atan**| Inverse Tangent funtion |
|**sinh**| Hyperbolic Sine funtion |
|**cosh**| Hyperbolic Cosine funtion |
|**tanh**| Hyperbolic Tangent funtion |
|**asinh**| Inverse Hyperbolic Sine funtion |
|**acosh**| Inverse Hyperbolic Cosine funtion |
|**atanh**| Inverse Hyperbolic Tangent funtion |
|**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 |
## Features
## 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**.
### 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.

@@ -86,6 +84,5 @@ and Pi(1,15,n) will evaluate to 1307674368000 as n is multiplied from 1 to 15 which is equal to 15!

##Changelog
### Removed lodash.indexof and used native Array.prototype.indexOf hence dropping suppports for IE8 and below.
### 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

@@ -1,309 +0,382 @@

var Mexp=require('./math_function.js');
function inc(arr,val){
for(var i=0;i<arr.length;i++)
arr[i]+=val;
return arr;
}
var token=['sin','cos','tan','pi','(',')','P','C',
'asin','acos','atan','7','8','9','int',
'cosh','acosh','ln','^','root','4','5','6','/','!',
'tanh','atanh','Mod','1','2','3','*',
'sinh','asinh','e','log','0','.','+','-',',','Sigma','n','Pi','pow'];
var show=['sin','cos','tan','&pi;','(',')','P','C',
'asin','acos','atan','7','8','9','Int',
'cosh','acosh',' ln','^','root','4','5','6','&divide;','!',
'tanh','atanh',' Mod ','1','2','3','&times;',
'sinh','asinh','e',' log','0','.','+','-',',','&Sigma;','n','&Pi;','pow'];
var eva=[Mexp.math.sin,Mexp.math.cos,Mexp.math.tan,'PI','(',')',Mexp.math.P,Mexp.math.C,
Mexp.math.asin,Mexp.math.acos,Mexp.math.atan,'7','8','9',Math.floor,
Mexp.math.cosh,Mexp.math.acosh,Math.log,Math.pow,Math.sqrt,'4','5','6',Mexp.math.div,Mexp.math.fact,
Mexp.math.tanh,Mexp.math.atanh,Mexp.math.mod,'1','2','3',Mexp.math.mul,
Mexp.math.sinh,Mexp.math.asinh,'E',Mexp.math.log,'0','.',Mexp.math.add,Mexp.math.sub,',',Mexp.math.sigma,'n',Mexp.math.Pi,Math.pow];
var preced={0:11,1:0,2:3,3:0,4:0,5:0,6:0,7:11,8:11,9:1,10:10,11:0,12:11,13:0};
var type=[0,0,0,3,4,5,10,10,
0,0,0,1,1,1,0,
0,0,0,10,0,1,1,1,2,7,
0,0,2,1,1,1,2,
0,0,3,0,1,6,9,9,11,12,13,12,8];
/*
0 : function with syntax function_name(Maths_exp)
1 : numbers
2 : binary operators like * / Mod left associate and same precedence
3 : Math constant values like e,pi,Cruncher ans
4 : opening bracket
5 : closing bracket
6 : decimal
7 : function with syntax (Math_exp)function_name
8: function with syntax function_name(Math_exp1,Math_exp2)
9 : binary operator like +,-
10: binary operator like P C or ^
11: ,
12: function with , seperated three parameters
13: variable of Sigma function
*/
var type0={0:true,1:true,3:true,4:true,6:true,8:true,9:true,12:true,13:true},//type2:true,type4:true,type9:true,type11:true,type21:true,type22
type1={0:true,1:true,2:true,3:true,4:true,5:true,6:true,7:true,8:true,9:true,10:true,11:true,12:true,13:true},//type3:true,type5:true,type7:true,type23
type_1={0:true,3:true,4:true,8:true,12:true,13:true},
empty={},
type_3={0:true,1:true,3:true,4:true,6:true,8:true,12:true,13:true},//type_5:true,type_7:true,type_23
type6={1:true},
newAr=[[],
["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","e","0",".",",","n"],
["pi","ln","Pi"],
["sin","cos","tan","Del","int","Mod","log","pow"],
["asin","acos","atan","cosh","root","tanh","sinh"],
["acosh","atanh","asinh","Sigma"]];
function match(str1,str2,i,x){
for(var f=0;f<x;f++){
if (str1[i+f]!==str2[f])
return false;
}
return true;
}
Mexp.addToken=function(tokens){
for(i=0;i<tokens.length;i++){
x=tokens[i].token.length;
var temp=-1;
var Mexp = require('./math_function.js')
function inc (arr, val) {
for (var i = 0; i < arr.length; i++) {
arr[i] += val
}
return arr
}
var token = ['sin', 'cos', 'tan', 'pi', '(', ')', 'P', 'C',
'asin', 'acos', 'atan', '7', '8', '9', 'int',
'cosh', 'acosh', 'ln', '^', 'root', '4', '5', '6', '/', '!',
'tanh', 'atanh', 'Mod', '1', '2', '3', '*',
'sinh', 'asinh', 'e', 'log', '0', '.', '+', '-', ',', 'Sigma', 'n', 'Pi', 'pow']
var show = ['sin', 'cos', 'tan', '&pi;', '(', ')', 'P', 'C',
'asin', 'acos', 'atan', '7', '8', '9', 'Int',
'cosh', 'acosh', ' ln', '^', 'root', '4', '5', '6', '&divide;', '!',
'tanh', 'atanh', ' Mod ', '1', '2', '3', '&times;',
'sinh', 'asinh', 'e', ' log', '0', '.', '+', '-', ',', '&Sigma;', 'n', '&Pi;', 'pow']
var eva = [Mexp.math.sin, Mexp.math.cos, Mexp.math.tan, 'PI', '(', ')', Mexp.math.P, Mexp.math.C,
Mexp.math.asin, Mexp.math.acos, Mexp.math.atan, '7', '8', '9', Math.floor,
Mexp.math.cosh, Mexp.math.acosh, Math.log, Math.pow, Math.sqrt, '4', '5', '6', Mexp.math.div, Mexp.math.fact,
Mexp.math.tanh, Mexp.math.atanh, Mexp.math.mod, '1', '2', '3', Mexp.math.mul,
Mexp.math.sinh, Mexp.math.asinh, 'E', Mexp.math.log, '0', '.', Mexp.math.add, Mexp.math.sub, ',', Mexp.math.sigma, 'n', Mexp.math.Pi, Math.pow]
var preced = {
0: 11,
1: 0,
2: 3,
3: 0,
4: 0,
5: 0,
6: 0,
7: 11,
8: 11,
9: 1,
10: 10,
11: 0,
12: 11,
13: 0
} // stores precedence by types
var type = [0, 0, 0, 3, 4, 5, 10, 10,
0, 0, 0, 1, 1, 1, 0,
0, 0, 0, 10, 0, 1, 1, 1, 2, 7,
0, 0, 2, 1, 1, 1, 2,
0, 0, 3, 0, 1, 6, 9, 9, 11, 12, 13, 12, 8]
/*
0 : function with syntax function_name(Maths_exp)
1 : numbers
2 : binary operators like * / Mod left associate and same precedence
3 : Math constant values like e,pi,Cruncher ans
4 : opening bracket
5 : closing bracket
6 : decimal
7 : function with syntax (Math_exp)function_name
8: function with syntax function_name(Math_exp1,Math_exp2)
9 : binary operator like +,-
10: binary operator like P C or ^
11: ,
12: function with , seperated three parameters
13: variable of Sigma function
*/
var type0 = {
0: true,
1: true,
3: true,
4: true,
6: true,
8: true,
9: true,
12: true,
13: true
} // type2:true,type4:true,type9:true,type11:true,type21:true,type22
var type1 = {
0: true,
1: true,
2: true,
3: true,
4: true,
5: true,
6: true,
7: true,
8: true,
9: true,
10: true,
11: true,
12: true,
13: true
} // type3:true,type5:true,type7:true,type23
var type1Asterick = {
0: true,
3: true,
4: true,
8: true,
12: true,
13: true
}
var empty = {}
var type3Asterick = {
0: true,
1: true,
3: true,
4: true,
6: true,
8: true,
12: true,
13: true
} // type_5:true,type_7:true,type_23
var type6 = {
1: true
}
var newAr = [
[],
['1', '2', '3', '7', '8', '9', '4', '5', '6', '+', '-', '*', '/', '(', ')', '^', '!', 'P', 'C', 'e', '0', '.', ',', 'n'],
['pi', 'ln', 'Pi'],
['sin', 'cos', 'tan', 'Del', 'int', 'Mod', 'log', 'pow'],
['asin', 'acos', 'atan', 'cosh', 'root', 'tanh', 'sinh'],
['acosh', 'atanh', 'asinh', 'Sigma']
]
//newAr is a specially designed data structure in which 1D array at location one of 2d array has all string with length 1 2 with 2 and so on
function match (str1, str2, i, x) {
for (var f = 0; f < x; f++) {
if (str1[i + f] !== str2[f]) {
return false
}
}
return true
}
Mexp.addToken = function (tokens) {
for (var i = 0; i < tokens.length; i++) {
var x = tokens[i].token.length
var temp = -1
if (x<newAr.length) //match to check if token is really huge and not existing
//if not checked it will break in next line as undefined index
for(y=0;y<newAr[x].length;y++){
if (tokens[i].token===newAr[x][y]){
temp=token.indexOf(newAr[x][y]);
break;
}
}
if (temp===-1) {
token.push(tokens[i].token);
type.push(tokens[i].type);
if(newAr.length<=tokens[i].token.length)
newAr[tokens[i].token.length]=[];
newAr[tokens[i].token.length].push(tokens[i].token);
eva.push(tokens[i].value);
show.push(tokens[i].show);
}
else {
token[temp]=tokens[i].token;
type[temp]=tokens[i].type;
eva[temp]=tokens[i].value;
show[temp]=tokens[i].show;
}
}
};
Mexp.lex=function(inp,tokens){
'use strict';
var str=[{type:4,value:"(",show:"(",pre:0}];
var ptc=[]; //Parenthesis to close at the beginning is after one token
var inpStr=inp;
var key;
var pcounter=0;
var allowed=type0;
var bracToClose=0;
var asterick=empty;
var prevKey='';
var i,x,y;
if(typeof tokens!=="undefined")
Mexp.addToken(tokens);
var obj={};
for(i=0;i<inpStr.length;i++){
if (inpStr[i]==' ') {
continue;
}
key='';
sec:for(x=(inpStr.length-i>(newAr.length-2)?newAr.length-1:inpStr.length-i);x>0;x--){
for(y=0;y<newAr[x].length;y++){
if (match(inpStr,newAr[x][y],i,x)){
key=newAr[x][y];
break sec;
}
}
}
i+=key.length-1;
if(key===''){
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}
var index=token.indexOf(key);
var cToken=key;
var cType=type[index];
var cEv=eva[index];
var cPre=preced[cType];
var cShow=show[index];
var pre=str[str.length-1];
for(j=ptc.length;j--;){ //loop over ptc
if(ptc[j]===0){
if([0,2,3,5,9,11,12,13].indexOf(cType)!==-1){
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
str.push({value:")",type:5,pre:0,show:")"});
allowed=type1;
asterick=type_3;
inc(ptc,-1).pop();
}
}
}
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
if(asterick[cType]===true){
cType=2;
cEv=Mexp.math.mul;
cShow="&times;";
cPre=3;
i=i-key.length;
}
obj={value:cEv,type:cType,pre:cPre,show:cShow};
if(cType===0){
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push(obj);
str.push({value:"(",type:4,pre:0,show:"("});
}
else if(cType===1){
if(pre.type===1){
pre.value+=cEv;
inc(ptc,1);
}
else {
str.push(obj);
}
allowed=type1;
asterick=type_1;
}
else if(cType===2){
allowed=type0;
asterick=empty;
inc(ptc,2);
str.push(obj);
}
else if(cType===3){//constant
str.push(obj);
allowed=type1;
asterick=type_3;
}
else if(cType===4){
pcounter+=ptc.length;
ptc=[];
bracToClose++;
allowed=type0;
asterick=empty;
str.push(obj);
}
else if(cType===5){
if(!bracToClose){
throw(new Mexp.exception("Closing parenthesis are more than opening one, wait What!!!"));
}
while(pcounter--){ //loop over ptc
str.push({value:")",type:5,pre:0,show:")"});
}
pcounter=0;
bracToClose--;
allowed=type1;
asterick=type_3;
str.push(obj);
}
else if(cType===6){
if(pre.hasDec){
throw(new Mexp.exception("Two decimals are not allowed in one number"));
}
if(pre.type!==1){
pre={value:0,type:1,pre:0}; //pre needs to be changed as it will the last value now to be safe in later code
str.push(pre);
inc(ptc,-1);
}
allowed=type6;
inc(ptc,1);
asterick=empty;
pre.value+=cEv;
pre.hasDec=true;
}
else if(cType===7){
allowed=type1;
asterick=type_3;
inc(ptc,1);
str.push(obj);
}
if(cType===8){
allowed=type0;
asterick=empty;
inc(ptc,4).push(4);
str.push(obj);
str.push({value:"(",type:4,pre:0,show:"("});
}
else if(cType===9){
if(pre.type===9){
if(pre.value===Mexp.math.add){
pre.value=cEv;
pre.show=cShow;
inc(ptc,1);
}
else if(pre.value===Mexp.math.sub&&cShow==='-'){
pre.value=Mexp.math.add;
pre.show='+';
inc(ptc,1);
}
}
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){//changesign only when negative is found
if(cToken==='-'){//do nothing for + token
//don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
}
}
else{
str.push(obj);
inc(ptc,2);
}
allowed=type0;
asterick=empty;
}
else if(cType===10){
allowed=type0;
asterick=empty;
inc(ptc,2);
str.push(obj);
}
else if(cType===11){
allowed=type0;
asterick=empty;
str.push(obj);
}
else if(cType===12){
allowed=type0;
asterick=empty;
inc(ptc,6).push(6);
str.push(obj);
str.push({value:"(",type:4,pre:0});
}
else if(cType===13){
allowed=type1;
asterick=type_3;
str.push(obj);
}
inc(ptc,-1);
prevKey=key;
}
for(var j=ptc.length;j--;){ //loop over ptc
if(ptc[j]===0){
// newAr is a specially designed data structure in which 1D array at location one of 2d array has all string with length 1 2 with 2 and so on
str.push({value:")",show:")",type:5,pre:3});
inc(ptc,-1).pop();
}
}
if (allowed[5]!==true) {
throw(new Mexp.exception("complete the expression"));
}
while(bracToClose--)
str.push({value:")",show:")",type:5,pre:3});
if (x < newAr.length) { // match to check if token is really huge and not existing
// if not checked it will break in next line as undefined index
for (var y = 0; y < newAr[x].length; y++) {
if (tokens[i].token === newAr[x][y]) {
temp = token.indexOf(newAr[x][y])
break
}
}
}
if (temp === -1) {
token.push(tokens[i].token)
type.push(tokens[i].type)
if (newAr.length <= tokens[i].token.length) {
newAr[tokens[i].token.length] = []
}
newAr[tokens[i].token.length].push(tokens[i].token)
eva.push(tokens[i].value)
show.push(tokens[i].show)
} else {
token[temp] = tokens[i].token
type[temp] = tokens[i].type
eva[temp] = tokens[i].value
show[temp] = tokens[i].show
}
}
}
Mexp.lex = function (inp, tokens) {
'use strict'
var changeSignObj = {
value: Mexp.math.changeSign,
type: 0,
pre: 21,
show: '-'
}
var closingParObj = {
value: ')',
show: ')',
type: 5,
pre: 0
}
var openingParObj = {
value: '(',
type: 4,
pre: 0,
show: '('
}
var str = [openingParObj]
var ptc = [] // Parenthesis to close at the beginning is after one token
var inpStr = inp
var key
var allowed = type0
var bracToClose = 0
var asterick = empty
var prevKey = ''
var i, x, y
if (typeof tokens !== 'undefined') {
Mexp.addToken(tokens)
}
var obj = {}
for (i = 0; i < inpStr.length; i++) {
if (inpStr[i] === ' ') {
continue
}
key = ''
for (x = (inpStr.length - i > (newAr.length - 2) ? newAr.length - 1 : inpStr.length - i); x > 0; x--) {
for (y = 0; y < newAr[x].length; y++) {
if (match(inpStr, newAr[x][y], i, x)) {
key = newAr[x][y]
y = newAr[x].length
x = 0
}
}
}
i += key.length - 1
if (key === '') {
throw (new Mexp.Exception('Can\'t understand after ' + inpStr.slice(i)))
}
var index = token.indexOf(key)
var cToken = key
var cType = type[index]
var cEv = eva[index]
var cPre = preced[cType]
var cShow = show[index]
var pre = str[str.length - 1]
var j
for (j = ptc.length; j--;) { // loop over ptc
if (ptc[j] === 0) {
if ([0, 2, 3, 4, 5, 9, 11, 12, 13].indexOf(cType) !== -1) {
if (allowed[cType] !== true) {
throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
}
str.push(closingParObj)
allowed = type1
asterick = type3Asterick
inc(ptc, -1).pop()
}
} else break
}
if (allowed[cType] !== true) {
throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
}
if (asterick[cType] === true) {
cType = 2
cEv = Mexp.math.mul
cShow = '&times;'
cPre = 3
i = i - key.length
}
obj = {
value: cEv,
type: cType,
pre: cPre,
show: cShow
}
if (cType === 0) {
allowed = type0
asterick = empty
inc(ptc, 2).push(2)
str.push(obj)
str.push(openingParObj)
} else if (cType === 1) {
if (pre.type === 1) {
pre.value += cEv
inc(ptc, 1)
} else {
str.push(obj)
}
allowed = type1
asterick = type1Asterick
} else if (cType === 2) {
allowed = type0
asterick = empty
inc(ptc, 2)
str.push(obj)
} else if (cType === 3) { // constant
str.push(obj)
allowed = type1
asterick = type3Asterick
} else if (cType === 4) {
inc(ptc, 1)
bracToClose++
allowed = type0
asterick = empty
str.push(obj)
} else if (cType === 5) {
if (!bracToClose) {
throw (new Mexp.Exception('Closing parenthesis are more than opening one, wait What!!!'))
}
bracToClose--
allowed = type1
asterick = type3Asterick
str.push(obj)
} else if (cType === 6) {
if (pre.hasDec) {
throw (new Mexp.Exception('Two decimals are not allowed in one number'))
}
if (pre.type !== 1) {
pre = {
value: 0,
type: 1,
pre: 0
} // pre needs to be changed as it will the last value now to be safe in later code
str.push(pre)
inc(ptc, -1)
}
allowed = type6
inc(ptc, 1)
asterick = empty
pre.value += cEv
pre.hasDec = true
} else if (cType === 7) {
allowed = type1
asterick = type3Asterick
inc(ptc, 1)
str.push(obj)
}
if (cType === 8) {
allowed = type0
asterick = empty
inc(ptc, 4).push(4)
str.push(obj)
str.push(openingParObj)
} else if (cType === 9) {
if (pre.type === 9) {
if (pre.value === Mexp.math.add) {
pre.value = cEv
pre.show = cShow
inc(ptc, 1)
} else if (pre.value === Mexp.math.sub && cShow === '-') {
pre.value = Mexp.math.add
pre.show = '+'
inc(ptc, 1)
}
} else if (pre.type !== 5 && pre.type !== 7 && pre.type !== 1 && pre.type !== 3 && pre.type !== 13) { // changesign only when negative is found
if (cToken === '-') { // do nothing for + token
// don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed = type0
asterick = empty
inc(ptc, 2).push(2)
str.push(changeSignObj)
str.push(openingParObj)
}
} else {
str.push(obj)
inc(ptc, 2)
}
allowed = type0
asterick = empty
} else if (cType === 10) {
allowed = type0
asterick = empty
inc(ptc, 2)
str.push(obj)
} else if (cType === 11) {
allowed = type0
asterick = empty
str.push(obj)
} else if (cType === 12) {
allowed = type0
asterick = empty
inc(ptc, 6).push(6)
str.push(obj)
str.push(openingParObj)
} else if (cType === 13) {
allowed = type1
asterick = type3Asterick
str.push(obj)
}
inc(ptc, -1)
prevKey = key
}
for (j = ptc.length; j--;) { // loop over ptc
if (ptc[j] === 0) {
str.push(closingParObj)
inc(ptc, -1).pop()
} else break // if it is not zero so before ptc also cant be zero
}
if (allowed[5] !== true) {
throw (new Mexp.Exception('complete the expression'))
}
while (bracToClose--) {
str.push(closingParObj)
}
str.push({type:5,value:")",show:")",pre:0});
// console.log(str);
return new Mexp(str);
};
module.exports=Mexp;
str.push(closingParObj)
console.log(str, inp)
// console.log(str);
return new Mexp(str)
}
module.exports = Mexp

@@ -1,120 +0,130 @@

var Mexp=function(parsed){
this.value=parsed;
var Mexp = function (parsed) {
this.value = parsed
}
};
Mexp.math={
isDegree:true, //mode of calculator
acos:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.acos(x):Math.acos(x));
},
add:function(a,b){
return a+b;
},
asin:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.asin(x):Math.asin(x));
},
atan:function(x){
return (Mexp.math.isDegree?180/Math.PI*Math.atan(x):Math.atan(x));
},
acosh:function(x){
return Math.log(x+Math.sqrt(x*x-1));
},
asinh:function(x){
return Math.log(x+Math.sqrt(x*x+1));
},
atanh:function(x){
return Math.log((1+x)/(1-x));
},
C:function(n,r){
var pro=1,other=n-r,choice=r;
if(choice<other){
choice=other;
other=r;
}
for(var i=choice+1;i<=n;i++)
pro*=i;
return pro/Mexp.math.fact(other);
},
changeSign:function(x){
return -x;
},
cos:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.cos(x);
},
cosh:function(x){
return (Math.pow(Math.E,x)+Math.pow(Math.E,-1*x))/2;
},
div:function(a,b){
return a/b;
},
fact:function(n) {
if(n%1!==0)return "NAN";
var pro=1;
for(var i=2;i<=n;i++)
pro*=i;
return pro;
},
inverse:function(x){
return 1/x;
},
log:function(i){
return Math.log(i)/Math.log(10);
},
mod:function(a,b){
return a%b;
},
mul:function(a,b){
return a*b;
},
P:function(n,r){var pro=1;
for(var i=Math.floor(n)-Math.floor(r)+1;i<=Math.floor(n);i++)
pro*=i;
return pro;
},
Pi:function(low,high,ex){
var pro=1;
for(var i=low;i<=high;i++){
pro*=Number(ex.postfixEval({n:i}));
}
return pro;
},
pow10x:function(e){
var x=1;
while(e--){x*=10;}
return x;
},
sigma:function(low,high,ex){
var sum=0;
for(var i=low;i<=high;i++){
sum+=Number(ex.postfixEval({n:i}));
}
return sum;
},
sin:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.sin(x);
},
sinh:function(x){
return (Math.pow(Math.E,x)-Math.pow(Math.E,-1*x))/2;
},
sub:function(a,b){
return a-b;
},
tan:function(x){
if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
return Math.tan(x);
},
tanh:function(x){
return Mexp.sinha(x)/Mexp.cosha(x);
},
toRadian:function(x){
return x*Math.PI/180;
}
};
Mexp.exception=function(message){
this.message=message;
};
module.exports=Mexp;
Mexp.math = {
isDegree: true, // mode of calculator
acos: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x))
},
add: function (a, b) {
return a + b
},
asin: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x))
},
atan: function (x) {
return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x))
},
acosh: function (x) {
return Math.log(x + Math.sqrt(x * x - 1))
},
asinh: function (x) {
return Math.log(x + Math.sqrt(x * x + 1))
},
atanh: function (x) {
return Math.log((1 + x) / (1 - x))
},
C: function (n, r) {
var pro = 1
var other = n - r
var choice = r
if (choice < other) {
choice = other
other = r
}
for (var i = choice + 1; i <= n; i++) {
pro *= i
}
return pro / Mexp.math.fact(other)
},
changeSign: function (x) {
return -x
},
cos: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.cos(x)
},
cosh: function (x) {
return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2
},
div: function (a, b) {
return a / b
},
fact: function (n) {
if (n % 1 !== 0) return 'NaN'
var pro = 1
for (var i = 2; i <= n; i++) {
pro *= i
}
return pro
},
inverse: function (x) {
return 1 / x
},
log: function (i) {
return Math.log(i) / Math.log(10)
},
mod: function (a, b) {
return a % b
},
mul: function (a, b) {
return a * b
},
P: function (n, r) {
var pro = 1
for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) {
pro *= i
}
return pro
},
Pi: function (low, high, ex) {
var pro = 1
for (var i = low; i <= high; i++) {
pro *= Number(ex.postfixEval({
n: i
}))
}
return pro
},
pow10x: function (e) {
var x = 1
while (e--) {
x *= 10
}
return x
},
sigma: function (low, high, ex) {
var sum = 0
for (var i = low; i <= high; i++) {
sum += Number(ex.postfixEval({
n: i
}))
}
return sum
},
sin: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.sin(x)
},
sinh: function (x) {
return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2
},
sub: function (a, b) {
return a - b
},
tan: function (x) {
if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
return Math.tan(x)
},
tanh: function (x) {
return Mexp.sinha(x) / Mexp.cosha(x)
},
toRadian: function (x) {
return x * Math.PI / 180
}
}
Mexp.Exception = function (message) {
this.message = message
}
module.exports = Mexp

@@ -41,2 +41,7 @@ // This test is for node JS

assert.equal(a.lex("3^2-2^2").toPostfix().postfixEval(),5);
assert.equal(
Math.round((a.eval("(4-(2-1)^2)^.5") + Number.EPSILON) * 100) / 100,
Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
)
});

@@ -106,3 +111,6 @@

it('tan5(6+3)', function () {
assert.equal(a.eval("tan5(6+3)"),"1");
assert.equal(
Math.round((a.eval("tan45(6+3)") + Number.EPSILON) * 100) / 100,
Math.round((9 + Number.EPSILON) * 100) / 100
);
});

@@ -117,2 +125,10 @@ it('tan(40+5)', function () {

assert.equal(a.eval("root4"),"2");
assert.equal(
Math.round((a.eval("root(4-1^2)") + Number.EPSILON) * 100) / 100,
Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
)
assert.equal(
Math.round((a.eval("root(4-(2-1)^2)") + Number.EPSILON) * 100) / 100,
Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
)
});

@@ -175,1 +191,6 @@ it('checks + precedence before number insise parenthesis ', function () {

});
describe('Check autoclose of parenthesis of parser', function () {
it('should tell to compllete expression', function () {
assert.equal(a.eval("((2+3*4"),"14");
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc