Python 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 Python Programming Language and is designed for a production environmen.
#         The first limitation of this version is how much memory Python Programming Language allow the string to be store.
#         The second limitation is how much memory Python'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 costed 500MB per string.
class libZ:
    def infiX(self, a,b):

        isaNeg = 1 if a[0] is "-" else 0
        isbNeg = 1 if b[0] is "-" else 0
        a = a.lstrip("+-")
        b = b.lstrip("+-")
        a = a.lstrip("0")
        b = b.lstrip("0")
        aDecPos = a.find(".")
        bDecPos = b.find(".")
        oDecPos = 0

        if ( aDecPos > -1 ):
            a = a.rstrip("0")
            aDecPos = (len(a) - 1 - aDecPos) if (len(a)- 1 - aDecPos) > 0 else 0
            a = str.replace(a, ".", "")

        if ( bDecPos > -1 ):    
            b = b.rstrip("0")
            bDecPos = (len(b) - 1 - bDecPos) if (len(b) - 1 - bDecPos) > 0 else 0
            b = str.replace(b, ".", "")

        if ( len(a) < 1 or len(b) < 1  ):
            return "0"

        if ( a is "1" and aDecPos < 1 ):
            if ( bDecPos > 0 ):
                b = b[:len(b) - bDecPos] + "." + b[len(b) - bDecPos:]
                b = "0" + b if b[0] is "." else b
                return ( "-" + b if (isaNeg != isbNeg) else b)
            return ( "-" + b if (isaNeg != isbNeg) else b)

        if ( b is "1" and bDecPos < 1 ):
            if ( aDecPos > 0 ):
                a = a[:len(a) - aDecPos] + "." + a[len(a) - aDecPos:]
                a = "0" + a if a[0] is "." else a
                return ( "-" + a if (isaNeg != isbNeg) else a)
            return ( "-" + a if (isaNeg != isbNeg) else a)


        if (aDecPos > -1 or bDecPos > -1):
            aDecPos = aDecPos if aDecPos > -1 else 0
            bDecPos = bDecPos if bDecPos > -1 else 0
            oDecPos = aDecPos + bDecPos

        temp = 0
        alen = len(a)
        blen = len(b)
        output = ""
        posr = 0
        posw = 0
        digit = 5
        digitneg = digit * -1
        pad0 = ""
        for i in range(1,digit):
            pad0 = "0" + pad0

        for bi in range(blen,0,digitneg):
            z = int(b[(bi - digit if bi - digit > -1 else 0):bi])
            outlen = len(output)
            leftover = 0
            if ( outlen < 1 ):
                for ai in range(alen,0,digitneg):
                    temp = str(int( a[ (ai - digit if ai - digit > -1 else 0):ai]) * z + leftover)
                    leftover = int(temp[0:digitneg] or "0")
                    output = (pad0 + temp)[digitneg:] + output
                if ( leftover > 0 ):
                    output = (pad0 + str(leftover))[digitneg:] + output
            else:
                remainder = 0
                tempadd = 0
                loopidx = 0
                for ai in range(alen,0,digitneg):
                    posw = (posr + loopidx) * digitneg
                    temp = str( int(a[(ai - digit if ai - digit > -1 else 0):ai]) * z + leftover + remainder)
                    leftover = int(temp[0:digitneg] or "0")
                    tempadd = str(int( temp[digitneg:]) + int(output[posw + digitneg:posw] or "0"))
                    remainder = int(tempadd[0:digitneg] or "0")
                    output = output[0:posw + digitneg] + (pad0 + tempadd[digitneg:])[digitneg:] + output[posw:]
                    loopidx = loopidx + 1

                if ( remainder + leftover > 0 ):
                    output = (pad0 + str(leftover + remainder))[digitneg:] + output

            posr = posr + 1

        if ( oDecPos > 0 ):
          while (len(output) < oDecPos):
            output = "0" + output
          output = output[:(len(output) - oDecPos)] + "." + output[(len(output) - oDecPos) :]
          output = output.rstrip("0")
          output = output.rstrip(".")
          output = output.lstrip("0")
          output =  "0" + output if output[0] is "." else output
          return ("-" + output if isaNeg != isbNeg else output)


        output = output.lstrip("0")
        return ("-" + output if isaNeg != isbNeg else output)


x = "+99999999999999999999999999999999999999999.999999999999999999999999999999999999999999"
y = "-99999999999999999999999999999999999999999.999999999999999999999999999999999999999999"
e = libZ()
print(e.infiX(x,y))
Advertisement


Random Article You May Like