mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-19 13:49:17 +02:00
Handling ratios, inexact and complex in number->string.
Fixes issue #317.
This commit is contained in:
parent
fec1016254
commit
57dd5464c5
1 changed files with 21 additions and 6 deletions
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue