Skip to content

state_exec: state-set stores a float in its shortest round-trip form - #885

Merged
transfix merged 2 commits into
masterfrom
fix/state-set-shortest-float
Oct 30, 2026
Merged

transfix merged 2 commits into
masterfrom
fix/state-set-shortest-float

Conversation

@transfix

@transfix transfix commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

state-set wrote a float with six significant digits, so a balance or a price lost 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. state-incr and #883's state-set-tree already write the shortest
text that reads back as the same double; state-set now does too. This came up while building
#883 and was left out of it because it changes existing output.

Change

  • coerce_state_string (src/cvc/state/state_exec/intrinsics.cpp) formats a double with
    format_number_shortest (std::to_chars, or detail::format_number_shortest_fallback where
    the library has no floating-point to_chars) instead of a default std::ostringstream.
    format_number_shortest moves above it; <sstream> is no longer used there.
  • Its callers: state-set (scalars), and state-cas / state-swap through atomic_scalar.
    All three now store the same text state-incr does.
value before after
1234567.5 1.23457e+06 1234567.5
(+ 0.1 0.2) 0.3 0.30000000000000004
0.1 0.1 0.1
1e21 1e+21 1e+21
100000.0 100000 1e+05

The last row changes for the worse to the eye: to_chars writes the shorter of the fixed and
scientific forms. The text still reads back as the same number, state-incr already writes it
this way, and state_exec_locks_test pins 1e5 -> "1e+05" for the formatter. Whole floats below 1e5, and those with more
significant digits (120000.0 -> "120000"), are unchanged.

Not affected

  • Ariadne widgets: slider_float writes through cvc::state::value<double> (bind.h's
    write<T>, boost::lexical_cast), not coerce_state_string, and fmt: formats only the
    slider's display. No other widget or .ari component writes floats through it.
  • str and a list or dict's printed form still write six digits (to_string in
    data_job.cpp). So (state-set "x" (str 1234567.5)) still stores "1.23457e+06"; the guide
    now says to pass the number itself. Changing to_string would move many print goldens, so it is
    left for its own PR.

Docs

docs/STATE_EXEC_DEVELOPER_GUIDE.md:

  • §5 says how state-set turns a value into text, with the examples above.
  • §5's (state-children "myapp.config") ;; => ("timeout") was wrong: state-children returns
    every descendant, in pre-order, by full path (state::children()). The example now shows
    ("myapp.config.timeout") and a nested ("myapp.users.ann" "myapp.users.ann.role").
  • §12.5 no longer says state-set converts with str.

Tests

  • StateTreeIntrinsicsTest.StateSetStoresAFloatInItsShortestRoundTripForm: 1234567.5, 0.1,
    1e21, -2.5, 1e5, 0.1 + 0.2; strtod of the stored text equals the value for
    0.1 + 0.2, 1/3, 6.02214076e23, 5e-324, 123456789.123; state-swap stores the same.
  • StateTreeIntrinsicsTest.StateChildrenListsEveryDescendantByFullPath pins the guide's example.
  • Local (Linux, GCC, Ninja Release; CUDA, CVCGL, ARIGL, ARISRV, CLI, Jolt, libigl, assimp,
    ImageMagick, HDF5, CGAL and mesher off): every state_exec_*_test and ariadne_*_test binary
    passes (34 binaries, intrinsics 158/158). No existing test pinned the six-digit text.
  • git clang-format (18.1.3): no changes.

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.
@transfix

Copy link
Copy Markdown
Owner Author

The red recipe / macos-latest / Release (run 40987421212, job 123841580916) isn't caused by this PR:

So a re-run would fail the same way until #886 is on master. I'll re-run this job then. #889 (stacked on this) will hit the same failure.

@transfix

Copy link
Copy Markdown
Owner Author

#886 is on master now (a872192). I merged master into this branch (8c2da63) instead of re-running the job. A re-run keeps the original run's commit, and the macOS jobs checked out 8b333b55 by SHA. That's this PR merged into 4bd513ad, from before #886, so a re-run would build without the fix. The push starts a new run on a fresh merge. All three red macOS jobs here (recipe Release, recipe Debug, package-macos / libcvc-release) were the same MeshContourTree.RandomFieldsOnLoopyDomainsStillGiveTrees failure. #889 got the same treatment (9c7ce76).

@transfix

Copy link
Copy Markdown
Owner Author

The new run (merge 1f389e12, on master 4580d124) passes MeshContourTree now that #886 is in. recipe / macos-latest / Release is red again, on a different test: Fem.SteppedPoissonMatchesTheOneShotSolve (fem_test.cpp:612, "a single source value": max relative difference 0.0968, where the test allows under 1e-10). That test came with #890 (merged 04:37 UTC), whose macOS jobs were cancelled before it merged, so it hadn't run on macOS before. This PR doesn't touch FEM or the stepped solver. The check isn't required, so it doesn't block the auto-merge.

@transfix
transfix merged commit 9cac267 into master Oct 30, 2026
20 of 26 checks passed
@transfix
transfix deleted the fix/state-set-shortest-float branch October 30, 2026 07:25
transfix added a commit that referenced this pull request Oct 30, 2026
…ts in full

#885 made state-set write a float in its shortest round-trip form, as state-set-tree already
did, so the comment, the developer guide and a test note no longer call that a difference.
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