Maximum and Minimum values for ints

前端 未结 9 1898
北荒
北荒 2020-11-22 06:36

I am looking for minimum and maximum values for integers in python. For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. Is there som

相关标签:
9条回答
  • 2020-11-22 07:15

    If you want the max for array or list indices (equivalent to size_t in C/C++), you can use numpy:

    np.iinfo(np.intp).max
    

    This is same as sys.maxsize however advantage is that you don't need import sys just for this.

    If you want max for native int on the machine:

    np.iinfo(np.intc).max
    

    You can look at other available types in doc.

    For floats you can also use sys.float_info.max.

    0 讨论(0)
  • 2020-11-22 07:15

    I rely heavily on commands like this.

    python -c 'import sys; print(sys.maxsize)'
    

    Max int returned: 9223372036854775807

    For more references for 'sys' you should access

    https://docs.python.org/3/library/sys.html

    https://docs.python.org/3/library/sys.html#sys.maxsize

    0 讨论(0)
  • 2020-11-22 07:15

    sys.maxsize is not the actually the maximum integer value which is supported. You can double maxsize and multiply it by itself and it stays a valid and correct value.

    However, if you try sys.maxsize ** sys.maxsize, it will hang your machine for a significant amount of time. As many have pointed out, the byte and bit size does not seem to be relevant because it practically doesn't exist. I guess python just happily expands it's integers when it needs more memory space. So in general there is no limit.

    Now, if you're talking about packing or storing integers in a safe way where they can later be retrieved with integrity then of course that is relevant. I'm really not sure about packing but I know python's pickle module handles those things well. String representations obviously have no practical limit.

    So really, the bottom line is: what is your applications limit? What does it require for numeric data? Use that limit instead of python's fairly nonexistent integer limit.

    0 讨论(0)
提交回复
热议问题