Martin Albrecht <martinralbre...@googlemail.com> writes:
> On Thursday 24 May 2012, Oleksandr Kazymyrov wrote:
>> Dear all,
>> 
>> In manual "ZZ ?" you can find:
>> 
>> As an inverse to "digits()", lists of digits are accepted, provided
>>        that you give a base. The lists are interpreted in little-endian
>>        order, so that entry "i" of the list is the coefficient of
>>        "base^i":
>> 
>>           sage: Z([3, 7], 10)
>>           73
>>           sage: Z([3, 7], 9)
>>           66
>>           sage: Z([], 10)
>>           0
>> 
>> But for base more than 2^64 it doesn't work. It looks stupid, because you
>> can call "digits(2^64)", but not an inverse function:
>> sage: a=ZZ(randint(0,2^128-1)).digits(2^64)
>> sage: a
>> [1154963902035838039, 8176620537326016718]
>> sage: ZZ(a,2^64)
>> ERROR: An unexpected error occurred while tokenizing input
>> The following traceback may be corrupted or invalid
>> The error message is: ('EOF in multi-line statement', (1348, 0))
>> 
>> ---------------------------------------------------------------------------
>> OverflowError                             Traceback (most recent call last)
>> ....
>> OverflowError: long int too large to convert
>> 
>> Of course it is possible to convert it back using Sage:
>> sage: a=ZZ(randint(0,2^128-1))
>> sage: a
>> 201636464310824733716520014075404236490
>> sage: b=a.digits(2^64)
>> sage: b
>> [6699442741605840586, 10930734632904602844]
>> sage: sum([b[i]*(2^64)^i for i in xrange(len(b))]) == a
>> True
>> 
>> Why core library doesn't include this functionality? Are there any other
>> ways to do the same as described above using another function?
>
> It's easy: implement it and send us a patch ;) On a more serious note, it 
> seems you found a bug and your code above is the right fix. So it would be 
> great if you could open a ticket and provide a patch which falls back to the 
> generic code above if the base is >= 2^64.

Well, not really - IMO, the correct fix is what David Roe already
implemented at http://trac.sagemath.org/sage_trac/ticket/13000 . There's
already code in sage/rings/integer.pyx to handle large bases. The bug is
that the input is cast to unsigned int in all cases, instead of just in
the case that it is detected as being possible to do so. The patch fixes
it without resorting to fallbacks into generic Python, and actually
fixes the real bug.

-Keshav

----
Join us in #sagemath on irc.freenode.net !

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to