Working With Number – Infinity Multiplication – Decimal, Precise Float Calculation – Python

In the previous chapter of infinity multiplication, we were discussing in regard to storing two integer numbers in string format and how to apply mathematical multiplication formula on the two strings by multiplying one digit at a time to produce an answer.

When it’s come to applying a mathematical formula on numbers that contains decimal values, since computer stores integer binary value as a whole digit and their base increase by the power of two for every additional binary digit, it is not possible to inject a decimal into a correct binary position. Thus computer software will usually utilize a floating point calculation formula on numbers which contains decimal values. Numbers with decimal values are stored as a float type in the computer. Thus numbers with decimal values usually describe as a float when it’s related to programming or computer.

Float calculation formula can be very precise on certain values. Nevertheless, there are values which float calculation have to round off some of the digits on the right side of the decimal to get a near precision value. These type of rounding can be very minor in our daily life and likely to not affect our day to day mathematical calculation.

Nevertheless, if we truly want to calculate a decimal value that can be thousand of digits long, is there a solution to that? This is the second chapter of “Infinity Multiplication”, “Decimal, Precise Float Calculation”.

To have a little hint on this infinity subject, most of my readers ask me if this is truly an infinity library and is this truly an infinity subject? Infinity is the subject that is very hard to explain in words. Since the values can be endless and since it is endless, can we really calculate it? This is my formula for calculating an infinity value “infC = infA = infAL” or can be represent with “C = A = AL”, which can also translate to “Infinity Calculation equal to Infinity Assumption and is equal to Infinity Alteration”. I will discuss in regard to the infinity formula and how to apply the formula in a programming perspective in chapter five of the infinity multiplication, division, adding and subtracting subject. Chapter five of the infinity subject will only be available in full at http://kevinhng86.iblog.website, Programming World, and sites that have permission from me to posts the article in full content.

For the next chapter of infinity multiplication which is chapter three that is scheduled to be released this week, the chapter will contain the optimized source for multiplication that can be used for a production environment. With the source code, the tester which I used for testing hundreds of thousand of test cases against my code will also be released with the article. The only code that I do not release with the article is the benchmark test code, which tests how fast the program run versus 100,000 or test 1,000,000 cases.

This article was written for the Python Programming Language. The source code to this article was written for the Python Programming Language. Information related to how to multiply two string of digits together without the decimal can be referred to from chapter one of this article, Infinity Multiplication, “Beyond Integer”.

Previous Chapter: Beyond IntegerNext Chapter: Optimised The Code

How To Multiply Two String Of Digits Together With Precise Decimal Calculation

To simplify the explanation for this article, we are assuming that we are multiplying a string full of digits which name A to another string that is full of digits which name B. When multiplying two strings of digits together to get a result value, the first challenge we would face for applying the mathematical equation on the strings is the decimal value or the digits behind the decimal. For addition and subtracting we can split the digit in A string into two parts. One part can contain the digits before the decimal and the other part can contain the digits behind the decimal. However, this technique will be very complicated when applying to multiplication. This is because of, every digit, whether if before or after the decimal in string A has to be multiplied to every digit in string B.

If we split A string into two parts, it’s can get very complicated to manipulate each part of A string to properly multiply to each part of B string, consider that they are both carry a decimal value. It’s doable, however complicated. We can have another simple solution, of which we can remove the decimal from both A and B string and treat the string as if the string does not contain the decimal. After we finished with the mathematical multiplication procedure we can inject the decimal into the correct position for our answer string.

In my opinion, the method of injecting the decimal into the correct position is highly efficient, is error proof and is simpler for loop controlling. This method is doable for multiplication due to how the decimal is placed in a multiplication procedure. Nevertheless, let first inspect the formula for calculating a decimal value in a mathematical multiplication procedure.

Formula: Calculating the decimal value for multiplication.
Step 1: Count the amount of digit behind the decimal in A string that is not a trailing zero. Remove trailing zero. 
Step 2: Count the amount of digit behind the decimal in B string that is not a trailing zero. Remove trailing zero.
Step 3: Add the number of digits that are behind the decimal for both A and B string together.
        In my description, this value is called the total amount of digits that need to be behind the decimal in our answer string.
