Handling ratios, inexact and complex in number->string.

Fixes issue #317.
This commit is contained in:
Alex Shinn 2016-03-19 15:12:09 +09:00
parent fec1016254
commit 57dd5464c5

View file

@ -394,12 +394,27 @@
((if (null? o) #t (eq? 10 (car o))) ((if (null? o) #t (eq? 10 (car o)))
(%number->string num)) (%number->string num))
(else (else
(let lp ((n (abs num)) (d (car o)) (res '())) (let ((d (car o)))
(if (> n 0) (cond
(lp (quotient n d) d (cons (digit-char (remainder n d)) res)) ((%complex? num)
(if (null? res) (let ((real (real-part num))
"0" (imag (imag-part num)))
(list->string (if (negative? num) (cons #\- res) res)))))))) (string-append (number->string real d) (if (negative? imag) "-" "+")
(number->string imag d) "i")))
((inexact? num)
(string-append "#i" (number->string (inexact->exact num) d)))
((ratio? num)
(string-append (number->string (numerator num) d) "/"
(number->string (denominator num) d)))
(else
(let lp ((n (abs num)) (res '()))
(cond
((> n 0)
(lp (quotient n d) (cons (digit-char (remainder n d)) res)))
((null? res)
"0")
(else
(list->string (if (negative? num) (cons #\- res) res)))))))))))
(define (list->string ls) (define (list->string ls)
(call-with-output-string (call-with-output-string