Repository navigation
fix(dart/core): preserve large numbers in formatString - #4196
leoafarias wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request fixes an issue in formatString where whole doubles outside the signed 64-bit range were being clamped. It updates _numberToString and _normalizeNumbers in function_support.dart to ensure that only integral doubles within the signed 64-bit range are converted to integers, while larger values remain as doubles to prevent clamping. Additionally, comprehensive tests have been added to verify this behavior across various large number boundaries and structures. I have no further feedback to provide.
|
Hi @leoafarias, thank you for raising this PR and we verified that it fixes large number formatting in formatString. |
|
From triage bot, analysis: For humansTL;DR Summary: Targeted issues: Suggestion: The PR meets the review criteria:
#4230, by the same author, asks to export the conversion that
Reply:
To adjust the analysis and apply its suggestion, run in your a2ui checkout: tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4196For softwareThis analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision. 🤖 Agent-generated |
|
Thanks for the fix and the tests. This PR now has merge conflicts with |
Resolve the function_support.dart conflict with a2ui-project#4305's reformat and move the CHANGELOG entry under Unreleased, since 0.8.0 has shipped.
Move the CHANGELOG entry under a new Unreleased section, since 0.9.0 has shipped.
|
Thanks @polina-c! Merged main and fixed the conflicts. I moved the changelog entry under Unreleased since 0.9.0 already shipped. Tests pass, ready for another look. |
…ke JavaScript (#4362) ## Problem The Swift SDK turns a number into text with `Double.description` in two places: `formatString` interpolation, and `NodeResolver` when it coerces a `DynamicString` bound to a number (for example a `Text` whose `text` is `{"path": "/amount"}`). Swift writes very small and very large numbers with a zero-padded exponent, so the same data renders differently than in the web renderers, which use JavaScript's `String(number)`: | Value | Swift before | Web (`String(n)`) | |---|---|---| | `0.00001` | `1e-05` | `0.00001` | | `1e-7` | `1e-07` | `1e-7` | | `1e20` | `1e+20` | `100000000000000000000` | | `2^63` | `9.223372036854776e+18` | `9223372036854776000` | The existing `Int(exactly:)` branch only covers integers below 2^63, so `1e19` and larger also get an exponent. #4196 fixes the large-integer case in Dart. ## Changes 1. Add a package-level `Double.ecmaScriptString` in `A2UICore`. It takes the shortest round-trip digits from `description` and lays them out by the ECMAScript `Number::toString` rules: no exponent for magnitudes from `1e-7` up to `1e21`, otherwise `1e+21` or `1.5e-7` style. `-0` is written as `0`, and NaN and the infinities as `NaN`, `Infinity` and `-Infinity`. 2. `FormatStringFunction` and `NodeResolver.coerceToString` use it for `.number` values. `.integer` values and the JSON output for objects and arrays don't change. 3. Tests: a parameterized `DoubleECMAScriptStringTests` (expected strings taken from Node.js, each value also checked negated), a `formatString` interpolation test, and a `NodeResolver` test for a `Text` bound to a number. 4. A `swift/CHANGELOG.md` entry. I also compared the helper against Node's `String(number)` for 200,000 doubles (random bit patterns, random values in several ranges, and decimal values with exponents from -12 to 24) and found no differences. That script is not part of the PR. The core conformance suite (`conformance/core/functions.yaml`) has no case for this, and I didn't add one: the Python core writes `1e-05` (it uses `str(float)`), and Dart needs #4196 first. Shared cases can follow once all SDKs agree. ## Pre-launch Checklist One time: - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I read the [Style Guide]. - [ ] I have built and run at least one [sample]. For this PR: - [x] I have updated the relevant CHANGELOG.md file. - [ ] I updated/added relevant documentation. - [x] My code changes (if any) have tests. - [ ] If my branch is on a fork, I have verified that [scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh) passes. If you need help, consider asking for advice on the [discussion board]. <!-- Links --> [CLA]: https://cla.developers.google.com/ [Contributors Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md [discussion board]: https://github.com/a2ui-project/a2ui/discussions [Style Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style [sample]: https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples --------- Signed-off-by: Pete Biencourt <piebie@google.com> Co-authored-by: Pete Biencourt <piebie@google.com>
Description
Fixes #4193.
formatStringprinted whole numbers of 2^63 or more as9223372036854775807on the Dart VM and Wasm._numberToStringand_normalizeNumberscalltoInt(), which clamps to the 64-bit limits, and_normalizeNumbershas no range check at all.Solution
toInt()only for whole doubles in[-2^63, 2^63).1e20prints100000000000000000000. Inside a list or map the value stays a double, sojsonEncodewrites[100000000000000000000.0], the right number with a.0._normalizeNumbersdoc comment anddart/a2ui_core/CHANGELOG.md.Verification
dart/a2ui_core/test/basic_catalog_test.dart: bare, list and object values around both 64-bit limits and1e21, plus1e300and-1e300. 26 of them fail onmain.dart testindart/a2ui_corepasses (2,398 tests), and so dodart analyze --fatal-infosandscripts/fix_format.sh --check.Pre-launch Checklist
One time:
For this PR:
I haven't run a sample or
scripts/e2e_test.sh: both need an agent backed by a live model and an API key I don't have set up here.If you need help, consider asking for advice on the discussion board.