Skip to content

fix(format): write exact quantities in cooklang source - #736

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/701-exact-quantities
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/701-exact-quantities

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #701

--format cooklang emitted q.value().to_string(), and Number's Display rounds to 3 decimal places: @saffron{0.0005%g} → 0.001, @water{12.3456%ml} → 12.346. Quantities now write through write_value/write_number instead:

  • Number::Regular writes a shortest() form — the f64 Display (shortest string that reparses to the same value), unless a 10-decimal rounded form is shorter and nonzero, so scaled values print 0.3 rather than 0.30000000000000004 while 0.0005 and 1e-7 stay exact; -0 writes 0.
  • Number::Fraction keeps its Display (1/2, 2 1/2) when err == 0; a fraction carrying a conversion error writes n.value() so the error is not lost.
  • Ranges write each endpoint the same way; Value::Text is verbatim.

Test plan

  • quantities_keep_their_full_precision asserts exact @saffron{0.0005%g}/@water{12.3456%ml}/#spray bottle{0.0000001%l} and reparses to equal quantities
  • scaled_quantities_do_not_print_float_noise: 0.1×3 → 0.3, 1/3×2 → 0.6666666667
  • a_fractions_error_is_written_into_its_value
  • cargo test -p cooklang-format green

Generated with Devin

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This fixes #701 for ingredients, cookware and timers, since all three go through ComponentFormatter. 0.0005, 12.3456, 1e-7 and 1e22 all round-trip. A few points:

  • Float noise in scaled output. 0.1 × 3 used to print 0.3 and now prints 0.30000000000000004. 1/3 cup × 2 used to print 0.667 and now prints 0.6666666666666666. That's lossless but ugly for cook recipe x.cook:2 -f cooklang. One compromise: about 10 decimal places with trailing zeros trimmed. That keeps 0.0005 and 1e-7 exact and drops the noise. Up to the maintainers.
  • Fractions with a rounding error (cooklang_source.rs:281). A fraction's Display drops its err field, so a fraction carrying an error changes value. cookcli's default converter never produces one, but a library user with fractions enabled can. Writing n.value() when err != 0 would cover it.
  • Weak test (:540). contains("0.0005") also matches inside 10.0005. Asserting the exact @saffron{0.0005%g} and parsing it back would be stronger, plus one cookware or timer case.

This conflicts with #737 in the if let Some(q) block (push =, then write_value) and in the tests, and with #735 in the tests. All trivial.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

The writer emitted `q.value().to_string()`, and `Number`'s Display
rounds every value to 3 decimal places — the right summary for a
person, but data corruption in a file meant to be read back:
`@saffron{0.0005%g}` came out as `0.001`. Write the underlying `f64`
of a regular number verbatim; fractions and ranges keep their display
forms.

Closes #701
@drawmeanelephant
drawmeanelephant force-pushed the fix/701-exact-quantities branch from 177ddb1 to 0c7eabd Compare October 14, 2026 12:37
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

All three points addressed:

  • Float noise: Number::Regular now goes through a shortest() helper — f64 Display unless the 10-decimal rounded form is shorter and nonzero. 0.1×3 → 0.3, 1/3×2 → 0.6666666667, while 0.0005 and 0.0000001 still print exactly, and anything that would collapse to 0 keeps the long form. -0 normalizes to 0.
  • Fraction err: a Number::Fraction with err != 0 writes n.value() instead of its Display, so the error survives the round trip. Exact fractions still print 1/2 / 2 1/2.
  • Test: now asserts the exact @saffron{0.0005%g} / @water{12.3456%ml} / #spray bottle{0.0000001%l} (a cookware case), reparses the rendered output and compares ingredient values, plus new tests for scaled noise and fraction err.

@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks, all three points are covered. I ran the PR binary on 0.1×3 (0.3), 1/3×2 (0.6666666667), 0.0005, 0.0000001, a 21-digit integer scaled by 2 (exact now, upstream gives 199999999999999970000), and fractions with and without a lock. I also round-tripped cook recipe x.cook:3 -f cooklang through the parser again. Every quantity comes back the same, except the float noise the PR now rounds away on purpose. fmt, clippy, the workspace tests and CI are green, and it looks good to merge. The conflicts with #735 and #737 are still there and still trivial.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@LeMyst
LeMyst merged commit a09f41d into cooklang:main Oct 17, 2026
5 checks passed
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.

cooklang format rounds every quantity to 3 decimal places

2 participants