bug-gnulib
[Top][All Lists]
Advanced

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

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


From: Jim Meyering
Subject: [bug-gnulib] Re: [bug-grep] length of dec. representation of a number
Date: Tue, 08 Mar 2005 16:11:45 +0100

Paul Eggert <address@hidden> wrote:
> Here's what I've drafted so far -- I haven't tested it though.  I'm
> planning to turn this sort of thing into a separate gnulib module with
> an include file named "int-props.h" or something like that; this sort
> of code is used in several places in gnulib and coreutils now, and
> should be factored out.
>
> /* The extra cast works around compiler bugs.  */
> #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
>
> /* Bound on length of the string representing an integer value of type t.
>    Subtract one for the sign bit if t is signed;
>    30103 / 100000 is log10 (2) rounded up;
>    round the result up;
>    add one for a minus sign if t is signed.  */
> #define INT_STRLEN_BOUND(t) \
>  (((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 30103 + 99999) / 100000 \
>   + TYPE_SIGNED (t))
>
> #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)

I like the idea of factoring these out into their own header file.

Another such header file might include definitions for TYPE_MINIMUM and
TYPE_MAXIMUM and the fallback _MIN and _MAX definitions for common types,
like UINTMAX_MAX, OFF_T_MAX, etc.

Or maybe they belong in the proposed int-props.h file?
TYPE_MINIMUM and TYPE_MAXIMUM do depend on TYPE_SIGNED, after all.




reply via email to

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