From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Mikael Djurfeldt Newsgroups: gmane.lisp.guile.devel Subject: Re: GMP code committed -- watch for bugs. Date: Sun, 06 Apr 2003 21:12:20 +0200 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: References: <87he9dhoxh.fsf@raven.i.defaultvalue.org> <87vfxrv543.fsf@raven.i.defaultvalue.org> Reply-To: djurfeldt@nada.kth.se NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1049656412 15702 80.91.224.249 (6 Apr 2003 19:13:32 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sun, 6 Apr 2003 19:13:32 +0000 (UTC) Cc: djurfeldt@nada.kth.se Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Apr 06 21:13:30 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 192Fa6-00044b-00 for ; Sun, 06 Apr 2003 21:13:30 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 192FZq-0001Ec-08 for guile-devel@m.gmane.org; Sun, 06 Apr 2003 15:13:14 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10.13) id 192FZY-0001CF-00 for guile-devel@gnu.org; Sun, 06 Apr 2003 15:12:56 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10.13) id 192FZW-0001Bl-00 for guile-devel@gnu.org; Sun, 06 Apr 2003 15:12:55 -0400 Original-Received: from kvast.blakulla.net ([213.212.20.77]) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 192FZ3-00010d-00 for guile-devel@gnu.org; Sun, 06 Apr 2003 15:12:25 -0400 Original-Received: from barbara.blakulla.net ([213.212.21.238] helo=linnaeus) by kvast.blakulla.net with esmtp (Exim 3.36 #1 (Debian)) id 192FZ0-00061p-00; Sun, 06 Apr 2003 21:12:22 +0200 Original-Received: from mdj by linnaeus with local (Exim 3.36 #1 (Debian)) id 192FYz-0001QR-00; Sun, 06 Apr 2003 21:12:21 +0200 Original-To: Rob Browning In-Reply-To: <87vfxrv543.fsf@raven.i.defaultvalue.org> (Rob Browning's message of "Sun, 06 Apr 2003 13:43:40 -0500") User-Agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.2 (gnu/linux) Original-cc: guile-devel@gnu.org X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Developers list for Guile, the GNU extensibility library List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:2135 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:2135 Rob Browning writes: > Mikael Djurfeldt 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