On Tue, Jan 10, 2012 at 03:36:04AM +0100, Alexander Shendi wrote:
> Dear Chicken users,
> 
> I have encounterd what seems to be a bug in the numbers egg. The problem
> occurs when I evaluate "(log 297.0+0.1i)".

Please try the attached patch (or numbers trunk, as of r25802).

#;1> (use numbers)
#;2> (log 297.0+0.1i)
5.69373219548625+0.000336700323976755i

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth
Index: numbers.scm
===================================================================
--- numbers.scm (revision 25798)
+++ numbers.scm (working copy)
@@ -1359,15 +1359,17 @@
 (define exp %exp)
 
 (define (%log x)
-  (cond
-   ((%> x 0.0 'log) ; avoid calling inexact->exact on X here (to avoid 
overflow?)
-    (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x)))
-   ((%= x 0.0)
-    (if (%exact? x)
-        (log0 'log x)
-       (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x))))
-   (else ; negative
-    (%+ (%log (%magnitude x)) (* (make-complex 0 1) (%angle x))))))
+  (let ((type (%check-number x)))
+    (cond
+     ;; avoid calling inexact->exact on X here (to avoid overflow?)
+     ((or (eq? type COMP) (%< x 0.0 'log)) ; General case
+      (%+ (%log (%magnitude x)) (* (make-complex 0 1) (%angle x))))
+     ((eq? x 0)                        ; Exact zero?  That's undefined
+      (log0 'log x))
+     ((eq? type NONE)
+      (bad-number 'exp x))
+     (else                             ; Simple real number case
+      (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x))))))
 
 (define log %log)
 
Index: tests/numbers-test.scm
===================================================================
--- tests/numbers-test.scm      (revision 25798)
+++ tests/numbers-test.scm      (working copy)
@@ -418,6 +418,13 @@
  (test "log of exp = 1" 1.0 (log (exp 1)))
  (test "log of -1" (string->number "0.0+3.141592653589793i") (log -1))
 
+ (test "log with complex number"
+       (string->number "0.0+1.5707963267948966i")
+       (log (string->number "+i")))
+
+ (test "exp(log(x)) = x"
+       (string->number "2.0-3.0i") (exp (log (string->number "2.0-3.0i"))))
+
  (letrec ((fac (lambda (n)
                  (if (zero? n)
                      1
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to