On 09/20/21 18:39, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 06:25:34PM +0200, Laszlo Ersek wrote:
> On 09/20/21 13:04, Richard W.M. Jones wrote:
>> +/* If you allocate a buffer of at least this length in bytes
and pass
>> + * it as the first parameter to human_size, then it will not overrun.
>> + */
>> +#define HUMAN_SIZE_LONGEST 64
>
> (5) The integer constant expression
>
> ((sizeof (uint64_t) * 8 + 2) / 3 + 1)
>
> would be more frugal (but we might not care).
>
> If we take the number of three-bit groups in the word, and divide that
> by three -- rounded up --, we get the number of necessary octal digits.
> The number of decimal digits needed never exceeds the number of octal
> digits needed, so this is safe. Add one character for the NUL terminator.)
Yeah I was just guessing here. FWIW gnulib really does the hard work:
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/human.h;h=...
I was prepared to mention log10(2) and that we could approximate it more
closely than with 1/3; gnulib didn't disappoint this time either! :)
Thanks!
Laszlo