Repository navigation
fix(encoder): round in FloatPrecisionCodec instead of truncating - #27
Merged
Merged
Conversation
data * factor is not exact in floating point: 0.29 * 100 is 28.999999999999996, which the int codec truncated to 28, so 0.29 decoded as 0.28. Both JS and Go now round to the nearest integer, halves away from zero, so a JS and a Go sender produce the same bytes.
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.
Stacked on #26: the base is
feat/js-codec-cursor, so this diff only shows the rounding fix. Merge #26 first, then this one, and both go out in v4.0.0.Problem
FloatPrecisionCodecmultiplies by10^precisionand hands the result to an integer codec, which truncates it. Floating-point multiplication is not exact, so values land just below the integer and lose one unit:value * 1000.2928.999999999999996280.280.5756.99999999999999560.56-0.29-28.999999999999996-28-0.28JS (
DataView.setInt8) and Go (T(c.factor*data)) both truncate.Fix
Round to the nearest integer before encoding, on both sides:
value < 0 ? -Math.round(-value) : Math.round(value). JSMath.roundrounds halves up (-12.5→-12), so the sign split makes halves round away from zero, like Go.T(math.Round(float64(c.factor * data))).JS and Go now produce identical bytes. I checked 14 values:
±0.29,±0.125(exact halves),±2.675,±1.005,123.455,±0.015, and the int16 limits327.67/-327.68.The wire format is unchanged. Only the values sent change: they're now rounded instead of truncated.
Performance
About 1.3 ns per encoded value (≈ 6.4 ns → 7.7 ns in a micro-benchmark, with the same call shape for every variant). The ternary was as fast as
Math.sign(v) * Math.round(Math.abs(v))and plainMath.round, and it's the only one of the three that matches Go.Test plan
±0.29,±0.57,±0.125→±0.13,1.27,-1.28. They fail before the fix (0.29→0.28) and pass after.npm test: 93 passed.go test ./go/...passes.npm run lint,go vet