# Working With Number – Infinity Multiplication – Chapter 3B – JavaScript

Thank you all my reader for following up on the “Infinity” subject. This chapter is the sub-release chapter of Chapter 3 – Optimised The Code for JavaScript in the Infinity subject. This is a very short chapter before I move onto chapter four of the subject. I will start releasing chapter four of the Infinity subject as early as Saturday this week or latest is by Sunday this week. The modification in the code is just to make the code operate a bit quicker and more flexible.

I modified the code to make the program more flexible in terms of to be able to change the amount of digit the program can work with at a time. At the time of writing this chapter for JavaScript, the JavaScript’s version I was working with was version 1.7. The amount of digit each number can have for multiplying two numbers together in JavaScript’s version 1.7 can’t be greater than the maximum of seven digits per number or 14 digits for both number. This is why the program was set to process seven digits at a time.

What had been modified? I introduced the pad0 variable into the code. The pad0 hold the amount of zero that need to be added to a result value for each cycle of multiplication. This was explained in the previous chapter. I changed the prepos variable name to digitneg to make the code more understandable. I also changed the method of getting the value for the remainder variable to be the exact same method as the method that gets the value for the leftover variable. I also secure the substring() function with a condition declaration when getting a digit from A string or B string. As you may know, if the first position’s value of the substring() function is a negative value and the second position’s value is a positive value, the substring() function will treat the first position as a zero position in the string. Nevertheless, this behavior may change between difference platforms. Therefore I did a conditional declaration to turn the negative value into zero without needing the aid from the substring() function.

After all the changes the same benchmark test for the previous version of infiX was applied to this version of infiX. For this version of infiX, the program took 3.940 seconds to computes the multiplication result 100,000 times for the same two strings of digits, each string contained 40 digits and both strings did not contain the decimal. The earlier production version for normal usage of infiX took 6.952 seconds to compute the result for the same test. With the decimal, this version of infiX took 4.247 seconds while the earlier version of infiX took 7.254 seconds to compute the result. This version of infiX-vr-2 was set to compute seven digits at a time.

I also updated the test code to make the program run more interactively and display additional meaningful information.

What took me so long to release this chapter although it was short? In the last couple days, I have been a bit busy. I just finished launching a brand new web directory for anyone who owns a blog or a website that contain a good amount of articles can submit their website to be inclusive in the directory. If you have a blog, please feel free to submit your website’s link at http://directory.iblog.website. Iblog.website‘s main site was also launched. I also launched a brand new web shop to separate the e-commerce operation from the blog. The shop can be found at http://shop.iblog.website.

 Previous Chapter: Optimised The Code Next Chapter: To Be Release

This below is the updated test code to compare the result from this version to any previous infiX program.

```var um = 0,
m  = 0;
for (var id = 0; id < 100000; id++){
var a1 = parseInt(Math.random() * (99999 - 0) + 0, 10),
a2 = parseInt(Math.random() * (99999 - 0) + 0, 10),
a3 = parseInt(Math.random() * (99999 - 0) + 0, 10),
a4 = parseInt(Math.random() * (99999 - 0) + 0, 10),
a5 = parseInt(Math.random() * (99999 - 0) + 0, 10),
ac1 = parseInt(Math.random() * (99999 - 0) + 0, 10),
ac2 = parseInt(Math.random() * (99999 - 0) + 0, 10),
ac3 = parseInt(Math.random() * (99999 - 0) + 0, 10),
ac4 = parseInt(Math.random() * (99999 - 0) + 0, 10),
ac5 = parseInt(Math.random() * (99999 - 0) + 0, 10),

var b1 = parseInt(Math.random() * (99999 - 0) + 0, 10),
b2 = parseInt(Math.random() * (99999 - 0) + 0, 10),
b3 = parseInt(Math.random() * (99999 - 0) + 0, 10),
b4 = parseInt(Math.random() * (99999 - 0) + 0, 10),
b5 = parseInt(Math.random() * (99999 - 0) + 0, 10),
bc1 = parseInt(Math.random() * (99999 - 0) + 0, 10),
bc2 = parseInt(Math.random() * (99999 - 0) + 0, 10),
bc3 = parseInt(Math.random() * (99999 - 0) + 0, 10),
bc4 = parseInt(Math.random() * (99999 - 0) + 0, 10),
bc5 = parseInt(Math.random() * (99999 - 0) + 0, 10),

var idn = 0,
idn2 = 0;
while ( idn < parseInt(Math.random() * (15 - 0) + 0, 10) ){
idn++;
}
while ( idn < parseInt(Math.random() * (30 - 15) + 15, 10) ){
idn++;
}
while ( idn2 < parseInt(Math.random() * (15 - 0) + 0, 10) ){
idn2++;
}
while ( idn2 < parseInt(Math.random() * (30 - 15) + 15, 10) ){
idn2++;
}

ta = "-" + a1.toString() + a2.toString() + a3.toString() + a4.toString() + a5.toString() + "." + padA1 + ac1.toString() + ac2.toString() + ac3.toString() + padA2 + ac4.toString() + ac5.toString();
tb = "+" + b1.toString() + b2.toString() + b3.toString() + b4.toString() + b5.toString() + "." + padB1 + bc1.toString() + bc2.toString() + bc3.toString() + padB2 + bc4.toString() + bc5.toString();

var test1 = infiX(ta, tb),
test2 = infiX2(ta, tb);

if (test1 !== test2){
um = um + 1;
document.write("Unmatch case #" + um + "<br>");
document.write(ta + " * " + tb + "<br>");
document.write(test1 + " | " + test2 + "<br><br>");
}
if (test1 === test2){
m = m + 1;
}

}
document.write("Matched: " + m + " case(s).<br>");
document.write("Unmatched: " + um + " case(s).<br>");
```

This is the infiX version vr 2 that was written for JavaScript.

```// "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
//
// 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 === "-",
isbNeg = b === "-",
a = a.replace(/^[-+]+/g, "");
b = b.replace(/^[-+]+/g, "");
a = a.replace(/^0+/g, "");
b = b.replace(/^0+/g, "");

bDecPos = b.indexOf("."),
oDecPos = 0;

if ( aDecPos > -1 ){
a = a.replace(/0+\$/g, "");
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" + 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" + a: a;
return ((isaNeg !== isbNeg)? "-" + a : a);
}
return ((isaNeg !== isbNeg)? "-" + a : a);
}

if ( aDecPos > -1 || bDecPos > -1 ){
bDecPos = bDecPos > -1? bDecPos : 0;
}

var temp = 0,
outposition = 0,
alen = a.length,
blen = b.length,
output = "",
bi = blen,
posr = 0,
posw = 0,
digit = 7,
digitneg = digit * -1,
while ( pad0.length < digit - 1){
}

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,
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" + 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>");``` Copyright secured by Digiprove © 2017

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Working With Number – Infinity Multiplication – Chapter 3B – JavaScript".
• 