2

I think I understand the concept of how python is storing variables and why certain vars are larger than others. I also googled about float point but that couldn't answer my question:
Why is a float e.g. 0.5 is only taking 24 bytes of memory but a integer like 1 is taking 28? What even confuses me more is that a 0 takes 24 bytes too (That I understand. It stores just the object with "no" integer (I think...)). But how does it work that, when python adds 4 bytes if the number can't be saved with less, python can store a larger binary number like 0.5 in the same space like 0.

I used sys.getsizeof() to get the size of the objects in Python 3.9.1 64-bit

29thDay
  • 83
  • 1
  • 9
  • 1
    Long ints in python don't have a size limit. Perhaps the additional bytes come from this feature. – wyattis Jan 04 '21 at 13:18
  • this could help you https://stackoverflow.com/questions/40344159/understanding-memory-allocation-for-large-integers-in-python – ljuk Jan 04 '21 at 13:20
  • First: Thanks for the comments. Second: I still don't understand it. Python creates variables as objects. And a var with only a 0 in it, will be created as an object and the payload will be emtpy (I think). And since the object takes 24 bytes, the size of a variable with a 0 is 24 bytes. But if I take a float (e.g. 0.5) that the payload is not empty but the size is still 24 bytes. How? How can a empty payload have the same size as a not empty. And even when I add numbers to the float, that size remains the same. Why? – 29thDay Jan 04 '21 at 13:34
  • *Variables don't have sizes*. Objects do. But in any case, what do you mean by "payload"? – juanpa.arrivillaga Jan 04 '21 at 14:15
  • If you look at ltaljuk's comment, there if a answer where the writer (@bgusach) called the information payload. So to make it clear: with payload I mean "the thing to store". So if you take `a = 0` that "a" is the variable and 0 is the payload. The information given to the object. – 29thDay Jan 04 '21 at 14:35
  • 1
    Basically, a Python `float` is the standard PyObject "head" and a C double, so 16 bytes + 8 bytes = 24 bytes. A Python `int` objects has the standard "head", plus an 8bit integer telling you the size of the int, and an array of 32 bit numbers. For small integers, that's 16 bytes + 8 bytes + 4 bytes == 28 bytes – juanpa.arrivillaga Jan 04 '21 at 14:42
  • 1
    So, consider a larger integer, `sys.getsizeof(10**10)` and you'll get 32 bytes, because now the array is of size 2, so it requires an addtional 4 bytes. This can keep going until you run out of memory or address space – juanpa.arrivillaga Jan 04 '21 at 14:45
  • That explains the whole thing. Thank you for the help. Just to make sure I got it: a float does not have these extra 4 bytes. And if the variable is 0 than the object doesn't store these extra 4 bytes so it's also 24 bytes (?) – 29thDay Jan 04 '21 at 14:47
  • @AkiraK. um, well, it's actually just a different representations. `float` objecst basically just wrap a double. `int` objects require a 64bit integer (on 64bit systems) that gives you the *size* of the int, plus an array of 32-bit int's which represents the actual integer. I think 0 is just special-cased because and it simply lacks the array (and I believe the size will be 0). All of these are implementation details that can and do change. – juanpa.arrivillaga Jan 04 '21 at 14:50
  • So a float is just not using an array to store the number? Is it stored in the 8 bytes / 64 bits? (That's further than my question but I am just wondering...) – 29thDay Jan 04 '21 at 14:56
  • For `float` check out: https://github.com/python/cpython/blob/v3.9.1/Include/floatobject.h#L15-L18 for `int` https://github.com/python/cpython/blob/v3.9.1/Include/longintrepr.h#L85-L88 (and around)... which also shows what has been written above and where are the 4B of difference between `int` holding `1` and `float`. – Ondrej K. Jan 04 '21 at 14:58

0 Answers0