python的最大负值

python的最大负值,python,Python,我认为python最不利的方面是-maxint-1 我希望有-2,将使整数溢出 from sys import maxint maximum_int = maxint minimum_int = -maxint - 2 # 2147483647 # -2147483649 print maximum_int print minimum_int 然而。将显示正确的结果,并显示比-maxint-1更负的值 我可以知道原因吗?Python autopromotesint值溢出到long,除了可用内

我认为python最不利的方面是
-maxint-1

我希望有-2,将使整数溢出

from sys import maxint
maximum_int = maxint
minimum_int = -maxint - 2
#  2147483647
# -2147483649
print maximum_int
print minimum_int
然而。将显示正确的结果,并显示比
-maxint-1
更负的值


我可以知道原因吗?

Python autopromotes
int
值溢出到
long
,除了可用内存之外没有其他限制。

在Python中,
int
s将自动升级到
long
(bigint)。

Python实现了biging概念,类型被调用。大小实际上是无限的。

如果您确实想要python的最大负值,float('-inf')可以很好地工作。

在这里您可以看到结果被提升为长值

>>> from sys import maxint
>>> type(-maxint)
<type 'int'>
>>> type(-maxint-1)
<type 'int'>
>>> type(-maxint-2)
<type 'long'>
>>> 
来自sys import maxint的
>>
>>>类型(-maxint)
>>>类型(-maxint-1)
>>>类型(-maxint-2)
>>> 

请注意,有符号值的通常约定是负数多于正数,因此在本例中,
-2147483648
仍然是一个int

Python将
int
的溢出提升到仅受可用内存限制的任意长度精度

您可以使用以下代码查看促销:

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
在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
在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
对于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
"""



有趣的是,在我的系统上的Python2.7中,当您将maxint强制为float时,就会升级为long。似乎转换为浮点等于maxint+1。在我的sys上:
int(float(sys.maxint))==9223372036854775808L==sys.maxint+1
给出了
True
。对于浮点转换o-maxint:
int(float(-sys.maxint))==-9223372036854775808
int(float(-sys.maxint))=-sys.maxint-1
都是
True
。因此,在将maxint强制为float时要小心@霍布斯。这是由于浮点精度有限(约15位小数)。