Fix reversal of results in rounding all leading 9's (issue #859).

This commit is contained in:
Alex Shinn 2022-09-15 21:24:00 +09:00
parent fa8a506ed5
commit 1e47c78b8a
2 changed files with 17 additions and 8 deletions

View file

@ -210,6 +210,15 @@
(test "1.1" (show #f (numeric #i9/8 10 1)))
(test "1" (show #f (numeric #i9/8 10 0)))
(test "1.0"
(show #f (with ((precision 1)) 0.999999999999876)))
(test "10.0"
(show #f (with ((precision 1)) 9.999999999999876)))
(test "10.0"
(show #f (numeric 9.999999999999876 10 1)))
(test "10.00"
(show #f (numeric 9.999999999999876 10 2)))
;; precision-show, base-4
(test "1.1230" (show #f (numeric 91/64 4 4)))
(test "1.123" (show #f (numeric 91/64 4 3)))

View file

@ -145,7 +145,7 @@
(let lp ((ls ls) (res '()))
(cond
((null? ls)
(cons 1 res))
(append (reverse res) '(1)))
((not (number? (car ls)))
(lp (cdr ls) (cons (car ls) res)))
((= (car ls) (- radix 1))
@ -203,9 +203,9 @@
(+ i 1)
(cons q res))))))
(else
(list->string
(reverse-list->string
(map char-digit
(reverse (maybe-trim-zeros i (maybe-round n d res) (inexact? n-orig))))))))))
(maybe-trim-zeros i (maybe-round n d res) (inexact? n-orig)))))))))
;; Generate a fixed precision decimal result by post-editing the
;; result of string->number.
(define (gen-fixed n)
@ -240,11 +240,11 @@
(string-ref/cursor
s (string-cursor-prev s last)))
'(1 3 5 7 9))))))
(list->string
(reverse
(reverse-list->string
(map char-digit
(round-up
(reverse (map digit-value (string->list res)))))))
(reverse
(map digit-value (string->list res))))))
res))))))
(else
(gen-general n))))