Changeset 25802 in project


Ignore:
Timestamp:
01/10/12 22:12:53 (9 years ago)
Author:
sjamaan
Message:

Fix logarithm so it works for complex numbers (reported by Alexander Shendi)

Location:
release/4/numbers/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/4/numbers/trunk/numbers.scm

    r25798 r25802  
    13601360
    13611361(define (%log x)
    1362   (cond
    1363    ((%> x 0.0 'log) ; avoid calling inexact->exact on X here (to avoid overflow?)
    1364     (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x)))
    1365    ((%= x 0.0)
    1366     (if (%exact? x)
    1367         (log0 'log x)
    1368         (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x))))
    1369    (else ; negative
    1370     (%+ (%log (%magnitude x)) (* (make-complex 0 1) (%angle x))))))
     1362  (let ((type (%check-number x)))
     1363    (cond
     1364     ;; avoid calling inexact->exact on X here (to avoid overflow?)
     1365     ((or (eq? type COMP) (%< x 0.0 'log)) ; General case
     1366      (%+ (%log (%magnitude x)) (* (make-complex 0 1) (%angle x))))
     1367     ((eq? x 0)                        ; Exact zero?  That's undefined
     1368      (log0 'log x))
     1369     ((eq? type NONE)
     1370      (bad-number 'exp x))
     1371     (else                             ; Simple real number case
     1372      (##core#inline_allocate ("C_a_i_log" 4) (%exact->inexact x))))))
    13711373
    13721374(define log %log)
  • release/4/numbers/trunk/tests/numbers-test.scm

    r25798 r25802  
    419419 (test "log of -1" (string->number "0.0+3.141592653589793i") (log -1))
    420420
     421 (test "log with complex number"
     422       (string->number "0.0+1.5707963267948966i")
     423       (log (string->number "+i")))
     424
     425 (test "exp(log(x)) = x"
     426       (string->number "2.0-3.0i") (exp (log (string->number "2.0-3.0i"))))
     427
    421428 (letrec ((fac (lambda (n)
    422429                 (if (zero? n)
Note: See TracChangeset for help on using the changeset viewer.