Step 4: Remove the decimal from both A and B string, then apply the multiplication procedure on A and B string.
Step 5: Once the answer string is ready, count from right to left of the answer string.
        Using the value from step three and place the decimal in front of the total amount of digits that need to be behind the decimal in our answer string.
        If the answer string contains fewer digits than the count of step three, add a zero to the beginning of the answer string.
Answer: After placing the decimal, we can remove leading and trailing zeroes. 
        Trailing zeroes in decimal value do not alter the value of the number.
        Leading zeroes in a whole number do not alter the value of the number.

Example: 0.1 x 0.1
Step 1:   (1 digits behind the decimal in A) + (1 digits behind the decimal in B) = (2 digits behind the decimal needed in the answer string)
Step 2:   1 x 1 = 1   |  Answer string:    1  
Step 3:   Answer string has only 1 digit, there need to be 2 digits behind the decimal. Add zero to beginning of the answer string 
          Answer string: 01
Step 4.        .01      | add zero in front of the decimal for formatting | answer string = 0.01
Answer:  0.1 * 0.1 = 0.01

Example: 9.125 x 33.100
Step 1:   (3 digits behind the decimal in A) + (1 digits behind the decimal in B) = (4 digits behind the decimal needed in the answer string)
Step 2:   9125 x 331 = 3020375 |  Answer string:    3023075  
Step 3:   Answer string has more digits than the total digits needed to be behind the decimal. No need to modify. 
          Answer string: 3020375
Step 4.        302.0375    
Answer:  9.125 * 33.100 = 302.0375

Example: 152 x 239.55
Step 1:   (0 digits behind the decimal in A) + (2 digits behind the decimal in B) = (2 digits behind the decimal needed in the answer string)
Step 2:   152 x 23955 = 3641160  |  Answer string:    3023075  
Step 3:   Answer string has more digits than the total digits needed to be behind the decimal. No need to modify. 
          Answer string: 3641160
Step 4.        36411.60 | remove trailing zero for formatting | answer string is 36411.6   
Answer:  152 * 239.55 = 36411.6

With the formula above put in perspective, calculating the decimal values in a multiplication procedure can be done through indexing the decimal. This method is more of a string manipulating method. The placement for the decimal is calculated before calculating any actual value. In my opinion, this method is the simplest method for calculating the decimal for any multiplication procedure, and this method offers the best precision placement for the decimal value as this method calculates the exact spot the decimal have to be placed at in the answer string.

Besides the formula, we still have shortcuts. Any numbers multiply by one will return itself as the value and any numbers that multiply by zero will always be zero. There is another shortcut that I do not apply to this formula and that is any number that multiplied to a .1 or .01 and so on will only need the decimal placement to be moved over to the left.

Nevertheless, let look at some code block that we need for our multiplication procedures with decimal supported. For the first procedure, we will need to search for the decimal in both our A string and B string. If the decimal is found, we need the position of the decimal. If the decimal is not found within the string, it is preferable that we will get a “-1” as the value for our variable. We are also declaring a position variable to later calculate the decimal position for our answer string. In Python, we can use the find() string prototype function on the string to get the position of the decimal in the string. If the string doesn’t contain a decimal, the find() function will return “-1” as a value.

aDecPos = a.find(".")
bDecPos = b.find(".")
oDecPos = 0

To calculate how many digits behind the decimal in either our A or B string, we are subtracting the total length of the string to the decimal position in the string. Before getting the decimal position we have to remove any trailing zero in the string. This is why it is important that this code block below only executed if a decimal is found. Since we are removing trailing zeroes, we can have issues with strings that contains only zero digits behind the decimal. Thus if the total length of the string minus the decimal position go into the negative we have to set the value to zero. Zero also mean, there is not a digit behind the decimal.

