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
 
 
作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-09-24 Anti forgery token is meant for user "" but the current user is "username"
2021-09-24 Asp.net Mvc AntiForgeryToken全局处理
2021-09-24 Does it make sense to put antiforgerytoken in _Layout.cshtml?
2021-09-24 jQuery .val()
2021-09-24 Understanding multiple anti-forgery tokens in ASP.NET MVC
2021-09-24 Duplicate Files Search & Link
2021-09-24 The source contains no DataRows
点击右上角即可分享
微信分享提示