From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Bill Schottstaedt Newsgroups: gmane.lisp.guile.devel Subject: ratio implementation Date: Mon, 28 Jul 2003 04:24:57 -0700 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <3F250809.9030108@ccrma.stanford.edu> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: main.gmane.org 1059391669 3546 80.91.224.249 (28 Jul 2003 11:27:49 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 28 Jul 2003 11:27:49 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Jul 28 13:27:45 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 19h6AK-0000uj-00 for ; Mon, 28 Jul 2003 13:27:45 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19h6A0-00049S-9X for guile-devel@m.gmane.org; Mon, 28 Jul 2003 07:27:24 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19h68a-0003cy-6q for guile-devel@gnu.org; Mon, 28 Jul 2003 07:25:56 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19h684-0003Y3-PM for guile-devel@gnu.org; Mon, 28 Jul 2003 07:25:26 -0400 Original-Received: from smtp5.stanford.edu ([171.67.16.30]) by monty-python.gnu.org with esmtp (Exim 4.20) id 19h67i-0003K2-Js for guile-devel@gnu.org; Mon, 28 Jul 2003 07:25:02 -0400 Original-Received: (from root@localhost) by smtp5.Stanford.EDU (8.12.9/8.12.9) id h6SBP0i9003249 for guile-devel@gnu.org; Mon, 28 Jul 2003 04:25:00 -0700 (PDT) Original-Received: from cm-mail.stanford.edu (cm-mail.Stanford.EDU [171.64.197.135]) by smtp5.Stanford.EDU (8.12.9/8.12.9) with ESMTP id h6SBOwx8003245 for ; Mon, 28 Jul 2003 04:24:58 -0700 (PDT) Original-Received: from cmn30.stanford.edu (cmn30.stanford.edu [171.64.197.179]) by cm-mail.stanford.edu (8.11.6/8.11.6) with ESMTP id h6SBOw006218 for ; Mon, 28 Jul 2003 04:24:58 -0700 Original-Received: from ccrma.stanford.edu (localhost [127.0.0.1]) by cmn30.stanford.edu (8.12.8/8.12.5) with ESMTP id h6SBOvMA002804 for ; Mon, 28 Jul 2003 04:24:58 -0700 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225 X-Accept-Language: en-us, en Original-To: guile-devel@gnu.org X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Developers list for Guile, the GNU extensibility library List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:2659 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:2659 There is an implementation of ratios for Guile (based on CVS version of 27-Jul-03) at ccrma-ftp.stanford.edu:/pub/Lisp/gratio.tar.gz. Rather than send a huge diff, I placed the new versions of the changed files (all from the libguile directory) in the tarball along with *.diff showing the changes. I added numerator, denominator, rationalize and ratio?, and at the C level scm_make_ratio and scm_i_ratio2real (should it be scm_ratio2dbl?). "ratio?" is needed because "rational?" returns #t if passed a real -- there has to be some way to distiguish a ratio from a real. An alternative would be to make "rational?" rational. I don't know how the FSF/GPL copyright stuff works, but I did this work on my own time, did not look at any other implementation, and hereby donate the code to you. I'd be happy to "sign the papers". I use longs for the numerator and denominator, so if bignums are encountered, I fallback on the old method using scm_divide. I didn't try to provide the standard set of C-level type conversions -- not sure these are needed in this case. I decided to follow Common Lisp and reduce ratios, and return an integer if the ratio denominator is 1 (or if the numerator is 0). One ugly change is the SCM_NUMP macro in numbers.h -- the original of this macro strikes me as a kludge; the new version is worse. A questionable change (I followed Dybvig's book p 126): (/ 17) -> 1/17, (/ 2 3) -> 2/3. I notice that r5rs seems to imply that (inexact->exact .3) should return 3/10 (see the rationalize example which is assuming this) -- should I implement this? What is "reasonably close" in this case? Another gray area involves functions like round -- in r5rs (round 7/2) returns 4, whereas Guile returns 4.0 (I left it this way because (round 7) currently returns 7.0 rather than 7) -- I decided to make minimal changes, but handling of exact/inexact distinctions in Guile could use some work (I am willing do this, if others approve). Also, should random accept ratios? And currently (format #f "~F" 2/3) hangs, but so does (format #f "~B" 1.5) (in Guile 1.6.4 you get an error, but in the CVS Guile it hangs in mutex_lock). The changed files are: smob.c hash.c eq.c goops.c gc-card.c objects.c numbers.c (nearly all the changes are in this file) eval.c numbers.h tags.h objects.h Also in the tarball: ratio-tests.scm. _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://mail.gnu.org/mailman/listinfo/guile-devel