Alex Shinn wrote:
> One more thing - how was (expt 0 z) extended to
> return 0 for complex z? The approaches I've tried
> would result in NaN, and indeed this seems to be
> what most implementations return.
You may be misinterpreting the R6RS. (expt 0 z) returns zero
if the real part of z is positive. If z is zero, then it's
supposed to return zero. If z is non-zero and its real part
is zero or negative, then implementations are free to return
whatever number object they like, or to raise an exception
with condition type &implementation-restriction.
Implementation is straightforward. Here is Larceny's code:
(define (expt x y)
; x is nonzero, and y is an exact natural number.
(define (e x y)
(cond ((= y 0)
1)
((odd? y)
(* x (e x (- y 1))))
(else
(let ((v (e x (quotient y 2))))
(* v v)))))
(cond ((zero? x)
(let ((result (cond ((= y 0) 1)
((> (real-part y) 0) 0)
(else +nan.0))))
(if (and (exact? x) (exact? y))
result
(exact->inexact result))))
((and (exact? y) (integer? y))
(if (negative? y)
(/ (expt x (abs y)))
(e x y)))
(else
(exp (* y (log x))))))
Will
_______________________________________________
r6rs-discuss mailing list
[email protected]
http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss