guile-devel
[Top][All Lists]
Advanced

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

Re: GMP code committed -- watch for bugs.


From: Mikael Djurfeldt
Subject: Re: GMP code committed -- watch for bugs.
Date: Sun, 06 Apr 2003 21:12:20 +0200
User-agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.2 (gnu/linux)

Rob Browning <address@hidden> writes:

> Mikael Djurfeldt <address@hidden> writes:
>
>>               numbits = mpz_sizeinbase (SCM_I_BIG_MPZ (num), 2);
>>               if (UNSIGNED) numbits++;
>>               scm_remember_upto_here_1 (num);
>>               if (numbits > (sizeof (ITYPE) * 8))
>>                 scm_out_of_range (s_caller, num);
>>
>> Firstly, you probably intended to write "if (!UNSIGNED)" (to make room
>> for the sign bit).
>
> Indeed.
>
>> Secondly, that doesn't work either.  For example, the absolute value
>> of LLONG_MIN requires 64 bits, even though it is a signed number.
>> So, regardless if we have "if (UNSIGNED)" or "if (!UNSIGNED)" there
>> will always be a case where a valid long_long or ulong_long will
>> cause an out_of_range error.
>
> Hmm.  Actually it looks like mpz_sizeinbase includes the needed sign
> bit in its computation (for some reason I thought it didn't).  So I
> don't think we need the numbits++ at all.

That might be true, but it is difficult to arrive at that result from
the gmp manual entry:

 - Function: size_t mpz_sizeinbase (mpz_t OP, int BASE)
     Return the size of OP measured in number of digits in base BASE.
     The base may vary from 2 to 36.  The sign of OP is ignored, just
     the absolute value is used.  The result will be exact or 1 too
     big.  If BASE is a power of 2, the result will always be exact.
     If OP is zero the return value is always 1.

     This function is useful in order to allocate the right amount of
     space before converting OP to a string.  The right amount of
     allocation is normally two more than the value returned by
     `mpz_sizeinbase' (one extra for a minus sign and one for the
     null-terminator).

I guess, as always, the source code is the best documentation. :)

M




reply via email to

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