首页 > 编程语言 >Maximum and Minimum values for ints Python

Maximum and Minimum values for ints Python

时间:2022-09-24 01:11:58浏览次数:76  
标签:digit Python value sys int Minimum values str

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? – Gilberto Albino Mar 28 at 11:07    

标签:digit,Python,value,sys,int,Minimum,values,str
From: https://www.cnblogs.com/chucklu/p/16724798.html

相关文章

  • What do these operators mean (** , ^ , %, //)? [closed] Python
    Whatdotheseoperatorsmean(**,^,%,//)?[closed] 回答1**:exponentiation  指数^:exclusive-or(bitwise) 异或%:modulus//:dividewithinte......
  • 学习笔记:python (2)
    python学习1.列表形式如下:names=['liuyufan','yebenhao','luyuhang']调用列表中的内容:names=['liuyufan','yebenhao','luyuhang']print=name[0]#输出li......
  • 学习笔记:python(1)
    python学习1.HelloWorldprint("HelloWorld!")注意与c语言不同输出函数为print而不是printf2.数据类型(1)字符串:可用“”表示!注意例:“asdhas”中“a”是第0个字符......
  • 入门Python,看完这篇就行了!
    转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.top/3bbc3146.html你好,我是测试蔡坨坨。众所周知,Python语法简洁、功能强大,通过简单的代码就能实现很多实用、有趣......
  • python入门第三课
    pycharm下载与使用JETBRAINS官网:https://www.jetbrains.com/pycharm/1.免费版功能太少(community)我们尽量使用收费版(professional)30天试用。选择版本就选一......
  • 这个Python 0day 已存在15年,已影响超过35万个开源项目
    这个Python0day已存在15年,已影响超过35万个开源项目https://mp.weixin.qq.com/s/-00LEYzwa9HFg3Oam7LJqw这个Python0day已存在15年,已影响超过35万个开源项目RavieL......
  • python注释、变量、数据类型详细
    1.python注释什么是注释?注释是对代码的解释说明,写注释是为了下次来看能更快的理解,抵抗遗忘。单行注释:文字之前加警号pycharm中有快捷键ctrl+?#这是单行注释......
  • Python实验报告——第4章 序列的应用
    实验报告【实验目的】 1.掌握python中序列及序列的常用操作。2.根据实际需要选择使用合适的序列类型。【实验条件】1.PC机或者远程编程环境。 【实验内容】1.......
  • 盘点一个Python网络爬虫实战问题
    大家好,我是皮皮。一、前言前几天在Python铂金交流群【红色基因代代传】问了一个Python网络爬虫的问题,提问截图如下:代码截图如下:报错截图如下:要么就是原始网页没那......
  • python基础操作
    pycharm下载+使用该软件有两种版本分别是收费版和免费版:免费版功能太少,尽量使用收费版​30天试用操作如下:2.免费试用该软件:​版本越新越难弄成......