write a double in the digits that read back as it

CHICKEN prints 15 significant digits unless told otherwise, too few to
name every double: 0.9871465422297019 reached the C as
0.987146542229702, a different constant. Found in vs-map-viewer, where
380 literals in a port that has to match .NET bit for bit changed this
way. The shortest spelling that reads back is written, up to 17 digits.
This commit is contained in:
2026-10-09 18:15:54 +03:00
parent 449a53abf9
commit 5ccbb5d7e0
3 changed files with 34 additions and 3 deletions

View File

@@ -36,6 +36,7 @@
scheme
(scheme base) ; string->utf8, bytevector accessors
(chicken base)
(chicken flonum) ; flonum-print-precision
fmt
srfi-1
srfi-13)
@@ -192,7 +193,21 @@
((8) (cat "0" (number->string x 8)))
(else (dsp (number->string x))))
st))
(dsp (number->string x))))
(dsp (c-format-flonum x))))
;; MODIFIED FROM UPSTREAM fmt-c: a double in as few digits as read
;; back as the same double. number->string stops at
;; flonum-print-precision, 15 unless set, and 0.16666666666666666 came
;; out 0.166666666666667: a different double in the C. Seventeen
;; always do.
(define (c-format-flonum x)
(let ((precision (flonum-print-precision)))
(let try ((digits 15))
(flonum-print-precision digits)
(let ((written (number->string x)))
(if (or (= digits 17) (= (string->number written) x))
(begin (flonum-print-precision precision) written)
(try (+ digits 1)))))))
(define (c-format-string x)
(lambda (st) ((cat #\" (apply-cat (c-string-escaped x)) #\") st)))