unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Mikael Djurfeldt <djurfeldt@nada.kth.se>
Cc: djurfeldt@nada.kth.se
Subject: Re: GMP code committed -- watch for bugs.
Date: Sun, 06 Apr 2003 21:12:20 +0200	[thread overview]
Message-ID: <xy7el4fl9t7.fsf@nada.kth.se> (raw)
In-Reply-To: <87vfxrv543.fsf@raven.i.defaultvalue.org> (Rob Browning's message of "Sun, 06 Apr 2003 13:43:40 -0500")

Rob Browning <rlb@defaultvalue.org> writes:

> Mikael Djurfeldt <djurfeldt@nada.kth.se> 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


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


  reply	other threads:[~2003-04-06 19:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-04 22:34 GMP code committed -- watch for bugs Rob Browning
2003-04-05 12:24 ` Marius Vollmer
2003-04-05 21:51 ` Mikael Djurfeldt
2003-04-05 22:26   ` Rob Browning
2003-04-06  7:41     ` Mikael Djurfeldt
2003-04-06  9:16   ` Mikael Djurfeldt
2003-04-06 18:43     ` Rob Browning
2003-04-06 19:12       ` Mikael Djurfeldt [this message]
2003-04-06 20:09         ` Rob Browning
2003-04-06 20:25     ` Rob Browning
2003-04-06  9:23 ` Mikael Djurfeldt
2003-04-06 20:15   ` Rob Browning
2003-04-06 22:50 ` Kevin Ryde
2003-05-10  0:22   ` GMP code committed -- watch for bugs ... gcd n 0 Kevin Ryde
2003-04-23 22:40 ` GMP code committed -- watch for bugs Kevin Ryde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xy7el4fl9t7.fsf@nada.kth.se \
    --to=djurfeldt@nada.kth.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).