Skip to content

state_exec: to_string prints a float in its shortest round-trip form - #889

Merged
transfix merged 5 commits into
masterfrom
fix/to-string-shortest-float
Oct 30, 2026
Merged

transfix merged 5 commits into
masterfrom
fix/to-string-shortest-float

Conversation

@transfix

Copy link
Copy Markdown
Owner

Stacked on #885. This branch holds #885's commit (5ebe185) and one of its own (9fe3518);
review 9fe3518. Merge #885 first; I'll rebase this onto master then. It targets master because
CI runs only on PRs into master.

str, str-concat, print, an Ariadne computed label and a list's printed form wrote a float
with six significant digits: (str 1234567.5) was "1.23457e+06", and a float inside a list
lost digits the same way. After #885, state-set stores "1234567.5", so (state-set k (str d))
and (state-set k d) stored different text. Now they store the same text.

Change

  • The print walker's double branch (src/cvc/state/state_exec/data_job.cpp) calls
    format_number_shortest instead of a default std::ostringstream.
  • format_number_shortest moves out of intrinsics.cpp's anonymous namespace and is declared in
    types.h, beside to_string. Its definition (std::to_chars, or
    detail::format_number_shortest_fallback where the library has no floating to_chars) stays
    in intrinsics.cpp.
expression before after
(str 1234567.5) 1.23457e+06 1234567.5
(str (+ 0.1 0.2)) 0.3 0.30000000000000004
(str (/ 1.0 3)) 0.333333 0.3333333333333333
(str (list 1234567.5)) (1.23457e+06) (1234567.5)
(str 2.0) 2 2
(str 100000.0) 100000 1e+05
(str 0.1), (str 1e21) 0.1, 1e+21 unchanged

A whole float prints as 2, not 2.0

I kept the current form, for three reasons:

  • str then writes exactly what state-set and state-incr store, so the two ways of writing a
    number to the tree agree byte for byte.
  • Ariadne's computed labels (display_string -> to_string) keep showing 3 for (* 1.5 2).
    Only non-whole floats change, and only by gaining the digits they had lost.
  • 2.0 is there so a whole float reads back as a float, which is the parse(to_string(v)) == v
    goal of the grammar's phase P5. That goal also needs string escaping and +inf.0. Doing one
    piece of it here would change the printed form twice.

Visible changes to watch

Docs

Tests

  • BuiltinsTest.StrWritesAFloatInItsShortestRoundTripForm: (str 1234567.5),
    (str (+ 0.1 0.2)), 0.1, 1e21, 2.0 -> "2", -0.0 -> "-0", a list's floats,
    str-concat with a float; strtod of the printed text equals the value for five more.
  • StateTreeIntrinsicsTest.StateSetStoresAFloatInItsShortestRoundTripForm also checks that
    str and state-set write the same text.
  • No existing test pinned the six-digit print text.
  • Local (Linux, GCC, Ninja Release; CUDA, CVCGL, ARIGL, ARISRV, CLI, Jolt, libigl, assimp,
    ImageMagick, HDF5, CGAL and mesher off): all 35 state_exec_* and ariadne_* binaries pass.
    So do async_lane_test, stream_test, tf_overlay_tasks_test, app_test and thread_pool_test, the
    other tests that run the DSL.
  • Not run locally: cvc_cli_test (CLI off). Its float check only looks for "78.", and
    3.14159 * 25 prints 78.53975 either way. cvcGL tests (CVCGL off): their float strings are
    node values, not printed DSL floats.
  • git clang-format (18.1.3) applied.

coerce_state_string formatted a double with a default ostringstream, so
state-set (and state-cas / state-swap, which share it) kept six significant
digits: (state-set "x" 1234567.5) stored "1.23457e+06", and 0.1 + 0.2 stored
"0.3", which reads back as a different number. It now uses
format_number_shortest, as state-incr already does: "1234567.5",
"0.30000000000000004", "0.1", "1e+21". Of the fixed and scientific texts the
shorter is written, so 100000.0 is now "1e+05" (it was "100000").

The Ariadne widgets are unaffected: slider_float writes through
cvc::state::value<double> (lexical_cast), and fmt: formats only its display.
str and list printing still write six digits (to_string in data_job.cpp).

STATE_EXEC_DEVELOPER_GUIDE.md section 5 now says how state-set turns a value
into text, and its state-children example shows what state-children returns:
every descendant, in pre-order, by full path. A test pins that example.
The print walker (data_job.cpp) wrote a double with a default ostringstream,
so str, str-concat, print, an Ariadne computed label and a list's printed form
kept six significant digits: (str 1234567.5) was "1.23457e+06". It now calls
format_number_shortest, the text state-set and state-incr store, so
(state-set k (str d)) and (state-set k d) store the same thing.

format_number_shortest moves out of intrinsics.cpp's anonymous namespace and
is declared in types.h, beside to_string.

A whole float still prints as an integer's digits (2.0 as "2"), as before and
as state-set stores it; printing it as "2.0" so it reads back as a float
belongs with the rest of grammar phase P5's round trip (string escaping,
+inf.0). Non-whole floats now print up to 17 digits: (/ 1.0 3) is
"0.3333333333333333", where it was "0.333333".

Docs: the developer guide's note that str prints six digits goes; the locks
doc no longer says coerce_state_string writes 6 digits (stale since the
state-set change); the grammar doc says what to_string prints now and what P5
still has to do; the CLI guide shows (/ 355 113.0)'s full text.
@transfix
transfix enabled auto-merge (squash) October 30, 2026 05:48
…est-float

# Conflicts:
#	docs/STATE_EXEC_DEVELOPER_GUIDE.md
#	src/cvc/tests/state_exec_intrinsics_test.cpp
@transfix
transfix merged commit a137a27 into master Oct 30, 2026
22 of 26 checks passed
@transfix
transfix deleted the fix/to-string-shortest-float branch October 30, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant