Python | accuracy in multiplication and division (multiplication division)

Today, I'll dig into the details and record the accuracy of Python in multiplication and division

1. Division calculation

1.1 division in general

dived = 20
div = 7
r = dived / div
print(r)

# Operation result
# result = 2.857142857142857

1.2 obtain the precision division of 100 digits after the decimal point (only integer operation is supported)

According to the above figure, in general, when calculating the integer division, the result should be an infinite circular decimal, but when the decimal point is in the 16th place, the system retains the result by rounding by default. In a sense, this is a way to save system resources. However, if you want to continue to calculate and solve some high-precision problems, you need to continue to operate downward, What should I do?

The answer is relatively simple. It is divided into two steps. First, obtain the integer by division, and then obtain the decimal by the combination of remainder and cycle, and then combine the two.

## Division accuracy problem (only integer is supported)
## Idea: for divisional calculation, first calculate the integer part, and then calculate the decimal part
dived = 20
div = 7
r = ''
if dived > div:
    t = dived // div;
    r = str(int(t))+'.'
    dived = dived % div
else:
    r = '0.'

for i in range(100):
    dived = dived * 10
    t = dived // div
    r = r + str(int(t))
    dived = dived % div

print(r)


# result
# result = 2.8571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571

The results are verified by scientific calculator.

1.3 obtain the precision division of 100 digits after the decimal point (support integer & floating point number)

To solve the problem of division accuracy of floating-point numbers, it is not difficult to think about it. You only need to convert the divisor and the dividend into integers. The idea here is to judge the maximum value (multiple power of 10) required to move the decimal point right to the integer, and then multiply the two numbers by this maximum value (multiple power of 10) respectively before integer operation.

dived = 202  ## Define divisor
div = 71  ## Define divisor
print("The divisor is:", dived)
print("The divisor is:", div, "\n")

r = ''
## If floating-point type exists, convert to integer first
if type(dived) == float or type(div) == float:
    print("Floating point number present")
    len_dd = len(str(dived).split(".")[1])
    len_d = len(str(div).split(".")[1])
    print("dived Decimal places of:", len_dd)
    print("div Decimal places of:", len_d)
    if len_dd > len_d:
        multiple = len_dd
    else:
        multiple = len_d
    dived = int(10 ** multiple * dived)
    div = int(10 ** multiple * div)
    print("dived Convert to integer:", dived)
    print("div Convert to integer:", div)
else:
    print("Floating point number does not exist")

print("\n Positive start")
## Positive start
if dived > div:
    t = dived // div;
    r = str(int(t))+'.'
    dived = dived % div
else:
    r = '0.'

for i in range(100):
    dived = dived * 10
    t = dived // div
    r = r + str(int(t))
    dived = dived % div

print(r)

It is verified by comparing the two calculation results with the results of scientific calculator.


2. Multiplication calculation

2.1 multiplication in general

Generally, there is no problem with the accuracy of the multiplication of two integers, and there is an accuracy problem in the multiplication of decimals, which needs to be solved by parts.

m1 = 0.080189
m2 = 0.0088035
r1 = m1 * m2
print("r1:", r1)

m3 = 0.080189
m4 = 0.00088035
r2 = m3 * m4
print("r2:", r2)

m5 = 801890000000
m6 = 880350000000
r3 = m5 * m6
print("r3:", r3)

2.1 multiplication in general

2.2 solve the problem of multiplication accuracy

def multiple(m1, m2):
    r = ''
    ## If floating-point type exists, convert to integer first
    if type(m1) == float or type(m2) == float:
        print("Floating point number present")
        len_m1 = len(str(m1).split(".")[1])
        len_m2 = len(str(m2).split(".")[1])
        print("m1 Decimal places of:", len_m1)
        print("m2 Decimal places of:", len_m2)
        
        m1 = int(10 ** len_m1 * m1)
        m2 = int(10 ** len_m2 * m2)
        print("m1 Convert to integer:", m1)
        print("m2 Convert to integer:", m2)
        r = str(m1 * m2)
        print("r: ", r)

        l = len_m1 + len_m2
        print("l Total length of:", l)
        if l < len(r):
            r_front = r[:-l]
            r_last = r[-l:]
            print(r_front, "-", r_last)
            r = r_front + "." + r_last
        else:
            r = "0." + (l - len(r)) * "0" + r

        
    else:
        print("Floating point number does not exist")
        r = m1 * m2

    print("The result is:", r)

m1 = 1.111
m2 = 1.1
r1 = m1 * m2
print("r1:", r1)	# Normal calculation
multiple(m1, m2)	# Calculation after solving the problem of accuracy

Check the result. r1 is the calculation result under normal conditions (r1: 1.2221000000000002), and the result after solving the accuracy problem (1.2221)

Try several other situations

m1m2General results rResult after solving the accuracy problem, multiple(m1,m2)
1.1111.11.22210000000000021.2221
0.0801890.000880357.059438614999999e-050.00007059438615
801898803570594386157059438615

Tags: Python

Posted on Tue, 19 Oct 2021 23:14:12 -0400 by Evoke