unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Ian Price <ianprice90@googlemail.com>
To: bug-guile@gnu.org
Subject: (@ (rnrs base) log) optional second argument
Date: Wed, 6 Apr 2011 02:11:23 +0100	[thread overview]
Message-ID: <BANLkTi=90FXeuOb3aVUqHbTPqZkYfai6Pg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 401 bytes --]

Hello guilers,

Guile does not implement the optional second argument for log. I have
attached a simple patch that implements it using the formula you learn
in high school.

I have left the behaviour for two special cases alone, as there is no
clear right answer among the current R6RS implementations. If you have
strong opinions on this behaviour, feel free to write something better.

Regards,
Ian

[-- Attachment #2: 0001-Added-optional-second-arg-to-R6RS-log-function.patch --]
[-- Type: application/octet-stream, Size: 2192 bytes --]

From 733d8f68226db82fb1dda0b492ed4ab74ba779dd Mon Sep 17 00:00:00 2001
From: Ian Price <ianprice90@googlemail.com>
Date: Wed, 6 Apr 2011 01:53:38 +0100
Subject: [PATCH] Added optional second arg to R6RS log function

* module/rnrs/base.scm (log): now takes a base argument, using the
  change of base formula for logs.
* test-suite/tests/r6rs-base.test ("log (2nd arg)"): Add test cases.
---
 module/rnrs/base.scm            |    9 +++++++++
 test-suite/tests/r6rs-base.test |   16 ++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/module/rnrs/base.scm b/module/rnrs/base.scm
index 2f5a218..be25b6e 100644
--- a/module/rnrs/base.scm
+++ b/module/rnrs/base.scm
@@ -74,6 +74,7 @@
 
 	  syntax-rules identifier-syntax)
   (import (rename (except (guile) error raise)
+                  (log log-internal)
                   (euclidean-quotient div)
                   (euclidean-remainder mod)
                   (euclidean/ div-and-mod)
@@ -85,6 +86,14 @@
                   (inexact->exact exact))
           (srfi srfi-11))
 
+ (define log
+   (case-lambda
+     ((n)
+      (log-internal n))
+     ((n base)
+      (/ (log n)
+         (log base)))))
+
  (define (boolean=? . bools)
    (define (boolean=?-internal lst last)
      (or (null? lst)
diff --git a/test-suite/tests/r6rs-base.test b/test-suite/tests/r6rs-base.test
index 1509b04..dfddf7c 100644
--- a/test-suite/tests/r6rs-base.test
+++ b/test-suite/tests/r6rs-base.test
@@ -21,6 +21,22 @@
   :use-module ((rnrs base) :version (6))
   :use-module (test-suite lib))
 
+
+;; numbers are considered =? if their difference is less than a set
+;; tolerance
+(define (=? alpha beta)
+  (< (abs (- alpha beta)) 1e-10))
+
+(with-test-prefix "log (2nd arg)"
+  (pass-if "log positive-base" (=? (log 8 2) 3))
+  (pass-if "log negative-base" (=? (real-part (log 256 -4))
+                                   0.6519359443))
+  (pass-if "log base-one" (= (log 10 1) +inf.0))
+  (pass-if "log base-zero"
+    (catch #t
+      (lambda () (log 10 0) #f)
+      (lambda args #t))))
+
 (with-test-prefix "boolean=?"
   (pass-if "boolean=? null" (boolean=?))
   (pass-if "boolean=? unary" (boolean=? #f))
-- 
1.7.3.4


             reply	other threads:[~2011-04-06  1:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-06  1:11 Ian Price [this message]
2011-04-11 15:57 ` (@ (rnrs base) log) optional second argument Andy Wingo
2011-04-11 16:52   ` Ian Price

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='BANLkTi=90FXeuOb3aVUqHbTPqZkYfai6Pg@mail.gmail.com' \
    --to=ianprice90@googlemail.com \
    --cc=bug-guile@gnu.org \
    /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).