Upper limit?!

Neel Krishnaswami neelk at brick.cswv.com
Thu Sep 9 19:12:55 EDT 1999


On Tue, 07 Sep 1999 15:38:16 -0500, Warren 'The Howdy Man' Ockrassa 
<warren at nightwares.com> wrote:
>
>But back to the real world. If you explicitly declare your math
>operations as floats you can often bypass this limitation:
>
>  >>> import sys
>  >>> print sys.maxint
>  2147483647
>  >>> 2147483647 * 2.0
>  4294967294.0
>  >>> 2147483647 * 2147483647.0
>  4.61168601413e+018

Python has built-in support for arbitrary-sized integers, which are
nicer because floats will lose the low-order digits as they get big.
To make an integer long, stick an "L" at the end of it. Arithmetic
operations involving regular and long integers automatically yield
longs, so you can write a factorial function that will give the right
answer for any n like this:

  Python 1.5.2 (#8, May 12 1999, 17:46:31)  [GCC 2.7.2.1] on linux2
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> def fact(n):
  ...	result = 1L
  ...	for i in range(1,n+1):
  ...	    result = result * i
  ...	return result
  ... 
  >>> fact(10)
  3628800L
  >>> fact(100)
  933262154439441526816992388562667004907159682643816214685929638952175
  999932299156089414639761565182862536979208272237582511852109168640000
  00000000000000000000L


Neel




More information about the Python-list mailing list