整数的最大值和最小值

本文翻译自:Maximum and Minimum values for ints

I am looking for minimum and maximum values for integers in python. 我在python中寻找整数的最小值和最大值。 For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE . 例如,在Java中,我们有Integer.MIN_VALUEInteger.MAX_VALUE Is there something like this in python? 在python中有这样的东西吗?


#1楼

参考:https://stackoom.com/question/VuOk/整数的最大值和最小值


#2楼

sys.maxint constant has been removed from Python 3.0 onward, instead use sys.maxsize sys.maxint常量已从Python 3.0开始删除,而是使用sys.maxsize

Integers 整型

  • PEP 237: Essentially, long renamed to int. PEP 237:基本上,长期重命名为int。 That is, there is only one built-in integral type, named int; 也就是说,只有一个内置的整数类型,名为int; but it behaves mostly like the old long type. 但它的行为大多像旧的长型。
  • PEP 238: An expression like 1/2 returns a float. PEP 238:像1/2这样的表达式返回一个浮点数。 Use 1//2 to get the truncating behavior. 使用1 // 2来获取截断行为。 (The latter syntax has existed for years, at least since Python 2.2.) (后一种语法已存在多年,至少从Python 2.2开始。)
  • The sys.maxint constant was removed, since there is no longer a limit to the value of integers. 删除了sys.maxint常量,因为不再对整数值进行限制。 However, sys.maxsize can be used as an integer larger than any practical list or string index. 但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。 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). 它符合实现的“自然”整数大小,并且通常与同一平台上以前版本中的sys.maxint相同(假设具有相同的构建选项)。
  • 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. 长整数的repr()不再包含尾随L,因此无条件地剥离该字符的代码将切掉最后一位数字。 (Use str() instead.) (改用str()。)
  • Octal literals are no longer of the form 0720; 八进制文字不再是0720的形式; use 0o720 instead. 请改用0o720。

Refer : https://docs.python.org/3/whatsnew/3.0.html 请参阅: https//docs.python.org/3/whatsnew/3.0.html


#3楼

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. 这适用于python 2和3。


#4楼

For Python 3, it is 对于Python 3,它是

import sys
max = sys.maxsize
min = -sys.maxsize -1

#5楼

I rely heavily on commands like this. 我非常依赖这样的命令。

python -c 'import sys; print(sys.maxsize)'

Max int returned: 9223372036854775807 Max int返回: 9223372036854775807

For more references for 'sys' you should access 有关'sys'的更多参考,您应该访问

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

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


#6楼

If you want the max for array or list indices (equivalent to size_t in C/C++), you can use numpy: 如果你想要数组或列表索引的最大值(相当于C / C ++中的size_t ),你可以使用numpy:

np.iinfo(np.intp).max

This is same as sys.maxsize however advantage is that you don't need import sys just for this. 这与sys.maxsize相同,但是优点是您不需要为此导入sys。

If you want max for native int on the machine: 如果你想在机器上使用max for native int:

np.iinfo(np.intc).max

You can look at other available types in doc . 您可以在doc中查看其他可用类型。

For floats you can also use sys.float_info.max . 对于浮点数,您还可以使用sys.float_info.max

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值