Home > Software design >  Python handling long numbers
Python handling long numbers

Time:10-05

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)
    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

CodePudding user response:

as I see , you're trying to get the numbers as list ,try this:

 
num = 6145390195186705544
lista = []
num=str(num)

for number in num:
    lista.append(int(number))
print(lista)

CodePudding user response:

[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])
  • Related