[Pharo-project] digitAt: issue

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Mon May 2 17:31:36 CEST 2011


Yes, if you want ith decimalDigit of integer n, 1 being least
significant digit, then you shall write the division with a method
like:

decimalDigitOfRank: i
    "Answer the ith decimal digit, i=1 being the least significant digit"

    ^self // (10 raisedTo: i - 1) \\ 10

However, the awfull method you proposed might be faster if your
intention is to scan all digits on very large numbers, because
printString is optimized.

Nicolas

2011/5/2 Bernat Romagosa <tibabenfortlapalanca at gmail.com>:
> Ouch, understood!
> Then what would be the proper way to address a decimal digit in a number? I
> can only think of (bigNumber asString at: index) asNumber, which is...
> awful.
>
> 2011/5/2 Sven Van Caekenberghe <sven at beta9.be>
>>
>> Bernat,
>>
>> On 02 May 2011, at 11:13, Bernat Romagosa wrote:
>>
>> > Hi, try to run the following:
>> >
>> > (2 raisedTo: 100000) digitAt: 1
>> >
>> > The expected result (if I understood what digitAt: should return) is 9,
>> > but the message returns 0 instead. In fact, it returns 0 for any index.
>> >
>> > Is this a bug or am I missunderstanding how digitAt: should work?
>> >
>> > Cheers,
>> >
>> > Bernat Romagosa.
>> >
>> > p.s. My config is Pharo 1.2 with a 'Croquet Closure Cog VM
>> > [CoInterpreter VMMaker-oscog.51]' on Debian Lenny.
>>
>> Consider,
>>
>> 2 raisedTo: 32
>>
>> #digitAt: for digits 1 to 4 returns 0, 5 return 1.
>> From the comments you can see that the number is looked at in base 256.
>> The above number thus has 5 digits in this base, four are zero and the
>> highest one is one.
>> Furthermore, the first digit is the lowest one.
>> If you inspect the number it might become clearer.
>>
>> Any #digitAt: has to depend on the base you use to represent the number,
>> this one doesn't, so it seems to be useful only to return internal parts of
>> a number.
>>
>> HTH,
>>
>> Sven
>>
>>
>
>



More information about the Pharo-project mailing list