Fix: Prevent float precision loss for large integers in MessagePack encoding#18
Merged
jinzhongjia merged 3 commits intozigcc:mainfrom Mar 31, 2026
Merged
Fix: Prevent float precision loss for large integers in MessagePack encoding#18jinzhongjia merged 3 commits intozigcc:mainfrom
jinzhongjia merged 3 commits intozigcc:mainfrom
Conversation
Refactor writeFloat function to improve float encoding logic.
There was a problem hiding this comment.
Code Review
This pull request updates the writeFloat function in src/msgpack.zig to ensure f64 values are only encoded as f32 when there is no loss of precision. It also adds a test case in src/test.zig to verify that large integers are correctly encoded as f64. I have no feedback to provide.
Member
|
oh, nice pr, thx |
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.
Problem
Large floating-point numbers (e.g.,
1774904202196) were being truncated when encoded as MessagePack floats. This happened because the encoder would downgrade to 32-bit float (f32) encoding, which only supports 24 bits of mantissa precision. Values requiring more than 24 bits would lose precision.Solution
Refactored the
writeFloat()function to intelligently choose the encoding format:This ensures large integers preserve their exact value while still optimizing for smaller numbers.
Changes
writeFloat()to detect precision losslarge integer float precision (issue fix)testTesting
The new test demonstrates:
1774904202196is preserved exactlyExample