26

I have a 100 digit number and I am trying to put all the digits of the number into a list, so that I can perform operations on them. To do this, I am using the following code:

for x in range (0, 1000):
   list[x] = number % 10
   number = number / 10

But the problem I am facing is that I am getting an overflow error something like too large number float/integer. I even tried using following alternative

number = int (number / 10)

How can I divide this huge number with the result back in integer type, that is no floats?

0

4 Answers 4

65

In Python 3, number / 10 will try to return a float. However, floating point values can't be of arbitrarily large size in Python and if number is large an OverflowError will be raised.

You can find the maximum that Python floating point values can take on your system using the sys module:

>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308

To get around this limitation, instead use // to get an integer back from the division of the two integers:

number // 10

This will return the int floor value of number / 10 (it does not produce a float). Unlike floats, int values can be as large as you need them to be in Python 3 (within memory limits).

You can now divide the large numbers. For instance, in Python 3:

>>> 2**3000 / 10
OverflowError: integer division result too large for a float

>>> 2**3000 // 10
123023192216111717693155881327...
5
  • 1
    But does this works on long int also? I suppose in case of a large number, python will automatically choose it as long it? Jan 14, 2015 at 15:41
  • 1
    In Python 3 there is no long type (integers can be any size). It works in Python 2 with both types.
    – Alex Riley
    Jan 14, 2015 at 15:42
  • Awesome. Didn't know about // division before.
    – Mudit Jain
    Jul 24, 2016 at 20:09
  • 2
    Careful. Just because you don't get an OverflowError doesn't mean the division is correct. stackoverflow.com/questions/26740938/…
    – Maxpm
    May 30, 2019 at 3:08
  • @Maxpm: that's true for true division (/) where you get back a float, but for integer a and b you can be sure that the integer division a // b is correct, regardless of the magnitude of the resulting integer.
    – Alex Riley
    May 30, 2019 at 19:33
7

If you have an integer and you want each digit in a list, you can use:

>>> map(int,list(str(number)))
[1, 5, 0, 3, 0, 0, 7, 6, 4, 2, 2, 6, 8, 3, 9, 7, 5, 0, 3, 6, 6, 4, 0, 5, 1, 2, 4, 3, 7, 8, 2, 5, 2, 4, 4, 5, 4, 8, 4, 0, 6, 6, 4, 5, 0, 9, 2, 4, 8, 9, 2, 9, 7, 8, 7, 3, 9, 9, 9, 7, 0, 1, 7, 4, 8, 2, 4, 4, 2, 9, 6, 9, 5, 1, 7, 1, 3, 4, 8, 5, 1, 3, 3, 1, 7, 9, 0, 1, 0, 1, 9, 3, 8, 4, 2, 0, 1, 9, 2, 9]

it transform the int into a string, then list will take each character of the string and put it in a list. Finally, map will convert each item of the list into an int again

2

Python will automatically handle large ints of arbitrary length. What it won't do is handle floats of arbitrary length so you need to make sure you're not getting floats along the way.

2
  • true. But, how can I avoid that in case I am dividing the number. Is there any function to handle this. Jan 14, 2015 at 15:38
  • yeah as previously mentioned, using the // operator will help you ensure your answer is always an int.
    – ZekeDroid
    Jan 14, 2015 at 15:46
1

Try int(number) % 10. You can only mod integers.

3
  • The number is already integer and it works. Problem happens in case of division. When I try to divide it by 10, i just want remaining integer. If i use int(number/10) i get error number too big for float. Jan 14, 2015 at 15:37
  • That's interesting because x mod 10 should be in the subset [0, 9]; thus, division by 10 would be 0 by integer division.
    – erip
    Jan 14, 2015 at 15:40
  • 1
    Oh wait, python's / isn't integer division. // is the way to go.
    – erip
    Jan 14, 2015 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.