Janis Lesinskis' Blog

Assorted ramblings

  • All entries
  • About me
  • Projects
  • Economics
  • Misc
  • Software-engineering
  • Sports

How wide are the underlying data types for Python integers


I got asked this question just before:

Hey you know using the int() function I python, do you know how it decides what kind of int it picks? As in int32 or int64

So the interesting part about Python is that all integer objects since Python3 are arbitrary precision long integers. In Python 2 you had a distinction between fixed width integers int and arbitrary precision integers long.

$ python2
Python 2.7.17 (default, Feb 27 2021, 15:10:58) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**8)
<type 'int'>
>>> type(2**100)
<type 'long'>
>>> 
$ python3
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**8)
<class 'int'>
>>> type(2**100)
<class 'int'>

As you can see in Python3 from the perspective of the programming logic in pure python code you don't have to worry about how many bits are contained in an integer. However the sizes of these will vary:

>>> import sys
>>> sys.getsizeof(2**8)
28
>>> sys.getsizeof(2**100)
40
>>> sys.getsizeof(2**1000)
160

If you are working with something like Pandas or some other low level array library that deals with data of fixed widths you'd want to know what the underlying integer widths were when optimizing your data structures. On my machine 2**100 might not fit in a fixed width integer type whereas 2**8 likely would. There's a convenient way to get this information out:

>>> import sys
>>> sys.int_info
sys.int_info(bits_per_digit=30, sizeof_digit=4)

As you can see on this version of Python the integers are 4 bytes in the underlying C type as per sizeof_digit. The bits available per digit before it switches over to the arbitrary length integers is 30 bits. Note that this is less than the 32 bits.

There's a similar information for floats in sys.float_info

Published: Thu 11 March 2021
By Janis Lesinskis
In Software-engineering
Tags: python integers

links

  • JaggedVerge

social

  • My GitHub page
  • LinkedIn

Proudly powered by Pelican, which takes great advantage of Python.