[PYTHON] Precautions for integer rounding by casting

Introduction

When I tried to get a quotient by solving ABC131 C problem of AtCoder and casting it to int type, python integer division operation by value I wrote this article because the calculation result was different from the case of using the child.

Actual code

When trying to calculate the quotient of 103/10 Rounding by cast

print(int(103/10)) #The result is 10

The calculation of the quotient by the integer division operator

print(103//10) #The result is 10

In the above cases, the result is the same, but when the number of digits is 17 or more.

l = []
for i in range(10 ** 17, 10 ** 17 + 10):
    for j in range(10 ** 17, 10 ** 17 + 10):
        s = i // j
        s_c = int(i / j)
        if s != s_c:
            l.append(str(i) + ", " + str(j) + ": " + "The quotient" +
                     str(s) + ", The cast" + str(s_c))
for out in l:
    print(out)

Result is

100000000000000000, 100000000000000001:The quotient is 0, the cast is 1
100000000000000000, 100000000000000002:The quotient is 0, the cast is 1
100000000000000000, 100000000000000003:The quotient is 0, the cast is 1
100000000000000000, 100000000000000004:The quotient is 0, the cast is 1
100000000000000000, 100000000000000005:The quotient is 0, the cast is 1
100000000000000001, 100000000000000002:The quotient is 0, the cast is 1
100000000000000001, 100000000000000003:The quotient is 0, the cast is 1
100000000000000001, 100000000000000004:The quotient is 0, the cast is 1
100000000000000001, 100000000000000005:The quotient is 0, the cast is 1
100000000000000001, 100000000000000006:The quotient is 0, the cast is 1
100000000000000002, 100000000000000003:The quotient is 0, the cast is 1
100000000000000002, 100000000000000004:The quotient is 0, the cast is 1
100000000000000002, 100000000000000005:The quotient is 0, the cast is 1
100000000000000002, 100000000000000006:The quotient is 0, the cast is 1
100000000000000002, 100000000000000007:The quotient is 0, the cast is 1
100000000000000003, 100000000000000004:The quotient is 0, the cast is 1
100000000000000003, 100000000000000005:The quotient is 0, the cast is 1
100000000000000003, 100000000000000006:The quotient is 0, the cast is 1
100000000000000003, 100000000000000007:The quotient is 0, the cast is 1
100000000000000003, 100000000000000008:The quotient is 0, the cast is 1
100000000000000004, 100000000000000005:The quotient is 0, the cast is 1
100000000000000004, 100000000000000006:The quotient is 0, the cast is 1
100000000000000004, 100000000000000007:The quotient is 0, the cast is 1
100000000000000004, 100000000000000008:The quotient is 0, the cast is 1
100000000000000004, 100000000000000009:The quotient is 0, the cast is 1
100000000000000005, 100000000000000006:The quotient is 0, the cast is 1
100000000000000005, 100000000000000007:The quotient is 0, the cast is 1
100000000000000005, 100000000000000008:The quotient is 0, the cast is 1
100000000000000005, 100000000000000009:The quotient is 0, the cast is 1
100000000000000006, 100000000000000007:The quotient is 0, the cast is 1
100000000000000006, 100000000000000008:The quotient is 0, the cast is 1
100000000000000006, 100000000000000009:The quotient is 0, the cast is 1
100000000000000007, 100000000000000008:The quotient is 0, the cast is 1
100000000000000007, 100000000000000009:The quotient is 0, the cast is 1
100000000000000008, 100000000000000009:The quotient is 0, the cast is 1

And the result was different.

Finally

I don't have a chance to solve problems more than D problem with AtCoder (in terms of difficulty), so I don't handle numbers more than 10 ^ 17 so much, but from now on, when I don't need it, I round it by casting. I will avoid it.

Recommended Posts

Precautions for integer rounding by casting
Precautions for cv2.cvtcolor