Maximum and Minimum values for ints

前端 未结 9 1869
北荒
北荒 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 06:55

    For Python 3, it is

    import sys
    max = sys.maxsize
    min = -sys.maxsize - 1
    
    0 讨论(0)
  • 2020-11-22 06:56

    If you just need a number that's bigger than all others, you can use

    float('inf')
    

    in similar fashion, a number smaller than all others:

    float('-inf')
    

    This works in both python 2 and 3.

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

    You may use 'inf' like this:

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

    Refer: math — Mathematical functions

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

    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

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

    Python 3

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

    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.

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

    In Python integers will automatically switch from a fixed-size int representation into a variable width long representation once you pass the value sys.maxint, which is either 231 - 1 or 263 - 1 depending on your platform. Notice the L that gets appended here:

    >>> 9223372036854775807
    9223372036854775807
    >>> 9223372036854775808
    9223372036854775808L
    

    From the Python manual:

    Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including binary, hex, and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer, in which case they yield a long integer. Integer literals with an 'L' or 'l' suffix yield long integers ('L' is preferred because 1l looks too much like eleven!).

    Python tries very hard to pretend its integers are mathematical integers and are unbounded. It can, for instance, calculate a googol with ease:

    >>> 10**100
    10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
    
    0 讨论(0)
提交回复
热议问题