After we calculated the amount of digits that are behind the decimal in the string, we are also removing the decimal. This code block below is demonstrating the previous procedures.

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

Since we removed the decimal and any trailing zero, if we want to use the shortcut of anything multiply by one will return itself as an answer, we would have to modify our code block for the shortcut procedure. This code block below will only execute if A string contained only a digit one and A string does not contain any digits behind the decimal. If we found that B string does, in fact, contained a decimal before this loop started, we placed the decimal back into B string at the correct position before we return B as the result.

To put the decimal into the correct position for B string, we get the digits from the zero position to the digit where the decimal is going to be placed. To calculate where the decimal is going to be placed, we get the total length of string B and subtract the value to the amount of digit B has after the decimal. We then add the decimal into the string and then we add the digits from where the decimal is placed to the end of the string. After placing the decimal and if the first character in B string is only the decimal, we can place a zero in front of the decimal. This is to correct input format such as “.123” or “.5774” or “.1111”.

After that we check if A and B is difference in positive and negative value. If they are, we can place a negative sign in front of B string and then we return B as our result. If B does not contain a decimal value, we simply return B. We also have to check for differences in negative and positive values in both A and B string. This code block below demonstrated the above procedures.

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)

After evaluating all the shortcuts for a multiplication procedure, we can then calculate the amount of digit we need to have behind the decimal in our answer string. This code block will only execute if either A or B was found with a decimal. If A is found with a decimal and B did not contain a decimal, the value for variable bDecPos would be a -1. Therefore if there was not a decimal in B we can set bDecPos as zero value. It’s would also mean, there is not a single digit behind the decimal in B string. We can then add up the total amount of digits behind the decimal in both A and B string together to get the total amount of digits that would need to be behind the decimal in our answer string. This code block below demonstrates the above procedures.

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

After the multiplication procedure and when we have an answer string, we can evaluate and place the decimal at the correct position in our answer string. We first check to see whether if our answer string contained fewer digits than the amount of digit that would need to be behind the decimal. This scenario occurs in events where “0.01 x 0.01” or “0.3 x 0.3” and so on.

To produce a new answer string with a decimal placement, we would grab the digits from the first position from the left side of our answer string to where the decimal is going to be placed. We then add the decimal behind these digits. After adding the decimal into the correct position, we add the rest of the digits in our answer string to the right side of the decimal. This new string’s value is now assigned as our answer string. To calculate where the decimal is going to be placed, we would need to subtract the length of our answer string to the amount of digit needed to be behind the decimal.

We then trim off any leading and trailing zero in our answer string. If the decimal is the last character in the string and there is not a digit behind the decimal, we would also remove the decimal. If the decimal is the first character in our answer string, we would place a zero in front of the decimal.

This code block below demonstrate the previous mentioned procedure in the Python Programming’s environment.

if ( oDecPos > 0 ):
     output = "0" + output if (len(output) < oDecPos) else 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	

This below is the fully functional code for this article which written for the Python Programming’s environment. The code is capable of calculating numbers which can contain more than thousand of digits with precise decimal calculation.

Advertisement

