Repository navigation
fix(format): write exact quantities in cooklang source - #736
Conversation
8fbe3e6 to
177ddb1
Compare
LeMyst
left a comment
There was a problem hiding this comment.
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 print0.3and now prints0.30000000000000004.1/3 cup× 2 used to print0.667and now prints0.6666666666666666. That's lossless but ugly forcook recipe x.cook:2 -f cooklang. One compromise: about 10 decimal places with trailing zeros trimmed. That keeps0.0005and1e-7exact and drops the noise. Up to the maintainers. - Fractions with a rounding error (
cooklang_source.rs:281). A fraction'sDisplaydrops itserrfield, so a fraction carrying an error changes value. cookcli's default converter never produces one, but a library user with fractions enabled can. Writingn.value()whenerr != 0would cover it. - Weak test (
:540).contains("0.0005")also matches inside10.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
177ddb1 to
0c7eabd
Compare
|
All three points addressed:
|
|
Thanks, all three points are covered. I ran the PR binary on 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
Closes #701
--format cooklangemittedq.value().to_string(), andNumber'sDisplayrounds to 3 decimal places:@saffron{0.0005%g}→0.001,@water{12.3456%ml}→12.346. Quantities now write throughwrite_value/write_numberinstead:Number::Regularwrites ashortest()form — thef64Display(shortest string that reparses to the same value), unless a 10-decimal rounded form is shorter and nonzero, so scaled values print0.3rather than0.30000000000000004while0.0005and1e-7stay exact;-0writes0.Number::Fractionkeeps itsDisplay(1/2,2 1/2) whenerr == 0; a fraction carrying a conversion error writesn.value()so the error is not lost.Value::Textis verbatim.Test plan
quantities_keep_their_full_precisionasserts exact@saffron{0.0005%g}/@water{12.3456%ml}/#spray bottle{0.0000001%l}and reparses to equal quantitiesscaled_quantities_do_not_print_float_noise:0.1×3 →0.3,1/3×2 →0.6666666667a_fractions_error_is_written_into_its_valuecargo test -p cooklang-formatgreenGenerated with Devin