Repository navigation
Implement proper xs:decimal support with arbitrary precision - #689
Open
DrRataplan wants to merge 2 commits into
Open
DrRataplan wants to merge 2 commits into
DrRataplan wants to merge 2 commits into
Conversation
DrRataplan
commented
Oct 30, 2026
| 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 |
Contributor
Author
There was a problem hiding this comment.
TODO: looks like something is still going wrong here
BundleMonUnchanged files (2)
No change in files bundle size Final result: ✅ View report in BundleMon website ➡️ |
* 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>
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.
Summary
Implements a
Decimalclass backed by BigInt to hold allxs:decimalandxs:integervalues, replacing JavaScriptnumberfor 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
Decimalclass (src/expressions/dataTypes/valueTypes/Decimal.ts): Arbitrary-precision decimal backed by BigInt unscaled value + scale. Supportsadd,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,modnow 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:roundrounds 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.roundwhich rounds half toward +inf for positives but incorrectly for negatives due to truncation direction).Test Results
Known Limitations (in unrunnableTestCases.csv)
number('NaN')keys (FOCA0002 when coercing NaN to Decimal in isSameMapKey)Performance:
Benchmark on my I7 laptop with Node 26.13.0:
$i * 2 + 1)$i * 0.5e0 + 1e0)@A * $i + 1)$i * 2.0 + 1.0)$i * 1.1 + 0.01)$i div 1.5)1.1 + 2.2(single eval)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 intoxs:doubleanyway.