fix: preserve exact value of large integers on round-trip#33
Open
spokodev wants to merge 1 commit into
Open
Conversation
Integers are encoded via Number#toString(36) and decoded via
parseInt(_, 36); both accumulate in a double, so integers beyond 2^53 lose
precision and break the documented round-trip contract:
unpack(pack({n: 1e21})).n // 1.0000000000000001e21, not 1e21
Route the integer codec through BigInt, which is exact. Dictionary indices
are small, so the BigInt path produces identical output for them, and the
float path (String(item)) is untouched. Added a round-trip test for values
beyond 2^53.
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.
Integers are encoded with
Number#toString(36)and decoded withparseInt(_, 36). Both accumulate in a double, so integers beyond 2^53 lose precision and the documented round-trip ("Returns the original value") breaks, even though plain JSON preserves these values:The integer codec is routed through
BigInt, which represents these values exactly. Dictionary indices are small, so the BigInt path produces identical output for them (no format change for existing data), and the float path (String(item)) is untouched. Added a round-trip test for values beyond 2^53; full suite passes.