Newbie questions

Nick Linker <>
Sat Apr 1 10:09:56 CEST 2006

Richard A. O'Keefe wrote:

>I'm aware of (and have used) several programming languages providing
>as-long-as-necessary integer arithmetic.  Without consulting manuals
>(everything is being moved out of my office so that new carpet can
>be laid) it's hard to be sure, but from memory, NONE of them provides
>an operation "how many decimal digits in this bignum".  Common Lisp
>has HAULONG, which tells you about how many BITS, and Java's
>BigInteger class offers bitLength(), which again tells you how many
>BITS.  In Smalltalk, the obvious thing to do would be n printString size,
>and in Scheme it would be (string-length (number->string n)).
Hm, for example Maple has "ilog10" function, that instantly gets the 
(Although Mapple is specialized language.)

>The question remains, WHY?  For what kind of problem is it useful to know
>the number of decimal digits but NOT what the digits actually are?
I had an idea to implement numeric mutable arrays as bignums. (Btw I 
know about ets, dictionary and ports).

>I benchmarked the following code against length(integer_to_list(N)),
>for the first however many factorials.  The two seemed to be about the
>same speed.  Your mileage will of course vary.
>integer_digits(N) when integer(N) ->
>    if N >= 0 -> natural_digits_loop(N, 0)
>     ; N <  0 -> natural_digits_loop(-N, 1)
>    end.
>natural_digits(N) when integer(N), N >= 0 ->
>    natural_digits_loop(N, 0).
>natural_digits_loop(N, D) when N >= 10000 ->
>    natural_digits_loop(N div 10000, D + 4);
>natural_digits_loop(N, D) when N >= 1000 ->
>    D + 4;
>natural_digits_loop(N, D) when N >= 100 ->
>    D + 3;
>natural_digits_loop(N, D) when N >= 10 ->
>    D + 2;
>natural_digits_loop(_, D) ->
>    D + 1.
Interesting idea - to move by 4 not by 1. I tried to do like that except 
I moved by 1 each step, and it wasn't better than 

Your answers are always comprehensive and very useful. Thank you, Richard.

Best regards,
Linker Nick.

More information about the erlang-questions mailing list