Python number pack?

Consider this Python code:

assert(a > 0) assert(b > 0) assert(a + b > 0) 

Can the third statement ever fail? In C / C ++, it can, if the sum overflows the maximum integer value. How is this handled in Python?

+6
python
source share
4 answers

Depends on which version of Python you are using.

Up to 2.2 or so you can get an OverflowError .

Version 2.2-2.7 advertises a sum up to long (arbitrary precision) if it is too large to fit int .

3.0+ has only one integer type, which is arbitrary precision.

+9
source share

Python will automatically advance integers to arbitrary precision. If the float gets too big, it will inf . This way it will only fail if a and b are integral and you run out of memory.

+3
source share

If a + b greater than the maximum value of the integers, the result will be long:

 >>> import sys >>> sys.maxint 9223372036854775807 >>> a = sys.maxint >>> b = 1 >>> a + b 9223372036854775808L # A long >>> assert a > 0 >>> assert b > 0 >>> assert a + b > 0 
+1
source share

Well, the answer to your question is usually not, however, if you are dealing with large numbers, you may have some problems, the details of python large numbers are given below.

Also see this post for information on inf (infinity) NaN (not a number (i.e. infinity / infinity = NaN))

Please note: this is on a 32bit AMD machine (although python says it is Intel (does that mean it is 32bit?)

Python 2.6.2 (r262: 71605, April 14, 2009, 10:40:02 PM) [MSC v.1500 32 bit (Intel)] on win32

The maximum number of CPython in its math module (below C lib), where otherwise it will overflow or return inf - 8.2184074615549724e + 309

 >>> x = 8.2184074615549724e+309 >>> x 8.2184074615549724e+309 >>> x + 1 >>> x inf >>> x = exp(710) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: math range error 

The maximum number (python can represent) is 1.7976931348623157e + 308 and can be obtained (possibly in other ways)

http://docs.scipy.org/doc/numpy/reference/generated/numpy.finfo.html

 >>> import numpy >>> f = numpy.finfo(float() >>> f.max 1.7976931348623157e+308 >>> m = f.max >>> m1 = m + 100 # supposedly increase the number by 100 >>> m 1.7976931348623157e+308 >>> m1 1.7976931348623157e+308 >>> # note how m1 and m are the same number >>> m == m1 True >>> 

I believe (but don't know) that this is due to the mathematical use of the C base library http://docs.python.org/library/math.html

Specifically for CPython The mathematical module consists mainly of thin wrappers around the mathematical function of platform C. Behavior in exceptional cases follows Appendix F of standard C99, where appropriate. An electric current implementation will raise a ValueError for invalid operations such as sqrt (-1.0) or log (0,0) (where C99 Appendix F recommends signaling an invalid operation or divide by zero) and OverflowError for results that overflow (for example, exp ( 1000,0))

Changed in version 2.6: Behavior in special cases is now aimed at following C99 Appendix F. In earlier versions of Python, behavior in special cases was poorly specified. It is obvious.

The maximum Python integer (type int) is determined by sys.maxint. The difference between the maximum integer and the maximum number is

 >>> type(x) <type 'float'> >>> int_x = int(x) >>> type(int_x) <type 'long'> >>> 

The maximum number is initially a float, but when we try to convert it to an integer using the int () function, it is automatically converted to type long because it exceeds sys.maxintloosely.

-one
source share

All Articles