2

While playing around with sys.getsizeof(), I noticed that it returns higher values for ints as their magnitude grows, signifying that they use more memory:

>>> sys.getsizeof(3)
28
>>> 

>>> sys.getsizeof(300000000000000000000000)
36

However with floats, I get the same memory usage regardless of magnitude:

>>> sys.getsizeof(3.0)
24

>>> sys.getsizeof(300000000000000.111111111)
24

>>> sys.getsizeof(300000000000000000000000.111111111)
24

>>> sys.getsizeof(300000000000000000000000.1111133333333331111)
24

According to the docs, I should be getting back the accurate memory usage of built-in types:

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific

gmds
  • 19,325
  • 4
  • 32
  • 58
Moondra
  • 4,399
  • 9
  • 46
  • 104

1 Answers1

2

The name float comes from the fact that they are floating-point numbers.

To simplify, floats are stored as a * b ** c, where a, b and c each take up a certain fixed number of bits respectively. Therefore, intuitively, if f is a float, f * 10 ** 10 will not take that much more space than f, since we only need to increase c by log_2 log_2 (10 ** 10) = 5 (normally, b is a constant, such as 2).

The flip side of this, of course, is that we can only ever represent a certain number of significant digits with floating-point numbers, which is why you run into rounding errors after a while.

In Python, ints are fixed-point, arbitrarily precise and variable width. That means that an int can grow as big as you want it to be, subject to memory limitations. The amount of space it takes is proportional to its magnitude, since it must store every single digit individually (more or less), rather than having an exponent-based calculation, as in floats. Therefore, multiplying an int i by 10 ** 10 will require log_2(10 ** 10) = 33 extra bits.

For completeness, I would note that in something like C, the corresponding int is, like float, fixed width, and therefore the equivalent sizeof will return the same value regardless of the magnitude of the int.

gmds
  • 19,325
  • 4
  • 32
  • 58
  • It's hard to tell where the cutoff point is, where `floats` will return significant rounding errors, but it seems if dealing with lots of numerical values that are somewhat large in Python, it's better to use float over int as you can save a lot of memory. – Moondra Apr 23 '19 at 05:27
  • It depends on your exact use case. Memory is so cheap nowadays that unless you are specifically in a situation where you *need* a (low-precision) floating point number, such as deep learning, I would just go for `int`. – gmds Apr 23 '19 at 05:29