Re: C function for returning number of digits?



In article <1133286782.453168.93740@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Ingo Menger <quetzalcotl@xxxxxxxxxxxxxx> wrote:

>Niklas Norrthon schrieb:

>> And how do you fix this when the next version of your compiler ships
>> (which use 333 bit longs)?

>I don't. I write the length of the char array as constant expression
>involving sizeof (long) in the first place. For example
> char digits[32 + 4 * sizeof (long)]
>That should do it.

pow(2,332)-1 is 100 digits. Your expression 32 + 4 * sizeof (long)
will not work unless sizeof(long) is at least 17 and CHAR_BIT is
at least 20.

You should be allocating about
CHAR_BITS * sizeof long / (log(10)/log(2))
digits... though remember to allow for rounding and the sign.

Getting sufficient precision on (log(10)/log(2)) at compile
time could be tricky, so possibly the easiest would be to round
that down and overeastimate the number of digits, resulting in

CHAR_BIT * sizeof long / 3
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
.