Skip to content

Fix JSON decoder bug with small floats like 0.0000040357127006276845 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025

Conversation

Copilot
Copy link
Contributor

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

@Copilot 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 Copilot AI requested a review from streamich August 8, 2025 17:38
Copilot finished work on behalf of streamich August 8, 2025 17:38
@streamich streamich marked this pull request as ready for review August 8, 2025 17:40
Copy link
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
Copy link

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