python的最大负值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4241832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
most negative value for python
提问by Cheok Yan Cheng
I expect the most negative for python is -maxint-1
我预计对 python 最不利的是 -maxint-1
I expect having -2, will make integer overflow.
我希望有 -2,会使整数溢出。
from sys import maxint
maximum_int = maxint
minimum_int = -maxint - 2
# 2147483647
# -2147483649
print maximum_int
print minimum_int
Yet. Correct result is displayed, and a value which is more negative than -maxint-1is shown.
然而。显示正确结果,并且显示比显示的值更负的值-maxint-1。
May I know why?
我可以知道为什么吗?
采纳答案by John La Rooy
Here you can see the result is promoted to a long
在这里你可以看到结果被提升为一个长
>>> from sys import maxint
>>> type(-maxint)
<type 'int'>
>>> type(-maxint-1)
<type 'int'>
>>> type(-maxint-2)
<type 'long'>
>>>
note that the usual convention for signed values is to have one more negative number than positive, so in this case -2147483648is still an int
请注意,有符号值的通常约定是负数比正数多一个,因此在这种情况下-2147483648仍然是 int
回答by Ignacio Vazquez-Abrams
Python autopromotes intvalues that overflow to long, which does not have a limit other than available memory.
Python 自动提升int溢出到 的值,long除了可用内存之外没有限制。
回答by J-16 SDiZ
In Python, ints will auto-promote to long(bigint).
在 Python 中,ints 将自动提升到long(bigint)。
回答by Kugel
回答by Coquelicot
If you actually want the most negative value for python, float('-inf') works nicely.
如果你真的想要 python 的最大负值, float('-inf') 效果很好。
回答by the wolf
Python promotes an overflow of intto an arbitrary precision long which is limited only by available memory.
Python 将溢出提升为int任意精度 long,仅受可用内存限制。
You can see the promotion with this code:
您可以使用此代码查看促销信息:
import struct
from sys import maxint
maximum_int = maxint
minimum_int = -maxint-1
big_minus = -maxint-(maxint*maxint)
big_plus=maxint*maxint*maxint
def platform():
return struct.calcsize("P") * 8
def bit_length(x):
s=bin(x)
s=s.lstrip('-0b')
return len(s)
print
print 'running in ', platform(), ' bit mode...'
print 'maxint: ', maximum_int, ' bits: ', bit_length(maximum_int)
print 'minint: ', minimum_int, ' bits: ', bit_length(minimum_int)
print 'a big minus: ', big_minus, ' bits: ', bit_length(big_minus)
print 'big_plus: ', big_plus, ' bits: ', bit_length(big_plus)
print
Running under 32 bit Python, here is the return:
在 32 位 Python 下运行,这是返回:
running in 32 bit mode...
maxint: 2147483647 bits: 31
minint: -2147483648 bits: 32
a big minus: -4611686016279904256 bits: 62
big_plus: 9903520300447984150353281023 bits: 93
Under 64 bit Python:
在 64 位 Python 下:
running in 64 bit mode...
maxint: 9223372036854775807 bits: 63
minint: -9223372036854775808 bits: 64
a big minus: -85070591730234615856620279821087277056 bits: 126
big_plus: 784637716923335095224261902710254454442933591094742482943 bits: 189
回答by karthik r
for Python 3,
对于 Python 3,
import sys
""" max negative number """
print(sys.maxsize * -1)
"""through python's internal casts from int to long"""
print(sys.maxsize * -1 + 1)
"""
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).
[1]: https://docs.python.org/3.1/whatsnew/3.0.html#integers
"""

