Re: C function for returning number of digits?
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Tue, 29 Nov 2005 21:07:30 +0000 (UTC)
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
.
- References:
- C function for returning number of digits?
- From: Luke Wu
- Re: C function for returning number of digits?
- From: Ingo Menger
- Re: C function for returning number of digits?
- From: Niklas Norrthon
- Re: C function for returning number of digits?
- From: Ingo Menger
- C function for returning number of digits?
- Prev by Date: Re: How to redefine arithmetic operators.
- Next by Date: Re: C function for returning number of digits?
- Previous by thread: Re: C function for returning number of digits?
- Next by thread: Re: C function for returning number of digits?
- Index(es):