Newbie questions
Sven-Olof Nystr|m
svenolof@REDACTED
Fri Mar 31 11:45:46 CEST 2006
Richard A. O'Keefe writes:
> Nick Linker <xlcr@REDACTED> wrote:
> I'm sorry, but I meant quite different question: given a big
> number N, how to compute the number of digits?
>
> There is obvious solution: length(integer_to_list(N)), but it
> is not very efficient. I wanted to have a bit faster
> solution... Ok, nevermind.
>
> 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)).
You seem to have your Lisps confused :-). Haulong is a function in
MacLisp; the corresponding operation in Common Lisp is
integer-length. Both functions give the length of the integer in bits.
> It's not completely clear what you want. Is the "number of digits"
> in -123456890123456890 twenty (literally the number of digits) or
> twenty-one (the number of characters in the minimal decimal representation)?
Curiously, both CL's integer-length and Java's bitLength() define the
length of negative numbers so that -1 has length 0 (same as the length
of 0, of course).
Sven-Olof
More information about the erlang-questions
mailing list