long number multiplication

Christopher A. Craig com-nospam at ccraig.org
Mon Dec 6 10:40:34 EST 2004


"I.V. Aprameya Rao" <aprameya at students.iiit.net> writes:

> i have been wondering, how does python store its very long integers and 
> perform aritmetic on it.

In CPython, A long integer is a combination of a signed word
indicating the sign and the size and an unsigned array of N words
where N is the absolute value of the size which contain the value of
the integer in base 32768.

Most operations are done in their simplest fashion (see Knuth's
SemiNumerical Algorithms chapter 4.3) with the exception of multiply,
which uses Karatsuba math for inputs greater than 35 base 15 bit
digits.

> i needed to implement this myself and was thinking of storing the digits 
> of an integer in a list.

That's sort of what Python does except the "digits" are 15 bits,
not base 10.  Doing it in base 10 would be a huge pain because of the
problems with base 10->base 2 conversion.

-- 
Christopher A. Craig <com-nospam at ccraig.org>
"When all else fails, read the instructions."




More information about the Python-list mailing list