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 7a15cde637
commit 5b7ed9793b
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)))

View File

@@ -789,7 +789,18 @@ whitespace aside, THEN?"
(test-assert "a subscript"
(emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])")))
;; A define's body was pasted unwalked, and unparenthesised: `cast'
;; A double is written in as few digits as read back as the same
;; double: CHICKEN prints 15 by default, and 0.16666666666666666 came
;; out 0.166666666666667, another number
(test-group "double literals"
(test-assert "keep every digit they need"
(emits? (in-fn "(var d double 0.16666666666666666)") "d = 0.16666666666666666;"))
(test-assert "and gain none they do not"
(emits? (in-fn "(var d double 0.1)") "d = 0.1;"))
(test-assert "sixteen when that is enough"
(emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;")))
;; A define's body was pasted unwalked, and unparenthesised: `cast'
;; stayed a call, and (* x D) regrouped with D's own operator
(test-group "defines"
(test-assert "an atom is left bare"

View File

@@ -8,7 +8,8 @@
"toplevel: 1 2.5 hi 12"
"closure in place: 5"
"a type is not a call: 7"
"radix literals: 224 5 128")
"radix literals: 224 5 128"
"a double reaches C whole: 1")
(return 0)
;;; The walk types an expression by its head, and the heads it had a
@@ -92,4 +93,8 @@
;; a mask reads best in the radix it is a mask in
(var lead int 0xE5)
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
;; ...and a double in all the digits it was written with
(var sixth double (/ 1.0 6.0))
(printf "a double reaches C whole: %d\n" (== 0.16666666666666666 sixth))
(return 0))