Hướng dẫn python uint32 max

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.

In Python3, int has no max limit.

Python2 has two integer types, int and long, but Python3 has only int. int in Python3 is equivalent to long in Python2, and there is no max limit. You can handle as large value as memory is available.

This article describes the following contents.

  • int and long in Python2
  • int in Python3 has no max limit

See the following article for the maximum and minimum values of the floating-point number float.

  • Maximum and minimum float values in Python

Note that NumPy uses data types with a fixed number of bits, such as int32 (32-bit integer) and int64 (64-bit integer).

  • NumPy: Cast ndarray to a specific dtype with astype()

int and long in Python2

Python2 has two integer types, int and long.

  • 5.4 Numeric Types — int, float, long, complex — Python 2.7.18 documentation

You can get the max value of int with sys.maxint. The min value (the largest negative value) is -sys.maxint-1.

  • 28.1. sys.maxint — System-specific parameters and functions — Python 2.7.18 documentation

sys.maxint is at least 2**31-1, and on a 64-bit environment, it is 2**63-1.

long has no maximum and minimum limit.

int in Python3 has no max limit

int in Python3 corresponds to long in Python2, and there is no max and min limit.

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). What’s New In Python 3.0 — Python 3.8.4 documentation

In Python3, sys.maxint has been removed, and sys.maxsize has been added.

  • sys.maxsize — System-specific parameters and functions — Python 3.8.4 documentation

sys.maxsize is 2**31-1 on a 32-bit environment and 2**63-1 on a 64-bit environment, like sys.maxint in Python2.

import sys

print(sys.maxsize)
# 9223372036854775807

print(type(sys.maxsize))
# 

print(sys.maxsize == 2**63 - 1)
# True

Converted to binary and hexadecimal numbers with bin() and hex(), sys.maxsize is expressed as follows.

  • Convert binary, octal, decimal, and hexadecimal in Python
print(bin(sys.maxsize))
# 0b111111111111111111111111111111111111111111111111111111111111111

print(hex(sys.maxsize))
# 0x7fffffffffffffff

sys.maxsize is not the max value of int, and you can handle larger values as memory is available.

i = 10**100

print(i)
# 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

print(i > sys.maxsize)
# True

The floating-point number float has inf representing infinity. inf is judged to be larger than any value of int.

print(float('inf'))
# inf

print(i > float('inf'))
# False

See the following article about infinity inf.

  • "inf" for infinity in Python