Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- if, for some reason, you ever wanted super precise unlimited multiplication, then here you have it -->
- <!-- only limited to where the carry over is greater than 9007199254740992 -->
- <!-- I think your computer would've ran out of memory by then -->
- <!-- Actually on second thought, the stack would overflow far sooner than reaching an actual limit -->
- <script>
- function MShift(res,i,j)
- {
- var dy=0,dx=0;
- if(j>0)dx=1;
- else if(i>0) dy=1;
- else return;
- var rem = res[i][j]%10;
- var car = (res[i][j]-rem)/10;
- res[i-dy][j-dx]+=car;
- res[i][j]=rem;
- if(res[i-dy][j-dx]>9)MShift(res,i-dy,j-dx);
- }
- function Multiply(a,b)
- {
- a=a.toString();
- b=b.toString();
- var adot = a.length-a.indexOf(".")-1;
- var bdot = b.length-b.indexOf(".")-1;
- if(adot==a.length)adot=0;
- if(bdot==b.length)bdot=0;
- var dotpos = adot+bdot;
- a = a.replace(/\./g,"");
- b = b.replace(/\./g,"");
- var al=a.length;
- var bl=b.length;
- if(al==0||bl==0)return NaN;
- var res = [];
- for(var i=0; i<al; i++)
- {
- res[i] = [];
- for(var j=0; j<bl; j++)
- {
- res[i][j] = a[i]*b[j]+(i>0&&j<bl-1?res[i-1][j+1]:0);
- if(res[i][j]>9)MShift(res,i,j);
- }
- }
- for(var i=0; i<al-1; i++)
- {
- res[i] = res[i][0];
- }
- var ret = res.toString().replace(/,/g,"");
- if(dotpos>0)ret = ret.substring(0,ret.length-dotpos)+"."+ret.substring(ret.length-dotpos);
- return ret;
- }
- function doMult()
- {
- var left = document.getElementById("left");
- var right = document.getElementById("right");
- var out = document.getElementById("out");
- out.innerHTML = Multiply(left.value,right.value);
- }
- </script>
- <input id="left" oninput="doMult()" /> x <input id="right" oninput="doMult()" /> = <span id="out"></span>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement