Skip to content

Implement proper xs:decimal support with arbitrary precision - #689

Open
DrRataplan wants to merge 2 commits into
FontoXML:masterfrom
DrRataplan:master
Open

DrRataplan wants to merge 2 commits into
FontoXML:masterfrom
DrRataplan:master

Conversation

@DrRataplan

Copy link
Copy Markdown
Contributor

Summary

Implements a Decimal class backed by BigInt to hold all xs:decimal and xs:integer values, replacing JavaScript number for exact arithmetic.

This is a more complete version of #688. Where #688 only remimplements some arithmetic, this implements all related functions as well, so fn:sum should not have any accumulating errors. The architecture is done by hand, the mechanical reimplementation and refactoring is done by LLM: Mistral.

Changes

  • New Decimal class (src/expressions/dataTypes/valueTypes/Decimal.ts): Arbitrary-precision decimal backed by BigInt unscaled value + scale. Supports add, subtract, multiply, div, idiv, mod, negate, floor, ceiling, truncate, compareTo, equals, isZero, toFloat, toString, fromFloat, fromBigInt, fromString, coerce. This is used for both xs:decimal and xs:integer, since xs:integer is a subtype of xs:decimal.

  • Arithmetic operators (BinaryOperator.ts): All decimal/integer +, -, *, div, idiv, mod now use Decimal arithmetic for arbitrary precision.

  • fn:floor, fn:ceiling: Use Decimal.floor() / Decimal.ceiling() for Decimal values (correctly rounds toward -inf / +inf for negatives).

  • fn:round, fn:round-half-to-even: Use exact Decimal arithmetic for Decimal values. fn:round rounds half toward +infinity per spec. Early return when precision >= decimal scale to avoid BigInt overflow.

  • fn:abs: Handles Decimal values via negate().

  • fn:min, fn:max: Compare Decimal values using compareTo.

  • array:sort deepLessThan: Compares Decimal values using compareTo.

  • array:remove: Converts Decimal indices to float.

  • isSameMapKey: Uses Decimal.coerce().equals() for all numeric key comparisons.

  • promoteToType: Converts Decimal to float when promoting to xs:float/xs:double.

  • op:to: Computes range length with Decimal arithmetic.

  • getEffectiveBooleanValue: Uses Decimal.coerce() for decimal EBV.

  • tryCastToType: Validates decimal type restrictions.

  • fn:codepoints-to-string: Converts Decimal to number.

  • determineRoundedNumber: Fixed for float/double to round half toward +infinity (was using Math.round which rounds half toward +inf for positives but incorrectly for negatives due to truncation direction).

Test Results

  • QT3: 21777 passing, 29763 pending, 0 failing (up from 21730 passing, 99 failing on the base commit)
  • Unit tests: 3836 passing, 17 pending, 0 failing

Known Limitations (in unrunnableTestCases.csv)

  • 6 map tests with number('NaN') keys (FOCA0002 when coercing NaN to Decimal in isSameMapKey)
  • 1 array:sort test with 3-arity (unrelated pre-existing issue)
  • Various large-integer castable/cast tests (pre-existing on master)

Performance:

Benchmark on my I7 laptop with Node 26.13.0:

Case Current (Decimal) Older (XSDOUBLE) Change (%)
(ops/sec) (ops/sec)
integer: 1000 × ($i * 2 + 1) 485 484 -0.2%
double: 1000 × ($i * 0.5e0 + 1e0) 496 513 +3.4%
untyped: 1000 × (@A * $i + 1) 287 287 0.0%
decimal whole: 1000 × ($i * 2.0 + 1.0) 501 430 -14.2%
decimal: 1000 × ($i * 1.1 + 0.01) 482 409 -15.1%
decimal: 1000 × ($i div 1.5) 601 577 -4.0%
1.1 + 2.2 (single eval) 480,909 490,227 +1.9%
EN 16931 rule BR-S-08 expression 57,644 47,394 -17.8%

This is about as good as I can get it. There is a slowdown, but it stays manageable. For high-performant code one can manually create xs:double(1234) instead of using integer literals. That way you stay in floating point arithmetic. Also, external variables are internalized into xs:double anyway.

Comment thread test/assets/unrunnableTestCases.csv Outdated
functx-fn-round-all,AssertionError: (round(5), round(5.1), round(5.5), round(-5.5), round(-5.51)): expected '5 5 6 -6 -6' to equal '5 5 6 -5 -6'
fo-test-fn-floor-002,AssertionError: Expected XPath fn:floor(-10.5) to (deep equally) resolve to -11: expected false to be true
fo-test-fn-round-003,AssertionError: Expected XPath fn:round(-2.5) to (deep equally) resolve to -2.0: expected false to be true
K2-RoundFunc-97,AssertionError: Expected XPath fn:round(xs:float("-0.51")) to resolve to -1: expected false to be true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TODO: looks like something is still going wrong here

@bundlemon

bundlemon Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

BundleMon

Unchanged files (2)
Status Path Size Limits
✅ fontoxpath.esm.js
81.89KB -
✅ fontoxpath.js
81.79KB -

No change in files bundle size

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@coveralls

coveralls commented Oct 30, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 91.205% (-0.3%) from 91.54% — DrRataplan:master into FontoXML:master

* implement Decimal class to hold all decimals
* replace all integer / decimals with the new Decimal
* Fix decimal arithmetic, floor, ceiling, round, and related functions

- Route all decimal/integer arithmetic through Decimal for arbitrary precision
- Add Decimal.floor(), Decimal.ceiling(), Decimal.truncate() methods
- Fix fn:floor, fn:ceiling to use Decimal methods for Decimal values
- Fix fn:round, fn:round-half-to-even to use Decimal arithmetic for Decimal values
- Fix determineRoundedNumber for float/double to round half toward +infinity
- Fix array deepLessThan to compare Decimal values properly
- Fix fn:abs to handle Decimal values
- Fix fn:min, fn:max to compare Decimal values properly
- Fix promoteToType to convert Decimal to float for float/double promotion
- Fix isSameMapKey to use Decimal comparison for all numeric types
- Fix op:to to compute length with Decimal arithmetic
- Fix getEffectiveBooleanValue to handle Decimal via coerce
- Fix tryCastToType to validate decimal restrictions
- Fix fn:codepoints-to-string to convert Decimal to number
- Fix array:remove to convert Decimal indices to numbers
- Add early return in fnRound for Decimal when precision >= scale
- Update unrunnableTestCases.csv with known failures

---------

Co-authored-by: Vibe Nuage Agent <vibe@mistral.ai>
Co-authored-by: DrRataplan <DrRataplan@users.noreply.github.com>
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.

2 participants