I'm working with numbers with tens of thousands of digits in python. The long type works beautifully in performing math on these numbers, however I'm unable to access the highest digits of these numbers in a sufficiently fast way. Note that I don't know exactly how many digits the number contains. The "highest digits" refers to the digits in the most significant place, the lowest digits can be accessed quickly using modulus.
I can think of two ways to access these digits in python but they're both too slow for my purposes. I have tried converting to a string and accessing digits through array methods, however type conversions are slow when you have 10,000+ digits. Alternatively I could simply mask out bits and truncate, but this requires that I know how many digits are in the long. Finding the number of digits in the long would require a loop over a counter and a mask test, this will surely be slower than string conversion.
From the description here it seems that the long type does in fact contain a bignum array. Is there some way I can access the underlying data structure that stores the long, or possibly check how many digits the long has from the base type?
If people are interested I can provide an example with benchmarks.
10**(orderMag-1)
. An integer division will give you the most significant digit – Distrustful.bit_length()
(Python 2.7+) to get the number of bits without looping and then rightshift downward? – Charinile_PyLong_AsByteArray
it seems, github.com/schmir/python/blob/2.7/Include/longobject.h#L95, implementation here github.com/schmir/python/blob/2.7/Objects/longobject.c#L616 looks intriguing – Chadwick