JavaScript Function – Infinity Multiplication – Version vr-2 – Normal Usage Production Version

Advertisement

// "Copyright Notice", please do not remove.
// Written by Kevin Ng
// The full tutorial on this subject can be found @ http://kevinhng86.iblog.website or http://programming.world.edu 
// This source code file is a part of Kevin Ng's Z library.
// This source code is licenses under CCDL-1.0  A copy of CDDL1.0 can be found at https://opensource.org/licenses/CDDL-1.0
// End "Copyright Notice" 
//
// Notice: Version: InfiX-vr-2 (Infinity Multiplication) | Normal Usage | Bound by some limitation. 
//         The first limitation of this version is how much memory JavaScript allow the string to be store.
//         The second limitation is how much memory JavaScript can assign to the program.
//         If memory allow, for this version a string can only be less than 1,073,741,823 digits
//
//    Example of memory usage calculation:
//         500 megabyte string of digits multiply to another 500 megabyte string of digits.
//         1000 Mb of memory to store the strings outside of the function.
//         1000 Mb of memory for when the strings are passing into the function. 
//         1000 Mb for the answer string.
//          100 Mb (this can vary but most likely to be less than this) for script operation.
//         3100 Mb of memory is requires for adding two strings of digits that costed 500MB per string.
// 
// Noticeable Feature: You can change the digit variable to the amount of digit you want to work with at a time.
//                     The maximum amount of digit JavaScript can work with at the current time of this writing is 7.
//                     If you are working in the older JavaScript environment, the value of the digit variable may need to be set to a lower value.
//                     To check how many digits you can multiply to each other at a time.
//                     Use native multiplication, multiply two integer numbers that are full of nine and are same length wise in the amount of digits. 
//                     If the last digit in the result is a one and before that there is some zeroes and then one eight before the zero and then some nines before the eight, that mean the JavaScript environment you are working in support multiplication of digits up to that length.
//                     If the last digit in the result is a zero or anything else, that mean the JavaScript environment you are working in does not yet support multiplication that produce a true result value up to that length.
//                     Also the amount of digits in the result must be equal to the total amount of digits that was multiply together. 
//                     This is a simple check.                     
//
//        For example: var a = 9999999 * 9999999; | Produced: 99999980000001 when I wrote this. 
//                     var a = 99999999 * 99999999; | Produced: 9999999800000000 when I wrote this. 



function infiX(a, b){     
    var isaNeg = a[0] === "-",
        isbNeg = b[0] === "-",
    a = a.replace(/^[-+]+/g, "");
    b = b.replace(/^[-+]+/g, "");    
    a = a.replace(/^0+/g, "");
    b = b.replace(/^0+/g, ""); 
    
    var aDecPos = a.indexOf("."),
        bDecPos = b.indexOf("."),
        oDecPos = 0;
  
    if ( aDecPos > -1 ){
        a = a.replace(/0+$/g, "");
        aDecPos = (a.length - 1 - aDecPos) > 0? (a.length - 1 - aDecPos) : 0;
        a = a.replace(/[.]/g, ""); 
    }
    if ( bDecPos > -1 ){    
        b = b.replace(/0+$/g, "");
        bDecPos = (b.length - 1 - bDecPos) > 0? (b.length - 1 - bDecPos) : 0;
        b = b.replace(/[.]/g, "");
    }
    
    if ( (a.length < 1 || b.length < 1 ) ){
        return "0";    
    }
    
    if ( a == "1" && aDecPos < 1 ){
        if ( bDecPos > 0 ){
            b = b.slice(0, b.length - bDecPos) + "." + b.slice(b.length - bDecPos); 
            b = b[0] === "."? "0" + b: b;
            return ((isaNeg !== isbNeg)? "-" + b : b);
        }
        return ((isaNeg !== isbNeg)? "-" + b : b);
    }
  
    if ( b == "1" && bDecPos < 1 ){
        if ( aDecPos > 0 ){
            a = a.slice(0, a.length - aDecPos) + "." + a.slice(a.length - aDecPos); 
            a = a[0] === "."? "0" + a: a;
            return ((isaNeg !== isbNeg)? "-" + a : a);
        }
        return ((isaNeg !== isbNeg)? "-" + a : a);
    }

    if ( aDecPos > -1 || bDecPos > -1 ){
        aDecPos = aDecPos > -1? aDecPos : 0;
        bDecPos = bDecPos > -1? bDecPos : 0;        
        oDecPos = aDecPos + bDecPos;
    }
    
    var temp = 0,
        outposition = 0,
        alen = a.length,
        blen = b.length,
        output = "",
        bi = blen,
        posr = 0,
        posw = 0,
        digit = 7,
        digitneg = digit * -1,
        pad0 = "";
    while ( pad0.length < digit - 1){
        pad0 = "0" + pad0;
    }

    while ( bi > 0 ){
        var z = parseInt(b.substring((bi - digit > -1? bi - digit : 0), bi)),
            outlen = output.length,
            ai = alen,
            leftover = 0;

        if ( outlen < 1 ){        
            while ( ai > 0){
                temp = parseInt(a.substring((ai - digit > -1? ai - digit : 0), ai), 10) * z + leftover;
                leftover = parseInt(temp.toString().slice(0, digitneg)) || 0;
                output = (pad0 + temp.toString().slice(digitneg)).slice(digitneg) + output;
                ai = ai - digit;
            }
            output = leftover > 0? (pad0 + leftover.toString()).slice(digitneg) + output : output;
        } else {
            var remainder = 0,
                tempadd = 0,
                loopidx = 0;
                
            while ( ai > 0){
                posw = (posr + loopidx) * digitneg;
                temp = parseInt(a.substring((ai - digit > -1? ai - digit : 0), ai)) * z + leftover + remainder;
                leftover = parseInt(temp.toString().slice(0, digitneg)) || 0;
                tempadd = parseInt(temp.toString().slice(digitneg), 10) + ( parseInt(output.slice(posw + digitneg,posw ),10) || 0);   
                remainder = parseInt(tempadd.toString().slice(0, digitneg)) || 0;
                output = output.slice(0, posw + digitneg) + (pad0 + tempadd.toString().slice(digitneg)).slice(digitneg) + output.slice(posw);
                loopidx = loopidx + 1;
                ai = ai - digit;               
            }
             output = remainder + leftover > 0? (pad0 + (remainder + leftover).toString()).slice(digitneg) + output: output;
        }
        posr = posr + 1;
        bi = bi - digit;
    }
    if ( oDecPos > 0 ){
        while (output.length < oDecPos){
            output = "0" + output;
        }
        output = output.slice(0, output.length - oDecPos  ) + "." + output.slice(output.length - oDecPos);
        output = output.replace(/0+$/, "");
        output = output.replace(/[.]$/, "");
        output = output.replace(/^0+/, "");
        output = output[0] === "."? "0" + output : output;
        return (isaNeg !== isbNeg? "-" + output : output);
    }    
    output = output.replace(/^0+/, "");
    return (isaNeg !== isbNeg? "-" + output: output);
}


x = "1234567890123456789012345678901234567890";
y = "1234567890123456789012345678901234567890";

c = infiX(x,y);

document.write(c)
document.write("<br>");
Advertisement


Random Article You May Like