bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-grep] length of dec. representation of a number


From: Paul Eggert
Subject: Re: [bug-grep] length of dec. representation of a number
Date: Thu, 24 Feb 2005 12:58:59 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

Stepan Kasal <address@hidden> writes:

>   I'm going to check in the following small patch.

How about if you use the standard gnulib macro for this instead of
reinventing the wheel?  Here it is, taken from inttostr.h:

/* Upper bound on the string length of an integer converted to string.
   302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
   add 1 for integer division truncation; add 1 more for a minus sign.  */
#define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)

#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)



Julian Foad <address@hidden> writes:

> Certainly "CHAR_BIT" was inappropriate and misleading

No, it's appropriate.  For example, sizeof (int) * CHAR_BIT is an
upper bound on the number of value bits in an int.  (On some
architectures, some of the int bits might not contribute to the value;
but you can't have more value bits than total bits.)

On some machines, CHAR_BIT is 32 and sizeof (int) == 1.  Admittedly
these are rare as GNU porting targets (they're typically DSPs or
something like that) but if it's easy to port to them we might as well
do so.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]