Skip to content

fix(filters): round must not alter large or high-precision numbers - #980

Merged
harttle merged 1 commit into
harttle:masterfrom
OsamaAnsar:fix/round-large-numbers
Oct 27, 2026
Merged

harttle merged 1 commit into
harttle:masterfrom
OsamaAnsar:fix/round-large-numbers

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

round multiplies the scaled value by 1 + Number.EPSILON (added in #901 so that 1.005 | round: 2 gives 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 large or high-precision inputs are changed by round even when they already have no more digits than requested.

Output on current master (10.30.0), before this change:

template before expected
{{ 1700000000000000 | round }} 1700000000000001 1700000000000000
{{ 4503599627370496 | round }} 4503599627370497 4503599627370496
{{ 9007199254740991 | round }} 9007199254740992 9007199254740991
{{ n | round }} with n = 1e21 1.0000000000000003e+21 1e+21
{{ 1700000000.123456 | round: 6 }} 1700000000.123457 1700000000.123456
{{ 1.5 | round: 20 }} 1.5000000000000002 1.5

All of these now return the expected value. 1.005, -1.005, 9.075 and -9.075 still 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 beyond Number.MAX_SAFE_INTEGER the input is returned unchanged, since a double that large has no fractional digits left to round. This also means Infinity is returned as is, and a very large digits argument no longer produces NaN.

Tests are added to test/integration/filters/math.spec.ts. With only src/filters/math.ts reverted, 5 of them fail (the large integers, 1e21, the negative large integer, 1700000000.123456 | round: 6 and 1.5 | round: 20); with the change all pass.

Local run: npm run build, npm test (89 suites, 1706 passed, 4 skipped) and npm run lint pass; npm run perf:diff reported 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 example 4085175.4959549997 | round: 5 gives 4085175.49596); the old code does the same and I did not try to fix that here.

Not tested or not changed:

  • ceil and floor do not share this code and are unchanged.
  • A non-integer digits argument (e.g. round: 1.5) now returns the input unrounded; before, it used Math.pow(10, 1.5) as the multiplier. I did not check what Shopify's Liquid does there.
  • I only ran the Node build and Jest; I did not run the browser bundles or the docs build.

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

Copy link
Copy Markdown

+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.

@harttle
harttle merged commit ea22af4 into harttle:master Oct 27, 2026
14 checks passed
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))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 10.30.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants