math-expression-evaluator
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,3 +0,3 @@ | ||
/** math-expression-evaluator version 1.0.3 | ||
Dated:2015-07-16 */ | ||
/** math-expression-evaluator version 1.0.4 | ||
Dated:2015-07-18 */ | ||
@@ -173,2 +173,13 @@ (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){ | ||
var pre=str[str.length-1]; | ||
for(j=0;j<ptc.length;j++){ //loop over ptc | ||
if(ptc[j]===0){ | ||
if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){ | ||
str.push({value:")",type:5,pre:3,show:")"}); | ||
allowed=type1; | ||
asterick=type_3; | ||
inc(ptc,-1).splice(j,1); | ||
j=-1; | ||
} | ||
} | ||
} | ||
if(allowed[cType]!==true){ | ||
@@ -185,13 +196,2 @@ console.error(key+" is not allowed after "+prevKey); | ||
} | ||
for(j=0;j<ptc.length;j++){ //loop over ptc | ||
if(ptc[j]===0){ | ||
if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){ | ||
str.push({value:")",type:5,pre:3}); | ||
allowed=type1; | ||
asterick=type_3; | ||
inc(ptc,-1).splice(j,1); | ||
j=-1; | ||
} | ||
} | ||
} | ||
obj={value:cEv,type:cType,pre:cPre,show:cShow}; | ||
@@ -228,8 +228,3 @@ if(cType===0){ | ||
else if(cType===4){ | ||
if(ptc.indexOf(1)!=-1&&pre.type===4){ | ||
ptc.splice(ptc.indexOf(1),1); | ||
bracToClose++; | ||
continue; | ||
} | ||
else inc(ptc,2); | ||
inc(ptc,2); | ||
bracToClose++; | ||
@@ -241,3 +236,6 @@ allowed=type0; | ||
else if(cType===5){ | ||
if(!bracToClose)return; | ||
if(!bracToClose){ | ||
console.error("Closing parenthesis are more than opening one, wait What!!!"); | ||
return; | ||
} | ||
bracToClose--; | ||
@@ -253,3 +251,3 @@ allowed=type1; | ||
} | ||
if(pre.type!=1){ | ||
if(pre.type!==1){ | ||
str.push({value:0,type:1,pre:0}); | ||
@@ -259,3 +257,3 @@ inc(ptc,-1); | ||
allowed=type6; | ||
inc(ptc,2); | ||
inc(ptc,1); | ||
asterick=empty; | ||
@@ -330,3 +328,3 @@ pre.value+=cEv; | ||
if(ptc[j]===0){ | ||
str.push({value:")",type:5,pre:3}); | ||
str.push({value:")",show:")",type:5,pre:3}); | ||
inc(ptc,-1).pop(j); | ||
@@ -336,2 +334,5 @@ j--; | ||
} | ||
while(bracToClose--) | ||
str.push({value:")",show:")",type:5,pre:3}); | ||
str.push({type:5,value:")",show:")",pre:0}); | ||
@@ -338,0 +339,0 @@ return new Mexp(str); |
@@ -24,4 +24,4 @@ /* | ||
*/ | ||
/** math-expression-evaluator version 1.0.3 | ||
Dated:2015-07-16 */ | ||
!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}):23===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value: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}:11===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b+"</sup>"+e[f].show+"<sub>"+a+"</sub>",type:11}):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})):22===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:22}));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;d>e;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","Del","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","10^x","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin(","cos(","tan(","π","(",")","Del","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("," 10^(","0",".","+","-",",","Σ","n","Π","pow("],j=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")","Del",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.pow,"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,f.math.pow10x,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],k=[11,11,11,0,0,0,0,7,7,11,11,10,0,0,0,11,11,11,10,10,11,0,0,0,3,11,11,11,10,0,0,0,3,0,11,11,0,11,11,0,0,1,1,0,11,0,11,11],l=[0,0,0,3,4,5,12,11,11,0,0,0,1,1,1,0,0,0,0,11,11,1,1,1,2,7,0,0,2,1,1,1,2,13,0,0,3,0,0,1,6,9,9,21,22,23,22,8],m={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,17:!0,22:!0,23:!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,13:!0,17:!0,21:!0,22:!0,23:!0},o={0:!0,3:!0,4:!0,8:!0,17:!0,22:!0,23:!0},p={},q={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,17:!0,22:!0,23:!0},r={1:!0},s=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","=","e","0",".",",","n"],["pi","ln","Pi"],["Ans","sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh","10^x"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){a:for(i=0;i<a.length;i++){if(x=a[i].token.length,x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y])continue a;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].ev),k.push(a[i].preced),h.push(a[i].show)}},f.lex=function(a,b){"use strict";var c,i,t,u=[{type:4,value:"(",show:"(",pre:0}],v=[],w=a,x="",y=m,z=0,A=p,B="";"undefined"!=typeof b&&f.addToken(b);var C={};for(c=0;c<w.length;c++)if(" "!=w[c]){a:for(i=w.length-c>s.length-2?s.length-1:w.length-c;i>0;i--)for(t=0;t<s[i].length;t++)if(e(w,s[i][t],c,i)){x=s[i][t];break a}if(c+=x.length-1,""===x)return void console.error("Can't understand after "+w.slice(c));var D=g.indexOf(x),E=l[D],F=j[D],G=k[D],H=h[D],I=u[u.length-1];if(y[E]!==!0)return void console.error(x+" is not allowed after "+B);for(A[E]===!0&&(E=2,F=f.math.mul,H="×",G=3,c-=x.length),J=0;J<v.length;J++)0===v[J]&&-1!==[0,2,3,5,9,21,22,23].indexOf(E)&&(u.push({value:")",type:5,pre:3}),y=n,A=q,d(v,-1).splice(J,1),J=-1);if(C={value:F,type:E,pre:G,show:H},0===E)y=m,A=p,d(v,2).push(2),u.push(C),u.push({value:"(",type:4,pre:0,show:"("});else if(1===E)1===I.type?(I.value+=F,d(v,1)):u.push(C),y=n,A=o;else if(2===E)y=m,A=p,d(v,2),u.push(C);else if(3===E)u.push(C),y=n,A=q;else if(4===E){if(-1!=v.indexOf(1)&&4===I.type){v.splice(v.indexOf(1),1),z++;continue}d(v,2),z++,y=m,A=p,u.push(C)}else if(5===E){if(!z)return;z--,y=n,A=q,u.push(C)}else if(6===E){if(I.hasDec)return void console.error("Two decimals are not allowed in one number");1!=I.type&&(u.push({value:0,type:1,pre:0}),d(v,-1)),y=r,d(v,2),A=p,I.value+=F,I.hasDec=!0}else 7===E&&(y=n,A=q,d(v,1),u.push(C));8===E?(y=m,A=p,d(v,4).push(4),u.push(C),u.push({value:"(",type:4,pre:0,show:"("})):9===E?(9===I.type?"+"===I.value?(I.value=F,I.show=H,d(v,1)):"-"===I.value&&"-"===H&&(I.value="+",I.show="+",d(v,1)):5!==I.type&&7!==I.type&&1!==I.type&&3!==I.type&&23!==I.type?(u.push({value:f.changeSigna,type:0,pre:21,show:"-"}),d(v,1)):(u.push(C),d(v,2)),y=m,A=p):11===E?(y=m,A=p,d(v,2),u.push(C)):21===E?(y=m,A=p,u.push(C)):22===E?(y=m,A=p,d(v,6).push(6),u.push(C),u.push({value:"(",type:4,pre:0})):23===E&&(y=n,A=q,u.push(C)),d(v,-1),B=x}for(var J=0;J<v.length;J++)0===v[J]&&(u.push({value:")",type:5,pre:3}),d(v,-1).pop(J),J--);return u.push({type:5,value:")",show:")",pre:0}),new f(u)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a){"use strict";this.value=a};d.math={isDegree:!1,acos:function(a){return d.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.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;e>f&&(f=e,e=b);for(var g=f+1;a>=g;g++)c*=g;return c/d.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.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;a>=c;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;b>=e;e++)d*=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;b>=e;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.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.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}},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");Array.indexOf||(Array.prototype.indexOf=function(a){for(var b=0;b<this.length;b++)if(a==this[b])return b;return-1}),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||23===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(21===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&&c>e;)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].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].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)})):11===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):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 b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):f.push({type:1,value:g[i].value(c.value,b.value)})):22===g[i].type?(b=f.pop(),c=f.pop(),e=f.pop(),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):23===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));return f[0].value>1e15?"Infinity":Number(f[0].value.toFixed(15)).toPrecision()},b.exports=d},{"./postfix.js":4}]},{},[1])(1)}); | ||
/** math-expression-evaluator version 1.0.4 | ||
Dated:2015-07-18 */ | ||
!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}):23===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value: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}:11===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b+"</sup>"+e[f].show+"<sub>"+a+"</sub>",type:11}):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})):22===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:22}));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;d>e;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","Del","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","10^x","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin(","cos(","tan(","π","(",")","Del","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("," 10^(","0",".","+","-",",","Σ","n","Π","pow("],j=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")","Del",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.pow,"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,f.math.pow10x,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],k=[11,11,11,0,0,0,0,7,7,11,11,10,0,0,0,11,11,11,10,10,11,0,0,0,3,11,11,11,10,0,0,0,3,0,11,11,0,11,11,0,0,1,1,0,11,0,11,11],l=[0,0,0,3,4,5,12,11,11,0,0,0,1,1,1,0,0,0,0,11,11,1,1,1,2,7,0,0,2,1,1,1,2,13,0,0,3,0,0,1,6,9,9,21,22,23,22,8],m={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,17:!0,22:!0,23:!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,13:!0,17:!0,21:!0,22:!0,23:!0},o={0:!0,3:!0,4:!0,8:!0,17:!0,22:!0,23:!0},p={},q={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,17:!0,22:!0,23:!0},r={1:!0},s=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","=","e","0",".",",","n"],["pi","ln","Pi"],["Ans","sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh","10^x"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){a:for(i=0;i<a.length;i++){if(x=a[i].token.length,x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y])continue a;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].ev),k.push(a[i].preced),h.push(a[i].show)}},f.lex=function(a,b){"use strict";var c,i,t,u=[{type:4,value:"(",show:"(",pre:0}],v=[],w=a,x="",y=m,z=0,A=p,B="";"undefined"!=typeof b&&f.addToken(b);var C={};for(c=0;c<w.length;c++)if(" "!=w[c]){a:for(i=w.length-c>s.length-2?s.length-1:w.length-c;i>0;i--)for(t=0;t<s[i].length;t++)if(e(w,s[i][t],c,i)){x=s[i][t];break a}if(c+=x.length-1,""===x)return void console.error("Can't understand after "+w.slice(c));var D=g.indexOf(x),E=l[D],F=j[D],G=k[D],H=h[D],I=u[u.length-1];for(J=0;J<v.length;J++)0===v[J]&&-1!==[0,2,3,5,9,21,22,23].indexOf(E)&&(u.push({value:")",type:5,pre:3,show:")"}),y=n,A=q,d(v,-1).splice(J,1),J=-1);if(y[E]!==!0)return void console.error(x+" is not allowed after "+B);if(A[E]===!0&&(E=2,F=f.math.mul,H="×",G=3,c-=x.length),C={value:F,type:E,pre:G,show:H},0===E)y=m,A=p,d(v,2).push(2),u.push(C),u.push({value:"(",type:4,pre:0,show:"("});else if(1===E)1===I.type?(I.value+=F,d(v,1)):u.push(C),y=n,A=o;else if(2===E)y=m,A=p,d(v,2),u.push(C);else if(3===E)u.push(C),y=n,A=q;else if(4===E)d(v,2),z++,y=m,A=p,u.push(C);else if(5===E){if(!z)return void console.error("Closing parenthesis are more than opening one, wait What!!!");z--,y=n,A=q,u.push(C)}else if(6===E){if(I.hasDec)return void console.error("Two decimals are not allowed in one number");1!==I.type&&(u.push({value:0,type:1,pre:0}),d(v,-1)),y=r,d(v,1),A=p,I.value+=F,I.hasDec=!0}else 7===E&&(y=n,A=q,d(v,1),u.push(C));8===E?(y=m,A=p,d(v,4).push(4),u.push(C),u.push({value:"(",type:4,pre:0,show:"("})):9===E?(9===I.type?"+"===I.value?(I.value=F,I.show=H,d(v,1)):"-"===I.value&&"-"===H&&(I.value="+",I.show="+",d(v,1)):5!==I.type&&7!==I.type&&1!==I.type&&3!==I.type&&23!==I.type?(u.push({value:f.changeSigna,type:0,pre:21,show:"-"}),d(v,1)):(u.push(C),d(v,2)),y=m,A=p):11===E?(y=m,A=p,d(v,2),u.push(C)):21===E?(y=m,A=p,u.push(C)):22===E?(y=m,A=p,d(v,6).push(6),u.push(C),u.push({value:"(",type:4,pre:0})):23===E&&(y=n,A=q,u.push(C)),d(v,-1),B=x}for(var J=0;J<v.length;J++)0===v[J]&&(u.push({value:")",show:")",type:5,pre:3}),d(v,-1).pop(J),J--);for(;z--;)u.push({value:")",show:")",type:5,pre:3});return u.push({type:5,value:")",show:")",pre:0}),new f(u)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a){"use strict";this.value=a};d.math={isDegree:!1,acos:function(a){return d.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.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;e>f&&(f=e,e=b);for(var g=f+1;a>=g;g++)c*=g;return c/d.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.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;a>=c;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;b>=e;e++)d*=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;b>=e;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.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.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}},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");Array.indexOf||(Array.prototype.indexOf=function(a){for(var b=0;b<this.length;b++)if(a==this[b])return b;return-1}),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||23===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(21===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&&c>e;)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].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].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)})):11===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):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 b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):f.push({type:1,value:g[i].value(c.value,b.value)})):22===g[i].type?(b=f.pop(),c=f.pop(),e=f.pop(),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):23===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));return f[0].value>1e15?"Infinity":Number(f[0].value.toFixed(15)).toPrecision()},b.exports=d},{"./postfix.js":4}]},{},[1])(1)}); |
{ | ||
"name": "math-expression-evaluator", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A flexible math expression evaluator", | ||
@@ -5,0 +5,0 @@ "main": "src/formula_evaluator.js", |
@@ -126,2 +126,13 @@ | ||
var pre=str[str.length-1]; | ||
for(j=0;j<ptc.length;j++){ //loop over ptc | ||
if(ptc[j]===0){ | ||
if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){ | ||
str.push({value:")",type:5,pre:3,show:")"}); | ||
allowed=type1; | ||
asterick=type_3; | ||
inc(ptc,-1).splice(j,1); | ||
j=-1; | ||
} | ||
} | ||
} | ||
if(allowed[cType]!==true){ | ||
@@ -138,13 +149,2 @@ console.error(key+" is not allowed after "+prevKey); | ||
} | ||
for(j=0;j<ptc.length;j++){ //loop over ptc | ||
if(ptc[j]===0){ | ||
if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){ | ||
str.push({value:")",type:5,pre:3}); | ||
allowed=type1; | ||
asterick=type_3; | ||
inc(ptc,-1).splice(j,1); | ||
j=-1; | ||
} | ||
} | ||
} | ||
obj={value:cEv,type:cType,pre:cPre,show:cShow}; | ||
@@ -181,8 +181,3 @@ if(cType===0){ | ||
else if(cType===4){ | ||
if(ptc.indexOf(1)!=-1&&pre.type===4){ | ||
ptc.splice(ptc.indexOf(1),1); | ||
bracToClose++; | ||
continue; | ||
} | ||
else inc(ptc,2); | ||
inc(ptc,2); | ||
bracToClose++; | ||
@@ -194,3 +189,6 @@ allowed=type0; | ||
else if(cType===5){ | ||
if(!bracToClose)return; | ||
if(!bracToClose){ | ||
console.error("Closing parenthesis are more than opening one, wait What!!!"); | ||
return; | ||
} | ||
bracToClose--; | ||
@@ -206,3 +204,3 @@ allowed=type1; | ||
} | ||
if(pre.type!=1){ | ||
if(pre.type!==1){ | ||
str.push({value:0,type:1,pre:0}); | ||
@@ -212,3 +210,3 @@ inc(ptc,-1); | ||
allowed=type6; | ||
inc(ptc,2); | ||
inc(ptc,1); | ||
asterick=empty; | ||
@@ -283,3 +281,3 @@ pre.value+=cEv; | ||
if(ptc[j]===0){ | ||
str.push({value:")",type:5,pre:3}); | ||
str.push({value:")",show:")",type:5,pre:3}); | ||
inc(ptc,-1).pop(j); | ||
@@ -289,2 +287,5 @@ j--; | ||
} | ||
while(bracToClose--) | ||
str.push({value:")",show:")",type:5,pre:3}); | ||
str.push({type:5,value:")",show:")",pre:0}); | ||
@@ -291,0 +292,0 @@ return new Mexp(str); |
@@ -21,17 +21,33 @@ // This test is for node JS | ||
}); | ||
it('equal test', function () { | ||
it('formula test', function () { | ||
assert.equal(a.lex("2").toPostfix().formulaEval(),2); | ||
}); | ||
it('equal test', function () { | ||
it('formula test', function () { | ||
assert.equal(a.lex("sinpi").toPostfix().formulaEval(),"sin(π)"); | ||
}); | ||
it('equal test', function () { | ||
it('formula test', function () { | ||
assert.equal(a.lex("cos pi").toPostfix().formulaEval(),"cos(π)"); | ||
}); | ||
it('equal test', function () { | ||
it('formula test', function () { | ||
assert.equal(a.lex("tan(pi)").toPostfix().formulaEval(),"tan(π)"); | ||
}); | ||
it('equal test', function () { | ||
it('formula test', function () { | ||
assert.equal(a.lex("2(7-4)3").toPostfix().formulaEval(),"(2×(7-4))×3"); | ||
}); | ||
it('test to check the bug when number contains decimal', function () { | ||
assert.equal(a.lex("int2.3").toPostfix().postfixEval(),"2"); | ||
}); | ||
var str=""; | ||
var x=a.lex("sin4!").value; | ||
for(i=0;i<x.length;i++) | ||
str+=x[i].show; | ||
console.log(str); | ||
var str=""; | ||
var x=a.lex("sin(4)!").value; | ||
for(i=0;i<x.length;i++) | ||
str+=x[i].show; | ||
console.log(str); | ||
it('test to check auto correct of parenthesis mismatch if opening>closing', function () { | ||
assert.equal(a.lex("(2+(3-4").toPostfix().postfixEval(),"1"); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1221
47512
10