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

Advertisement
Boost your Website Traffic with iNeedHits Today! Shop Now!

<?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  ){
        $aDecPos = ($aDecPos > -1)? $aDecPos : 0;
        $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;
    $pad0 = "";

    while (strlen($pad0) < $digit - 1){
        $pad0 = "0" . $pad0;
    }

    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;
            $tempadd = 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";

?>
Advertisement


Random Article You May Like