Skip to content

Commit

Permalink
Change arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ratajs committed Aug 17, 2023
1 parent a1bc70d commit 9a83279
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 46 deletions.
126 changes: 81 additions & 45 deletions AdvDiv.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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";
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion AdvDiv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a83279

Please sign in to comment.