long number multiplication

Nick Coghlan ncoghlan at email.com
Sun Dec 5 01:27:23 EST 2004


I.V. Aprameya Rao wrote:
> hi
> 
> i have been wondering, how does python store its very long integers and 
> perform aritmetic on it.
> 
> i needed to implement this myself and was thinking of storing the digits 
> of an integer in a list.
> 
> however this would be very slow for operations like division etc.
> 
> so if anyone can point me to some links or some method on how to do this i 
> would appreciate it
> 
> aprameya rao
> 

I don't know of any design docs, but the source itself is easily available:
http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Objects/longobject.c?rev=1.165&view=markup

Look at Python 2.4's decimal.py for something which is still pretty slow (heavy 
number crunching in Python code just ain't pretty), but alleviates that somewhat 
by using long objects to do the grunt work in most of the numerical operations.

(Random aside: actually having Decimal store long objects instead of a list of 
digits slows it down. The arithmetic is faster, but the exponentiation + 
division required for rounding is significantly slower than the list slicing 
that the current implementation is able to use).

Cheers,
Nick.



More information about the Python-list mailing list