Groovy Function – Infinity Multiplication – Version 3 – 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.
// 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: This is the normal usage version of infinity multiplication written for the Groovy Programming Language and is designed for a production environmen.
//         The first limitation of this version is how much memory the Groovy Programming Language allow the string to be store.
//         The second limitation is how much memory Groovy's environment 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 cost 500MB per string.
public class libZ
{
    public static String infiX(String a, String b)
    {
        boolean isaNeg = a[0] == '-'
        boolean isbNeg = b[0] == '-'
        a = a.replaceAll('^[-+]+', "")
        b = b.replaceAll('^[-+]+', "")
        a = a.replaceAll('^0+', "")
        b = b.replaceAll('^0+', "")

        int aDecPos = a.indexOf('.')
        int bDecPos = b.indexOf('.')
        int oDecPos = 0

        if ( aDecPos > -1){
            a = a.replaceAll('0+$', "")
            aDecPos = ((a.length() - 1 - aDecPos) > 0)? (a.length() - 1 - aDecPos) : 0
            a = a.replaceAll('[.]', "")
        }
        if ( bDecPos > -1){
            b = b.replaceAll('0+$', "")
            bDecPos = ((b.length() - 1 - bDecPos) > 0)? (b.length() - 1 - bDecPos) : 0
            b = b.replaceAll('[.]', "")
        }

        if ( (a.length() < 1 || b.length() < 1 ) ){
            return "0"
        }

        if ( a.equals('1') && aDecPos < 1 ){
            if ( bDecPos > 0 ){
                b = b.substring(0, b.length() - bDecPos).concat(".").concat(b.substring(b.length() - bDecPos))
                b = b.charAt(0) == '.'? "0".concat(b): b
                return ((isaNeg != isbNeg)? "-".concat(b) : b)
            }
            return ((isaNeg != isbNeg)? "-".concat(b) : b)
        }

        if ( b.equals('1') && bDecPos < 1 ){
            if ( aDecPos > 0 ){
                a = a.substring(0, a.length() - aDecPos).concat(".").concat(a.substring(a.length() - aDecPos))
                a = a.charAt(0) == '.'? "0".concat(a): a
                return ((isaNeg != isbNeg)? "-".concat(a) : a)

            }
            return ((isaNeg != isbNeg)? "-".concat(a) : a)
        }


        if ( aDecPos > -1 || bDecPos > -1  ){
            aDecPos = (aDecPos > -1)? aDecPos : 0
            bDecPos = (bDecPos > -1)? bDecPos : 0
            oDecPos = aDecPos + bDecPos
        }
        byte digit = 9
        long temp = 0
        int alen = a.length()
        int blen = b.length()
        int bi = blen
        int posr = 0
        int posw = 0
        String output = ""
        String pad0 = ""
        String tempstr = ""
        String leftoverstr = ""

        while ( pad0.length() < digit - 1 ){
            pad0 = "0" + pad0
        }

        while ( bi > 0 ){
            int outlen = output.length()
            int ai = alen
            long z = Long.parseLong(b.substring((bi - digit > -1? bi - digit : 0), bi))
            long leftover = 0

            if ( outlen < 1 ){
                while ( ai > 0){
                    temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover
                    tempstr = String.valueOf(temp)
                    leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0
                    tempstr = pad0.concat(tempstr)
                    output = tempstr.substring(tempstr.length() - digit).concat(output)
                    ai = ai - digit

                }
                if ( leftover > 0){
                    leftoverstr = pad0.concat(String.valueOf(leftover))
                    output = leftoverstr.substring(leftoverstr.length() - digit).concat(output)
                }
            } else {
                long tempadd = 0
                long outposval = 0
                int remainder = 0
                int loopidx = 0
                String tempaddstr = ""
                String outposstr = ""
                while ( ai > 0){
                    posw = (posr + loopidx) * digit
                    temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover + remainder

                    tempstr = String.valueOf(temp)
                    leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0
                    tempstr = pad0.concat(tempstr)

                    outlen = output.length()
                    outposstr = output.substring((outlen - posw - digit > 0 ? outlen - posw - digit : 0), (outlen - posw > 0 ? outlen - posw : 0))
                    outposval = outposstr.isEmpty()? 0 : Long.parseLong(outposstr)

                    tempadd = Long.parseLong(tempstr.substring(tempstr.length() - digit )) + outposval
                    tempaddstr = String.valueOf(tempadd)
                    remainder = tempaddstr.length() > digit? 1 : 0
                    tempaddstr = pad0.concat(String.valueOf(tempadd))

                    output = output.substring(0, (outlen - posw - digit > 0? outlen - posw - digit : 0)).concat(tempaddstr.substring(tempaddstr.length() - digit)).concat(output.substring((outlen - posw > 0? outlen - posw : 0), outlen))

                    loopidx = loopidx + 1
                    ai = ai - digit
                }
                if ( leftover + remainder > 0){
                    leftoverstr = pad0.concat(String.valueOf(leftover + remainder))
                    output = leftoverstr.substring(leftoverstr.length() - digit).concat(output)
                }
            }
            posr = posr + 1
            bi = bi - digit
        }

        if ( oDecPos > 0 ){
            while (output.length() < oDecPos){
                output = "0".concat(output)
            }
            output = output.substring(0, output.length() - oDecPos).concat(".").concat(output.substring(output.length() - oDecPos))
            output = output.replaceAll('0+$', "")
            output = output[-1].equals('.')? output[0..-2] : output
            output = output.replaceAll('^0+', "")
            output = output[0].equals('.') ? "0".concat(output) : output
            return (isaNeg != isbNeg? "-".concat(output): output)
        }
        output = output.replaceAll('^0+', "")
        return (isaNeg != isbNeg? "-".concat(output): output)
    }

}

String x = ".9999999999999999999999999999999"
String y = ".9999999999999999999999999999999"
c = libZ.infiX(x,y)
println(c)
Advertisement
StudioPress Premium WordPress Themes: Foodie Pro Theme


Random Article You May Like