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

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 PHP 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. PHP does support long integer on 64 bits architecture system and most computer now a day does support 64 bits architecture. Therefore I utilize the maximum capable of the long integer. Which is to be able to multiply a number that is nine digits in length to another number that is nine digits in length. In the event where you work on a 32 bits system, you can change the \$digit variable value to five and the program will still operate. Maybe in the future, we can have a double long integer, if so you may be able to set the value of the \$digit variable to 17+.

What had been modified? I introduced the \$pad0 variable into the code. The \$pad0 variable 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.

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 7.998 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 14.333 seconds to compute the result for the same test. With the decimal, this version of infiX took 8.729 seconds while the earlier version of infiX took 14.801 seconds to compute the result. This version of infiX-vr-2 was set to compute nine 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: Beyond Memory

This below is the updated test code to compare the result from this version to any previous infiX program. If you run this test code from the console, it will be more interactive and does not just produce a blank screen waiting for the result. I tested ten millions cases with this test code.

```\$um = 0;
\$m = 0;
for (\$id = 0; \$id < 10000; \$id++){
\$a1 = intval(rand(0,99999));
\$a2 = intval(rand(0,99999));
\$a3 = intval(rand(0,99999));
\$a4 = intval(rand(0,99999));
\$a5 = intval(rand(0,99999));
\$ac1 = intval(rand(0,99999));
\$ac2 = intval(rand(0,99999));
\$ac3 = intval(rand(0,99999));
\$ac4 = intval(rand(0,99999));
\$ac5 = intval(rand(0,99999));

\$b1 = intval(rand(0,99999));
\$b2 = intval(rand(0,99999));
\$b3 = intval(rand(0,99999));
\$b4 = intval(rand(0,99999));
\$b5 = intval(rand(0,99999));
\$bc1 = intval(rand(0,99999));
\$bc2 = intval(rand(0,99999));
\$bc3 = intval(rand(0,99999));
\$bc4 = intval(rand(0,99999));
\$bc5 = intval(rand(0,99999));

\$idn = 0;
\$idn2 = 0;
while ( \$idn < intval(rand(0,15)) ){
\$idn++;
}
while ( \$idn < intval(rand(15,30)) ){
\$idn++;
}
while ( \$idn2 < intval(rand(0,15)) ){
\$idn2++;
}
while ( \$idn2 < intval(rand(15,30)) ){
\$idn2++;
}

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

\$test1 = infiX(\$ta, \$tb);
\$test2 = infiX2(\$ta, \$tb);

echo "Case #\$id: \$ta * \$tb\n";
if (\$test1 != \$test2){
\$um = \$um + 1;
echo ("Question" .  " : " . strval(\$ta) . " * " . strval(\$tb) . "\n");
echo ("Version 3: " . strval(\$test1) . " | Version 2: " . strval(\$test2) . "\n\n");
}

if (\$test1 == \$test2){
\$m = \$m + 1;
}
echo "\n";

}
echo "Matched \$m cases.\n";
echo "Unmatched \$um cases.";
```

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

