Colin
Colin

Reputation: 10820

Python number wrapping?

Consider this Python code:

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

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

Upvotes: 6

Views: 5192

Answers (4)

Zimm3r
Zimm3r

Reputation: 3425

Ok the answer to your question is generally no, however if you deal with large numbers you can have some problems, below are details on python's big numbers.

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

Please Note: This is on a 32 bit AMD machine (Though python says it is intel (is that referring to it being 32 bit??)

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

CPython's max number in its math module (underlying C lib) where otherwise it will overflow or return inf is 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 gotten by (probably other ways also)

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 math's use of an underlying C library http://docs.python.org/library/math.html

Specific to CPython The math module consists mostly of thin wrappers around the platform C math functions. Behavior in exceptional cases follows Annex F of the C99 standard where appropriate. The current implementation will raise ValueError for invalid operations like sqrt(-1.0) or log(0.0) (where C99 Annex F recommends signaling 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 now aims to follow C99 Annex F. In earlier versions of Python the behavior in special cases was loosely specified.loosely specified.

Python's maximum integer (type int) is defined by sys.maxint. The difference between the maximum integer and the maximum number is this

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

The maximum number is originally a float but when we try and convert it to a integer using int() it is automatically converted to type long because it is over sys.maxintloosely specified.

Upvotes: -1

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

Python will automatically promote integers to arbitrary precision. If a float becomes too large it will be inf. Thus, this would only fail if a and b are both integral and you run out of memory.

Upvotes: 3

dan04
dan04

Reputation: 91179

Depends on which version of Python you're using.

Prior to 2.2 or so, you could get an OverflowError.

Version 2.2-2.7 promote the sum to a long (arbitrary precision) if it's too large to fit in an int.

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

Upvotes: 9

Will McCutchen
Will McCutchen

Reputation: 13117

If a + b is is larger than the maximum integer value, the result will be a 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

Upvotes: 1

Related Questions