[Tutor] ints, and long ints?

Kalle Svensson kalle@lysator.liu.se
Wed, 16 Oct 2002 09:49:01 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Thomi Richards]
> what happens when you store a number in an integer, which is larger
> than the maximum numbr able to be stored in an integer??

It depends on which version of Python you're using.

Python 2.1.3 (#1, Sep  7 2002, 15:29:56)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import sys
>>> i = sys.maxint
>>> i += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: integer addition
>>> i
2147483647
>>> l = long(i)
>>> l
2147483647L
>>> l += 1
>>> l
2147483648L

But note that in Python 2.2 overflow automatically creates longs:

Python 2.2.1 (#1, Sep  7 2002, 14:34:30) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> i = sys.maxint
>>> i += 1
>>> i
2147483648L

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9rRnmdNeA1787sd0RAhswAKCsVjqx18rOCPbQDwHGknYYJQy5RQCeKkAZ
KeoKkp5ghOlgWbxgzWZ/zGA=
=Dz9N
-----END PGP SIGNATURE-----