diff --git a/AdvDiv.js b/AdvDiv.js index 6a799c9..920204e 100644 --- a/AdvDiv.js +++ b/AdvDiv.js @@ -1,48 +1,84 @@ -function advdiv(n1, n2, r1, r2, rstr1, rstr2) { - var over = false; +function advdiv(n1, n2, minstr, decstr, rstr1, rstr2) { + var neg = false, over = false; var carry = 0, newcarry = 0, rcount = -1, x, y, d; - var n1string, n2string, res = "", result; + var res = "", result; var times10; var carries = []; var n1s; - var n1mc, n2mc, m1, m2; + var n2i, n1mc, n2mc, m1, m2; var sign, n1m, n2m; var n1s1; + var nre; + RegExp.escape = str => str.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&"); + + if(!minstr && minstr!=="") + minstr = "-"; + if(!decstr && decstr!=="") + decstr = "."; if(!rstr1 && rstr1!=="") rstr1 = "["; - if(!rstr2 && rstr2!="") + if(!rstr2 && rstr2!=="") rstr2 = "]"; - if(n2==0 && r2==0) + + if(n1.length==0 || n2.length==0 || /^\d*$/.test(minstr) || /^\d*$/.test(decstr) || /^\d*$/.test(rstr1) || /^\d*$/.test(rstr2)) + return false; + + nre = new RegExp("^("+RegExp.escape(minstr)+")?\\d*("+RegExp.escape(decstr)+"\\d*("+RegExp.escape(rstr1)+"\\d*"+RegExp.escape(rstr2)+")?)?$"); + + if(!nre.test(n1) || !nre.test(n2)) return false; - if(!r1) - r1 = 0; - sign = (parseFloat(n1) < 0 ? parseFloat(n2) >= 0 : parseFloat(n2) < 0) ? "-" : ""; - n1 = Math.abs(n1); - n2 = Math.abs(n2); - r1 = Math.abs(r1); - r2 = Math.abs(r2); - n1string = n1.toString(); - n2string = n2.toString(); - - if(r2!=0) { - n1m = n1string.replace(".", ""); - n2m = n2string.replace(".", ""); - - n1mc = parseInt(n1m+r1.toString()) - parseInt(n1m); - n2mc = parseInt(n2m+r2.toString()) - parseInt(n2m); - - if(n1string.indexOf(".") > -1) - m1 = Math.pow(10, n1string.length - n1string.indexOf(".") - 1 + r1.toString().length) - Math.pow(10, n1string.length - n1string.indexOf(".") - 1); + + if(n1.slice(0, minstr.length)==minstr) { + n1 = n1.slice(minstr.length); + neg = true; + }; + if(n2.slice(0, minstr.length)==minstr) { + n2 = n2.slice(minstr.length); + neg = !neg; + }; + + sign = neg ? minstr : ""; + + n1 = n1.replace(decstr, "."); + n2 = n2.replace(decstr, "."); + + if(n1[0]==".") + n1 = "0"+n1; + + if(n2[0]==".") + n1 = "0"+n2; + + r1 = (new RegExp(RegExp.escape(rstr1)+"(.+)"+RegExp.escape(rstr2)).exec(n1) || ["0", "0"])[1]; + n1 = n1.split(rstr1)[0].replace(/^0+/, "0").replace(r1=="0" ? /\.0*$/ : /\.*/, ""); + r2 = (new RegExp(RegExp.escape(rstr1)+"(.+)"+RegExp.escape(rstr2)).exec(n2) || ["0", "0"])[1]; + n2 = n2.split(rstr1)[0].replace(/^0+/, "0").replace(r2=="0" ? /\.0*$/ : /\.*/, ""); + + + if(n2=="0" && /^[0\.]+$/.test(r2)) + return false; + + if(n1=="0" && /^[0\.]+$/.test(r1)) + return "0"; + + if(r2!="0") { + n1m = n1.replace(".", ""); + n2m = n2.replace(".", ""); + + n1mc = parseInt(n1m+r1) - parseInt(n1m); + n2mc = parseInt(n2m+r2) - parseInt(n2m); + + if(n1.indexOf(".") > -1) + m1 = Math.pow(10, n1.length - n1.indexOf(".") - 1 + r1.toString().length) - Math.pow(10, n1.length - n1.indexOf(".") - 1); else m1 = Math.pow(10, r1.toString().length) - 1; - if(n2string.indexOf(".") > -1) - m2 = Math.pow(10, n2string.length - n2string.indexOf(".") - 1 + r2.toString().length) - Math.pow(10, n2string.length - n2string.indexOf(".") - 1); + if(n2.indexOf(".") > -1) + m2 = Math.pow(10, n2.length - n2.indexOf(".") - 1 + r2.toString().length) - Math.pow(10, n2.length - n2.indexOf(".") - 1); else m2 = Math.pow(10, r2.toString().length) - 1; - return advdiv(n1mc * m2, m1 * n2mc, 0, 0, rstr1, rstr2); + return sign+advdiv((n1mc * m2).toString(), (m1 * n2mc).toString(), minstr, decstr, rstr1, rstr2); }; times10 = function(nstring) { @@ -53,24 +89,24 @@ function advdiv(n1, n2, r1, r2, rstr1, rstr2) { return nstring + "0"; }; - while(n2string.indexOf(".") > -1) { - if(n1string.indexOf(".") < 0) { - n1string+= r1.toString()[0]; + while(n2.indexOf(".") > -1) { + if(n1.indexOf(".") < 0) { + n1+= r1.toString()[0]; if(r1.toString().length > 1) r1 = parseInt(r1.toString().slice(1)+r1.toString()[0]); } else - n1string = times10(n1string); - n2string = times10(n2string); + n1 = times10(n1); + n2 = times10(n2); }; - n1 = parseFloat(n1string); - n2 = parseInt(n2string); - n1s = n1string.split(""); - n1s1 = n1string.split(".")[0].split(""); + + n2i = parseInt(n2); + n1s = n1.split(""); + n1s1 = n1.split(".")[0].split(""); for(x = 0; x < n1s1.length; x++) { - d = Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s1[x])) / n2); + d = Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s1[x])) / n2i); res+= d.toString(); - carry = (parseInt(times10(carry.toString())) + parseInt(n1s1[x])) - n2 * d; + carry = (parseInt(times10(carry.toString())) + parseInt(n1s1[x])) - n2i * d; }; if(res=="") { res = "0"; @@ -85,25 +121,25 @@ function advdiv(n1, n2, r1, r2, rstr1, rstr2) { over = true; n1s.push(parseInt(r1.toString().split('')[(rcount) % r1.toString().length])); }; - newcarry = (parseInt(times10(carry.toString())) + parseInt(n1s[x])) - n2 * Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2); + newcarry = (parseInt(times10(carry.toString())) + parseInt(n1s[x])) - n2i * Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2i); if(over) { if(newcarry==0 && r1==0) { - res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2).toString(); - return sign+res.replace(/^0+|0$/gm, "").replace(/^\./, "0.").replace(/\.$/, ""); + res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2i).toString(); + return sign+res.replace(/^0+|0$/gm, "").replace(/^\./, "0.").replace(/\.$/, "").replace(".", decstr); }; for(y = 0; y < carries.length; y++) { if(over && carries[y]==newcarry && (y % r1.toString().length)==((rcount + 1) % r1.toString().length)) { - res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2).toString(); + res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2i).toString(); result = sign + ((res.slice(0, x - rcount + y)+"["+res.slice(x - rcount + y)+"]").replace(/^0+/gm, "").replace(/^\./, "0.")); if(result[result.indexOf("[") - 1]==result[result.indexOf("]") - 1]) result = result.slice(0, result.indexOf("[") - 1)+"["+result[result.indexOf("[") - 1]+result.slice(result.indexOf("[") + 1, result.indexOf("]") - 1)+"]"; if(result.indexOf("]")==result.indexOf("[") + 3 && result[result.indexOf("[") + 1]==result[result.indexOf("[") + 2]) result = result.slice(0, result.indexOf("[") + 2)+"]"; - return result.replace("[", rstr1).replace("]", rstr2); + return result.replace(".", decstr).replace("[", rstr1).replace("]", rstr2); }; }; }; - res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2).toString(); + res+= Math.floor((parseInt(times10(carry.toString())) + parseInt(n1s[x])) / n2i).toString(); if(over) carries.push(carry); carry = newcarry; diff --git a/AdvDiv.min.js b/AdvDiv.min.js index e55b419..53b3753 100644 --- a/AdvDiv.min.js +++ b/AdvDiv.min.js @@ -1 +1 @@ -function advdiv(a,r,t,e,n,s){var f=false;var i=0,p=0,I=-1,h,M,v;var l,u,o="",d;var F;var c=[];var g;var m,$,w,b;var j,k,q;var x;if(!n&&n!=="")n="[";if(!s&&s!="")s="]";if(r==0&&e==0)return false;if(!t)t=0;j=(parseFloat(a)<0?parseFloat(r)>=0:parseFloat(r)<0)?"-":"";a=Math.abs(a);r=Math.abs(r);t=Math.abs(t);e=Math.abs(e);l=a.toString();u=r.toString();if(e!=0){k=l.replace(".","");q=u.replace(".","");m=parseInt(k+t.toString())-parseInt(k);$=parseInt(q+e.toString())-parseInt(q);if(l.indexOf(".")>-1)w=Math.pow(10,l.length-l.indexOf(".")-1+t.toString().length)-Math.pow(10,l.length-l.indexOf(".")-1);else w=Math.pow(10,t.toString().length)-1;if(u.indexOf(".")>-1)b=Math.pow(10,u.length-u.indexOf(".")-1+e.toString().length)-Math.pow(10,u.length-u.indexOf(".")-1);else b=Math.pow(10,e.toString().length)-1;return advdiv(m*b,w*$,0,0,n,s)}F=function(a){if(a.indexOf(".")>-1&&a.indexOf(".")==a.length-2)return a.replace(".","");if(a.indexOf(".")>-1)return a.split(".")[0]+a.split(".")[1].split("")[0]+"."+a.split(".")[1].slice(1);return a+"0"};while(u.indexOf(".")>-1){if(l.indexOf(".")<0){l+=t.toString()[0];if(t.toString().length>1)t=parseInt(t.toString().slice(1)+t.toString()[0])}else l=F(l);u=F(u)}a=parseFloat(l);r=parseInt(u);g=l.split("");x=l.split(".")[0].split("");for(h=0;h=g.length){I++;f=true;g.push(parseInt(t.toString().split("")[I%t.toString().length]))}p=parseInt(F(i.toString()))+parseInt(g[h])-r*Math.floor((parseInt(F(i.toString()))+parseInt(g[h]))/r);if(f){if(p==0&&t==0){o+=Math.floor((parseInt(F(i.toString()))+parseInt(g[h]))/r).toString();return j+o.replace(/^0+|0$/gm,"").replace(/^\./,"0.").replace(/\.$/,"")}for(M=0;Mr.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&");if(!a&&a!=="")a="-";if(!t&&t!=="")t=".";if(!f&&f!=="")f="[";if(!n&&n!=="")n="]";if(r.length==0||e.length==0||/^\d*$/.test(a)||/^\d*$/.test(t)||/^\d*$/.test(f)||/^\d*$/.test(n))return false;q=new RegExp("^("+RegExp.escape(a)+")?\\d*("+RegExp.escape(t)+"\\d*("+RegExp.escape(f)+"\\d*"+RegExp.escape(n)+")?)?$");if(!q.test(r)||!q.test(e))return false;if(r.slice(0,a.length)==a){r=r.slice(a.length);i=true}if(e.slice(0,a.length)==a){e=e.slice(a.length);i=!i}m=i?a:"";r=r.replace(t,".");e=e.replace(t,".");if(r[0]==".")r="0"+r;if(e[0]==".")r="0"+e;r1=(new RegExp(RegExp.escape(f)+"(.+)"+RegExp.escape(n)).exec(r)||["0","0"])[1];r=r.split(f)[0].replace(/^0+/,"0").replace(r1=="0"?/\.0*$/:/\.*/,"");r2=(new RegExp(RegExp.escape(f)+"(.+)"+RegExp.escape(n)).exec(e)||["0","0"])[1];e=e.split(f)[0].replace(/^0+/,"0").replace(r2=="0"?/\.0*$/:/\.*/,"");if(e=="0"&&/^[0\.]+$/.test(r2))return false;if(r=="0"&&/^[0\.]+$/.test(r1))return"0";if(r2!="0"){b=r.replace(".","");j=e.replace(".","");l=parseInt(b+r1)-parseInt(b);o=parseInt(j+r2)-parseInt(j);if(r.indexOf(".")>-1)w=Math.pow(10,r.length-r.indexOf(".")-1+r1.toString().length)-Math.pow(10,r.length-r.indexOf(".")-1);else w=Math.pow(10,r1.toString().length)-1;if(e.indexOf(".")>-1)c=Math.pow(10,e.length-e.indexOf(".")-1+r2.toString().length)-Math.pow(10,e.length-e.indexOf(".")-1);else c=Math.pow(10,r2.toString().length)-1;return m+advdiv((l*c).toString(),(w*o).toString(),a,t,f,n)}E=function(r){if(r.indexOf(".")>-1&&r.indexOf(".")==r.length-2)return r.replace(".","");if(r.indexOf(".")>-1)return r.split(".")[0]+r.split(".")[1].split("")[0]+"."+r.split(".")[1].slice(1);return r+"0"};while(e.indexOf(".")>-1){if(r.indexOf(".")<0){r+=r1.toString()[0];if(r1.toString().length>1)r1=parseInt(r1.toString().slice(1)+r1.toString()[0])}else r=E(r);e=E(e)}M=parseInt(e);d=r.split("");k=r.split(".")[0].split("");for(u=0;u=d.length){g++;p=true;d.push(parseInt(r1.toString().split("")[g%r1.toString().length]))}I=parseInt(E(s.toString()))+parseInt(d[u])-M*Math.floor((parseInt(E(s.toString()))+parseInt(d[u]))/M);if(p){if(I==0&&r1==0){h+=Math.floor((parseInt(E(s.toString()))+parseInt(d[u]))/M).toString();return m+h.replace(/^0+|0$/gm,"").replace(/^\./,"0.").replace(/\.$/,"").replace(".",t)}for(v=0;v