# Written by Kevin Ng
# The full tutorial on this subject can be found @ http://kevinhng86.iblog.website 
# This source code file is a part of Kevin Ng Z library.
# To use this source code in your project please give the copyright credit.
# This function only multiply and does not check to see if it is only number in the string, you must build a checker around it.
# Notice: Version one and two of any infinity code from the libZ library are prototype.
#         They are not meant for production environment due to efficentcy.
#         Although are prototype these script were tested and ran through 300,000+ test cases without producing any errors.
#         This is version two of infinity multiplication for Python, with precise decimal calculation.

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
        outposition = 0
        alen = len(a)
        blen = len(b)
        output = ""

        for i in range((blen - 1), -1, -1):
          z = int(b[i])
          leftover = 0
          outtemp = ""
          aidx = alen - 1

          while ( aidx > -1 ):
            temp = int(a[aidx]) * z + leftover
            leftover = int(str(temp)[0:-1]) if (temp > 9) else 0
            outtemp = str(temp)[-1] + outtemp  
            aidx = aidx - 1
            
          outtemp = str(leftover) + outtemp if (leftover > 0) else outtemp
        
          if ( len(output) < 1):
            output = outtemp
            outtemp = ""
            outposition = outposition + 1
        
          else:
            tempadd = 0
            remainder = 0
            outlen = len(output) - 1
            outidx = 0
            cposition = 0
            tempaddstr = ""
            x = 0
            y = 0

            for idx in range( (len(outtemp) - 1), -1 , -1):
              cposition = (outlen - outidx - outposition)
              x = int(outtemp[idx])
              y = int(output[cposition]) if ( cposition > -1 ) else 0
              tempadd = x + y + remainder
              remainder = 1 if (tempadd > 9) else 0
              tempaddstr = str(tempadd)[-1]  + tempaddstr
              outidx = outidx + 1

            tempaddstr = str(remainder) + tempaddstr if (remainder > 0) else tempaddstr
            output = tempaddstr + output[ (outlen - outposition + 1):(outlen + 1)] 
            outtemp = ""
            outposition = outposition + 1
        if ( oDecPos > 0 ):
          output = "0" + output if (len(output) < oDecPos) else 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
        
        
        if ( isaNeg != isbNeg  ):
            output = "-" + output
        
        return output
        


x = "-000000000000000000000000.99999999999999999999999999"
y = ".9999999999999999999999999900000000000000000000000000"
e = libZ()

print(e.infiX(x,y))  

Digiprove sealCopyright 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 – Decimal, Precise Float Calculation – Python".
Original Post Link: http://kevinhng86.iblog.website/2017/02/15/working-with-number-infinity-multiplication-decimal-precise-float-calculation-python/.

Advertisement


Random Article You May Like

16 thoughts on “Working With Number – Infinity Multiplication – Decimal, Precise Float Calculation – Python

  1. I’ve been surfing online more than 3 hours today, yet I never
    found any interesting article like yours. It’s pretty worth enough
    for me. In my view, if all site owners
    and bloggers mare good content as you did,
    the web would be a lot more useful than ever before.
    I enjoy what you guys are usually up to.
    This sort of clever work and reporting! Keep up the terrific works guys I’ve added you guy to my
    own blogroll. I really love your site.. Excellent colour theme.
    Did you create this website yourself? Please reply back as I’m attempting to create my very own blog and would like to learn where you got this
    theme from or just what the theme is called. Cheers!
    http://cspan.org/

    1. What subject are you interest in writing? Would you like to join my main blog of iblog.website. I am opening iblog.website for guest post, authors and contributor post to a variety of subject. If you are into programming I have another blog for programmer to post at. You are allow to have one link back to your site in your post. You are also allow to place one advertisement banner in your content.

  2. Sweet blog! I found it while surfing around on Yahoo
    News. Do you have any tips on how to get listed in Yahoo
    News? I’ve been trying for a while but I never seem to get there!
    Thank you

  3. Hi there! This is my 1st comment here so I just wanted to give a quick shout out and tell
    you I genuinely enjoy reading your articles. Can you suggest any other blogs/websites/forums that deal
    with the same topics? Thanks for your time!

  4. Attractive component of content. I simply stumbled upon your
    web site and in accession capital to claim that I acquire in fact loved account your blog posts.
    Anyway I will be subscribing on your augment and even I achievement you get right
    of entry to persistently quickly.

  5. It’s very easy to find out any matter on net as compared to
    textbooks, as I found this article at this web page.

  6. I’ll right away grasp your rss feed as I can not find your email subscription link or e-newsletter service.
    Do you have any? Please allow me recognize in order that I may subscribe.
    Thanks.

  7. It’s going to be finish of mine day, however before finish I am reading this wonderful post to improve my knowledge.

  8. We’re a group of volunteers and starting a new scheme in our community.
    Your website offered us with valuable information to
    work on. You’ve done an impressive job and our entire community will be
    grateful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*