Newbie questions

Raimo Niskanen raimo@REDACTED
Fri Mar 31 13:23:55 CEST 2006


svenolof@REDACTED (Sven-Olof Nystr|m) writes:

> 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).
> 

That seems to be some kind of 2-complement notion of bit length.
If you define the bit length of negative numbers to be the number
of bits from the right to the last non-1 as opposed to for 
positive numbers to the last non-0, you would get this.

> 
> Sven-Olof
> 

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list