Skip to content

Fix JSON decoder bug with small floats like 0.0000040357127006276845#47

Merged
streamich merged 2 commits intomasterfrom
copilot/fix-46
Aug 8, 2025
Merged

Fix JSON decoder bug with small floats like 0.0000040357127006276845#47
streamich merged 2 commits intomasterfrom
copilot/fix-46

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 8, 2025

The JsonDecoder was incorrectly handling small floats with exactly 24 characters due to a typo in the readNum() method. When parsing numbers character by character, the decoder was missing c23 and duplicating c24 in the fromCharCode call, causing precision loss.

Problem:

const decoder = new JsonDecoder();
const smallFloat = 0.0000040357127006276845;
const decoded = decoder.decode(Buffer.from(JSON.stringify(smallFloat), 'utf-8'));

console.log(smallFloat);  // 0.0000040357127006276845
console.log(decoded);     // 0.000004035712700627685 (incorrect - lost precision)

Root Cause:
In JsonDecoder.ts line 587, the fromCharCode call was missing c23:

// Before (incorrect)
const num = +fromCharCode(c1, c2, ..., c22, c24);  // missing c23!

// After (fixed)  
const num = +fromCharCode(c1, c2, ..., c22, c23, c24);

Fix:

  • Corrected the typo to include both c23 and c24 in the proper order
  • Added test case to prevent regression
  • Verified all 2892 existing tests still pass

After the fix:

console.log(decoded);  // 0.0000040357127006276845 (correct!)

Fixes #46.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] JSON codec bug with small floats Fix JSON decoder bug with small floats like 0.0000040357127006276845 Aug 8, 2025
Copilot AI requested a review from streamich August 8, 2025 17:38
@streamich streamich marked this pull request as ready for review August 8, 2025 17:40
Copy link
Copy Markdown
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

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

LGTM

@streamich streamich merged commit 1c28fed into master Aug 8, 2025
4 checks passed
@streamich streamich deleted the copilot/fix-46 branch August 8, 2025 17:41
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 8, 2025

🎉 This PR is included in version 1.9.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.

JSON codec bug with small floats

2 participants