fix: read float attribute values back as numbers - #23
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Converts decoded float attribute strings into JavaScript numbers while preserving invalid or out-of-range values.
Changes:
- Coerces scalar and vector float values.
- Adds conversion and boundary tests.
- Documents returned attribute types.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Actions/Generator.ts |
Implements float coercion and range guards. |
test/attribute_map.test.ts |
Tests float conversion behavior. |
README.md |
Documents conversion contracts and release notes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const parsed = Number(value); | ||
| const rounded = float32 ? Math.fround(parsed) : parsed; | ||
|
|
||
| if (!Number.isFinite(rounded) || (rounded === 0 && parsed !== 0)) { |
Contributor
Author
There was a problem hiding this comment.
Fixed. The zero test now reads the digits ahead of the exponent instead of the parsed value, so '1e-400' and any other underflowing string keeps its string form while '0', '0.0000' and '0e10' still read as the number zero. Added float64 overflow and underflow cases plus a zero-string case to the non-finite test.
A map decoded by @wharfkit/antelope carries every float32 and float64 value as a string, because those wrappers serialize through toString. Passing such a map to convertAttributeMapToObject published a string where the schema declares a number, while the same attribute read from serialized bytes came back as a number, so one asset could hold both shapes for one field. The converter now reads a float string back into a number, and rounds a float32 to the nearest float32 so it matches the value the contract holds. Rounding is the reason the guard runs after it rather than before: a string above the float32 maximum rounds to Infinity, which has no JSON form, and one below the smallest subnormal rounds to zero, which would publish a wrong value. Both cases keep the string instead, as do an empty string and any string that is not a number. A caller that already holds numbers sees no change. The rounding recovers the contract value only when the string kept full precision. A float32 below 1 reaches JSON as seven decimal places rather than seven significant digits, so its string has already lost digits and the nearest float32 of that string is the best available answer.
robrigo
force-pushed
the
fix/float-attribute-coercion
branch
from
August 21, 2026 17:41
260d246 to
0ed9d66
Compare
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.
Why
A map decoded by
@wharfkit/antelopecarries everyfloat32andfloat64value as a string: those wrappers serialize throughtoString, andSerializer.objectifycalls it. Feeding such a map toconvertAttributeMapToObjectpublished a string where the schema declares a number, while the same attribute read from serialized bytes came back as a number, so one asset could carry both shapes for one field. The converter now reads a float string back into a number, and rounds afloat32to the nearest float32.The guard runs after the rounding rather than before it. A string above the float32 maximum rounds to
Infinity, which has no JSON form, and one below the smallest subnormal rounds to zero, which would publish a wrong value; both keep the string instead, as do an empty string and any string that is not a number. That boundary is the one the Postgresrealcast enforces, so an indexer that repairs stored strings and a consumer that calls this helper agree on which values are out of range.Rounding recovers the contract value only when the string kept full precision. A
float32below 1 reaches JSON as seven decimal places rather than seven significant digits, so its string has already lost digits and the nearest float32 of that string is the best available answer. The README says so where it documents the type contract.Validation
yarn test148 passing and 6 pending, against 142 and 6 on the base commit; the six added cases fail on the base implementation except the three that assert unchanged behaviour.yarn check-types,yarn lintandyarn buildare clean.