Skip to content

Commit c7a2930

Browse files
committed
double-str: skip .0 suffix when scientific notation is used
1 parent 4f7945c commit c7a2930

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

carp-reader.carp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,17 @@ strings round-trip readably.")
121121
caller's formatting pass.")
122122
(private double-str)
123123
(hidden double-str)
124-
(doc double-str "renders a `Double` with a decimal point preserved.
125-
`Double.str 10.0` returns `\"10\"`, which round-trips as an `Int`; we
126-
append `.0` so the source reads as a double again.")
124+
(doc double-str "renders a `Double` keeping it lexically a double.
125+
`Double.str 10.0` returns `\"10\"`, which would round-trip as an `Int`;
126+
scientific notation (`1e+09`) round-trips fine on its own. Append `.0`
127+
only when the output has neither a decimal point nor an exponent.")
127128
(defn double-str [d]
128129
(let-do [s (Double.str d)]
129-
(if (String.contains? &s \.) s (String.append &s ".0"))))
130+
(if (or
131+
(String.contains? &s \.)
132+
(or (String.contains? &s \e) (String.contains? &s \E)))
133+
s
134+
(String.append &s ".0"))))
130135

131136
(defn str [a]
132137
(match-ref a

test/carp-reader.carp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@
156156
"10.0"
157157
&(Located.str &(first-form "10.0"))
158158
"10.0 round-trips as Double, not Int")
159+
; Big doubles render in scientific notation; no `.0` should be
160+
; appended in that case (it would produce invalid `1e+09.0`).
161+
(assert-equal t
162+
"1e+09"
163+
&(Located.str &(first-form "999999999.0"))
164+
"large Double keeps scientific notation, no spurious .0")
159165

160166
; --- end positions track the byte after the form ---
161167
(assert-equal t

0 commit comments

Comments
 (0)