From 66d088f6fc853d20cf6e26ba7a12ea98b0fb406b Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 18:15:54 +0300 Subject: [PATCH] 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. --- sex-fmt-c.scm | 17 ++++++++++++++++- tests/codegen.scm | 13 ++++++++++++- tests/sex-programs/operators.sex | 7 ++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index 215617a..f07644e 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -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))) diff --git a/tests/codegen.scm b/tests/codegen.scm index 2b5847a..03638e4 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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" diff --git a/tests/sex-programs/operators.sex b/tests/sex-programs/operators.sex index 03baec0..7481f55 100644 --- a/tests/sex-programs/operators.sex +++ b/tests/sex-programs/operators.sex @@ -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))