```<?php
// "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.
// Release date to http://programming.world.edu will lag one week after release on http://kevinhng86.iblog.website
// 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 PHP allow the string to be store.
//         The second limitation is how much memory PHP 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.
//                     Since PHP natively support long integer, therefore this program built to utilise close to the maximum value that the long integer can store.
//                     In the event where you work in the environment that only offer a 32 bits integer, you can change the value for the \$digit variable to five.

function infiX(\$a, \$b){
\$isaNeg = \$a[0] == "-";
\$isbNeg = \$b[0] == "-";
\$a = ltrim(\$a, "-+");
\$b = ltrim(\$b, "-+");
\$a = ltrim(\$a, "0");
\$b = ltrim(\$b, "0");

\$aDecPos = strpos(\$a, ".");
\$bDecPos = strpos(\$b, ".");
\$oDecPos = 0;

if ( \$aDecPos > -1){
\$a = rtrim(\$a, "0");
\$aDecPos = ((strlen(\$a) - 1 - \$aDecPos) > 0)? (strlen(\$a) - 1 - \$aDecPos) : 0;
\$a = str_replace(".", "", \$a);
}
if ( \$bDecPos > -1){
\$b = rtrim(\$b, "0");
\$bDecPos = ((strlen(\$b) - 1 - \$bDecPos) > 0)? (strlen(\$b) - 1 - \$bDecPos) : 0;
\$b = str_replace(".", "", \$b);
}

if ( strlen(\$a) < 1 || strlen(\$b) < 1 ){
return "0";
}

if ( \$a == "1" && \$aDecPos < 1 ){
if ( \$bDecPos > 0 ){
\$b = substr(\$b, 0, strlen(\$b) - \$bDecPos) . "." . substr(\$b, strlen(\$b) - \$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 = substr(\$a, 0, strlen(\$a) - \$aDecPos) . "." . substr(\$a, strlen(\$a) - \$aDecPos );
\$a = \$a[0] == "."? "0" . \$a: \$a;
return ((\$isaNeg != \$isbNeg)? "-" . \$a : \$a);
}
return ((\$isaNeg != \$isbNeg)? "-" . \$a : \$a);
}

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

\$temp = 0;
\$alen = strlen(\$a);
\$blen = strlen(\$b);
\$output = "";
\$bi = \$blen;
\$posr = 0;
\$posw = 0;
\$digit = 9;
\$digitneg = \$digit * -1;

while (strlen(\$pad0) < \$digit - 1){
}

while ( \$bi > 0 ){
\$z = intval(substr(\$b, (\$bi - \$digit > -1? \$bi - \$digit : 0), (\$bi - 0 < \$digit? \$bi - 0: \$digit)));
\$outlen = strlen(\$output);
\$ai = \$alen;
\$leftover = 0;

if ( \$outlen < 1 ){
while ( \$ai > 0){
\$temp = intval(substr(\$a, (\$ai - \$digit > -1? \$ai - \$digit : 0), (\$ai - 0 < \$digit? \$ai - 0: \$digit))) * \$z + \$leftover;
\$leftover = intval(substr(strval(\$temp),0, \$digitneg)) ;
\$output = substr(\$pad0 . substr(strval(\$temp), \$digitneg), \$digitneg) . \$output;
\$ai = \$ai - \$digit;

}
\$output = \$leftover > 0? substr(\$pad0 . substr(strval(\$leftover), \$digitneg), \$digitneg) . \$output : \$output;
} else {
\$remainder = 0;
\$loopidx = 0;
while ( \$ai > 0){
\$posw = (\$posr + \$loopidx) * \$digitneg;
\$temp = intval(substr(\$a, (\$ai - \$digit > -1? \$ai - \$digit : 0), (\$ai - 0 < \$digit? \$ai - 0: \$digit))) * \$z + \$leftover + \$remainder;
\$leftover = intval(substr(strval(\$temp),0, \$digitneg)) ;
\$tempadd = intval(substr(strval(\$temp),\$digitneg)) + intval(substr(\$output,\$posw + \$digitneg,\$posw)) ;
\$remainder = intval(substr(strval(\$tempadd),0, \$digitneg));
\$output = substr(\$output,0, \$posw + \$digitneg) . substr(\$pad0 . substr(strval(\$tempadd),\$digitneg), \$digitneg) . substr(\$output,\$posw);
\$loopidx = \$loopidx + 1;
\$ai = \$ai - \$digit;
}
\$output = (\$remainder + \$leftover > 0)? substr(\$pad0 . strval(\$remainder + \$leftover), \$digitneg) . \$output: \$output;
}
\$posr = \$posr + 1;
\$bi = \$bi - \$digit;
}

if ( \$oDecPos > 0 ){
while (strlen(\$output) < \$oDecPos){
\$output = "0" . \$output;
}
\$output = substr(\$output, 0, strlen(\$output) - \$oDecPos) . "." . substr(\$output, strlen(\$output) - \$oDecPos );
\$output = rtrim(\$output, "0");
\$output = rtrim(\$output, ".");
\$output = ltrim(\$output, "0");
\$output = \$output[0] === "."? "0" . \$output : \$output;
return ( \$isaNeg != \$isbNeg? "-" . \$output : \$output );
}
\$output = ltrim(\$output, "0");
if ( \$isaNeg != \$isbNeg ){
\$output = "-" . \$output;
}
return \$output;
}

\$x = "+1234567890123456789012345678901234567890";
\$y = "-1234568790123456789012345678901234567890";
echo infiX(\$x, \$y) . "\n";

?>```

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Working With Number – Infinity Multiplication – Chapter 3B – PHP".
Original Post Link: http://kevinhng86.iblog.website/2017/03/03/working-with-number-infinity-multiplication-chapter-3b-php/.

## One thought on “Working With Number – Infinity Multiplication – Chapter 3B – PHP”

1. Jane says:

Hi thete just wanted too give yyou a quicck heafs
up. Thee texdt iin your post seem too be running offf thhe screeen iin Chrome.
I’m nnot surfe iff this is a formatting issue oor someting too ddo with web broser compatibility but I thouht I’d
post tto llet yyou know. Thhe layoutt llook grteat though!
Hopee youu get tthe problem resollved soon. Kudcos It’s thee beest ttime tto
makke some plan forr thee futre and itt iis time tto bbe
happy. I’ve read thiis popst andd iif I could I desite tto sufgest yyou feew interestting thinbs orr tips.
Maybe youu could wrote next artiles rederring to ths article.
I wish too rea even molre thikngs about it! Hi, I ddo believe
this is a great website. I stumbledupon itt 😉 I will reviswit
yeet agaijn sincee i have ssaved aas a favorit
it. Monry aand freedom iss the best waay tto change, maay youu bbe ruch annd continje tto hep others.