unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* (@ (rnrs base) log) optional second argument
@ 2011-04-06  1:11 Ian Price
  2011-04-11 15:57 ` Andy Wingo
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Price @ 2011-04-06  1:11 UTC (permalink / raw)
  To: bug-guile

[-- 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-04-11 16:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-06  1:11 (@ (rnrs base) log) optional second argument Ian Price
2011-04-11 15:57 ` Andy Wingo
2011-04-11 16:52   ` Ian Price

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).