On 30 Jun 99, at 0:44, Ken Kriesel wrote:
> >Here's a question that needs to be addressed: how to go from digits to
> >exponents, and exponent to digits.
>
> By all means include it in the faq; there's been too much repetition,
> including wrong responses, now and in past months.
Actually, things are a great deal easier than Ken makes out.
Notation used, square brackets means the "integer part of". I don't
like the notation "int(x)" since this has different interpretations
in different languages - does it mean chop to zero, chop down or
round to nearest or even? "ceil(x)" and "floor(x)" are precise, but
meaningless to C non-programmers.
Let k be the logarithm of r to base 2. Note that 2^p has [p/k] + 1
digits when represented in base r. k can be calculated as
log(r)/log(2) using logarithms to any arbitary base.
(a) If k is not a integer (i.e. r is not an integer power of 2),
2^p-1 has the same number of digits as 2^p, because there can be no
integers a, b such that a^b = 2^p unless a is an integer power of 2.
(b) If k is an integer (i.e. r is an integral power of 2), subtract 1
from the number of digits in 2^p, provided that k divides p.
The reverse algorithm (getting the smallest exponent with n digits)
is actually even easier:
p = [(n-1)*k] + 1 is the smallest p such that 2^p-1 has n digits when
represented in base r. (This works only for n > 1 because
2^0-1 = 0 still has 1 digit).
Note, the above is actually true irrespective of the numerical value
of the symbol "2" - i.e. replace "2" by "3" in the arguments above,
and they still work!
Regards
Brian Beesley
________________________________________________________________
Unsubscribe & list info -- http://www.scruz.net/~luke/signup.htm