Skip to content

fix: preserve exact value of large integers on round-trip#33

Open
spokodev wants to merge 1 commit into
rgcl:mainfrom
spokodev:fix-large-integer-precision
Open

fix: preserve exact value of large integers on round-trip#33
spokodev wants to merge 1 commit into
rgcl:mainfrom
spokodev:fix-large-integer-precision

Conversation

@spokodev

Copy link
Copy Markdown

Integers are encoded with Number#toString(36) and decoded with parseInt(_, 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:

unpack(pack({ n: 1e21 })).n    // 1.0000000000000001e21, expected 1e21
unpack(pack({ n: 1e23 })).n    // 9.999999999999997e22
unpack(pack({ n: 1.5e300 })).n // 1.4999999999999992e300

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.

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

1 participant