博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
整数的最大值和最小值
阅读量:2290 次
发布时间:2019-05-09

本文共 2815 字,大约阅读时间需要 9 分钟。

本文翻译自:

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楼

参考:


#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 : 请参阅: :


#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 sysmax = sys.maxsizemin = -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'的更多参考,您应该访问


#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 . 您可以在查看其他可用类型。

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

转载地址:http://bncnb.baihongyu.com/

你可能感兴趣的文章
Django运行方式及处理流程总结
查看>>
Android社区链接
查看>>
Android Booting
查看>>
Android4.0之init读取init.ic的过程
查看>>
uboot-2011.12移植到S3C2440(二)——点亮LED灯,the very beginning。
查看>>
LeetCode之Median Of Two Sorted Arrays
查看>>
java.util.Stack类简介
查看>>
求职信结尾处常用语
查看>>
申请原因常用语
查看>>
求职信开头处常用语
查看>>
英文求职信(五)
查看>>
英文求职信(四)
查看>>
英文求职信(三)
查看>>
英文求职信(二)
查看>>
UML在关系型数据库设计中的应用
查看>>
Hibernate为什么如此流行?
查看>>
rosdep init 或者rosdep update 连接错误的解决办法
查看>>
Gitlab 更新后gitlab-ctl reconfigure 提示 can only have one web server puma unicorn 错误
查看>>
videoc_streamon error 28, NO space left on device解决办法
查看>>
一个比make更好用的编译工具
查看>>