• Maximum and Minimum values for ints Python


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

    sys.int_info

    A named tuple that holds information about Python’s internal representation of integers. The attributes are read only.

    Attribute

    Explanation

    bits_per_digit

    number of bits held in each digit. Python integers are stored internally in base 2**int_info.bits_per_digit

    sizeof_digit

    size in bytes of the C type used to represent a digit

    default_max_str_digits

    default value for sys.get_int_max_str_digits() when it is not otherwise explicitly configured.

    str_digits_check_threshold

    minimum non-zero value for sys.set_int_max_str_digits(), PYTHONINTMAXSTRDIGITS, or -X int_max_str_digits.

    New in version 3.1.

    Changed in version 3.10.7: Added default_max_str_digits and str_digits_check_threshold.

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

    Maximum and Minimum values for ints

    How do I represent minimum and maximum values for integers in Python? In Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE.

    回答1

    Python 3

    In Python 3, this question doesn't apply. The plain int type is unbound.

    However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.

    Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.

    Python 2

    In Python 2, the maximum value for plain int values is available as sys.maxint:

    >>> sys.maxint
    9223372036854775807

    You can calculate the minimum value with -sys.maxint - 1 as shown here.

    Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.

    回答2

    The sys.maxint constant has been removed from Python 3.0 onward, instead use sys.maxsize.

    Integers

    • PEP 237: Essentially, long renamed to int. That is, there is only one built-in integral type, named int; but it behaves mostly like the old long type.
    • PEP 238: An expression like 1/2 returns a float. Use 1//2 to get the truncating behavior. (The latter syntax has existed for years, at least since Python 2.2.)
    • The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).
    • The repr() of a long integer doesn’t include the trailing L anymore, so code that unconditionally strips that character will chop off the last digit instead. (Use str() instead.)
    • Octal literals are no longer of the form 0720; use 0o720 instead.

    Refer : https://docs.python.org/3/whatsnew/3.0.html#integers

    回答3

    You may use 'inf' like this:

    import math
    bool_true = 0 < math.inf
    bool_false = 0 < -math.inf

    Refer: math — Mathematical functions

    Note that math.inf is equivalent to float('inf')
    – Georgy
    Sep 27, 2019 at 9:10
     
    The author questioned how to obtain the MAX and MIN int values. How does this answer relates to the question, since the results are True and False, not MAX and MIN? Mar 28 at 11:07
     
     
  • 相关阅读:
    数据结构-查找-有序查找
    发现新大陆 --21lic
    专利检索
    IT行业新闻事件
    流量校准仪开发日志-2017-10-24
    电池充电方案总结
    iOS中创建自定义的圆角按钮
    iOS 内存管理实践
    iOS 内存管理策略
    [置顶] 内存管理一点也不神秘————手绘iOS内存管理细节
  • 原文地址:https://www.cnblogs.com/chucklu/p/16724798.html
Copyright © 2020-2023  润新知