Repository navigation
fix(filters): round must not alter large or high-precision numbers - #980
Merged
Merged
Conversation
round multiplied the scaled value by (1 + Number.EPSILON) to make 1.005 round to 1.01. That factor shifts the value by |value * 10^digits| * 2^-52, which is a quarter at 2^50 and a whole unit at 2^52, so round: 0 changed 1700000000000000 into 1700000000000001 and round: 6 changed 1700000000.123456 into 1700000000.123457. Shift the decimal exponent through the number's string form instead, so 1.005 and 9.075 still round up, and return the input unchanged when the scaled value is beyond the safe integer range.
|
+1 from a downstream user of LiquidJS. Prices and IDs as large numbers are common in storefront themes, and a round that changes an already-integer value is a nasty surprise. The table of before/after cases makes this easy to review; worth keeping those as test cases. |
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 28, 2026
## [10.30.1](v10.30.0...v10.30.1) (2026-10-28) ### Bug Fixes * apply outputEscape to {% echo %} ([46ab5d8](46ab5d8)) * **array_to_sentence_string:** coerce non-array input with toArray ([#968](#968)) ([e843466](e843466)), closes [#964](#964) * **base64_decode:** throw on invalid input in every build ([#971](#971)) ([83bbfe0](83bbfe0)), closes [#965](#965) * **date_to_rfc822:** use English day and month names ([#974](#974)) ([e94a1c4](e94a1c4)), closes [#973](#973) * **filters:** round must not alter large or high-precision numbers ([#980](#980)) ([ea22af4](ea22af4)), closes [hi#precision](https://github.com/hi/issues/precision) * **filters:** truncate and slice must not split surrogate pairs ([#981](#981)) ([57016eb](57016eb)), closes [String#length](https://github.com/String/issues/length) [String#slice](https://github.com/String/issues/slice) * pad %Y to 4 digits like Ruby ([#978](#978)) ([95a2eed](95a2eed)) * **security:** apply outputEscape to {% cycle %} ([#984](#984)) ([2e1ebe0](2e1ebe0))
|
🎉 This PR is included in version 10.30.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
roundmultiplies the scaled value by1 + Number.EPSILON(added in #901 so that1.005 | round: 2gives1.01). That factor shifts the value by|value * 10^digits| * 2^-52, which is a quarter at 2^50 and a whole unit at 2^52, so large or high-precision inputs are changed byroundeven when they already have no more digits than requested.Output on current
master(10.30.0), before this change:{{ 1700000000000000 | round }}17000000000000011700000000000000{{ 4503599627370496 | round }}45035996273704974503599627370496{{ 9007199254740991 | round }}90071992547409929007199254740991{{ n | round }}withn = 1e211.0000000000000003e+211e+21{{ 1700000000.123456 | round: 6 }}1700000000.1234571700000000.123456{{ 1.5 | round: 20 }}1.50000000000000021.5All of these now return the expected value.
1.005,-1.005,9.075and-9.075still round as in the existing tests.The fix scales by shifting the decimal exponent of the number's string form (
1.005->1.005e2->100.5) instead of multiplying, and rounds the absolute value so halves still go away from zero. If the scaled value is beyondNumber.MAX_SAFE_INTEGERthe input is returned unchanged, since a double that large has no fractional digits left to round. This also meansInfinityis returned as is, and a very largedigitsargument no longer producesNaN.Tests are added to
test/integration/filters/math.spec.ts. With onlysrc/filters/math.tsreverted, 5 of them fail (the large integers,1e21, the negative large integer,1700000000.123456 | round: 6and1.5 | round: 20); with the change all pass.Local run:
npm run build,npm test(89 suites, 1706 passed, 4 skipped) andnpm run lintpass;npm run perf:diffreported 0.128% against 10.30.0.I compared both versions against an exact decimal-string reference on random (value, digits) pairs, bucketed by
|value * 10^digits|(one unseeded run of about 600,000 samples). Mismatches per bucket, old / new: 2^44-2^45 31/8716 vs 0; 2^47-2^48 154/7079 vs 4; 2^48-2^49 460/7713 vs 30; 2^50-2^51 1485/4663 vs 368; 2^51-2^52 4404/6298 vs 744; 2^53-2^54 2838/2869 vs 0. Below 2^41 the old code had one mismatch in about 55,000 samples and the new code had the same one. The remaining mismatches are values with 16-17 significant digits whose digits after the requested position are within one ULP of a tie (for example4085175.4959549997 | round: 5gives4085175.49596); the old code does the same and I did not try to fix that here.Not tested or not changed:
ceilandfloordo not share this code and are unchanged.digitsargument (e.g.round: 1.5) now returns the input unrounded; before, it usedMath.pow(10, 1.5)as the multiplier. I did not check what Shopify's Liquid does there.