Sukruth
Sukruth

Reputation: 1

Python handling long numbers

I am trying to convert a number to list, but the last few digits don't match. Can someone explain why this is happening.

num = 6145390195186705544
lista = []
while num !=0:

    lista.append(num%10)
    num = int(num/10)

print(lista[::-1])

[6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 6, 6, 2, 4, 6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 6, 6, 2, 4, 6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 6, 6, 2, 4, 6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 6, 6, 2, 4]

I am using python3

Upvotes: 0

Views: 73

Answers (1)

Bekhruz Rakhmonov
Bekhruz Rakhmonov

Reputation: 21

[PEP 238] says: The current division (/) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.

This should work:

num = 6145390195186705544
lista = []
while num != 0:
    lista.append(num % 10)
    num = num // 10

print(lista[::-1])

Upvotes: 1

Related Questions