Add fixed-point factories and guards#1561
Merged
Merged
Conversation
|
This was referenced Apr 24, 2026
Member
Author
This was referenced Apr 24, 2026
Merged
Merged
BundleMonFiles updated (4)
Unchanged files (143)
Total files change +7.81KB +1.56% Final result: ✅ View report in BundleMon website ➡️ |
Contributor
|
Documentation Preview: https://kit-docs-hck5tumxo-anza-tech.vercel.app |
a7240d5 to
f32b052
Compare
f32b052 to
ab5d757
Compare
17de005 to
2ef4868
Compare
mcintyre94
approved these changes
Apr 29, 2026
2ef4868 to
f04b24e
Compare
8cf3251 to
b713946
Compare
29d9851 to
aa0861a
Compare
b713946 to
05fd721
Compare
Member
Author
Merge activity
|
05fd721 to
f60f33b
Compare
aa0861a to
b015176
Compare
This PR is part of the stack implementing the fixed-point number types proposed in #1545. It adds the first runtime code on top of the type and error scaffolding from previous PRs. Adds three curried factory families per kind (outer call validates the shape once, inner call constructs values): - `decimalFixedPoint` / `binaryFixedPoint` parse decimal strings. - `rawDecimalFixedPoint` / `rawBinaryFixedPoint` accept a pre-scaled raw bigint. - `ratioDecimalFixedPoint` / `ratioBinaryFixedPoint` construct from a `numerator / denominator`. ```ts const usdc = decimalFixedPoint('unsigned', 64, 6); usdc('42.5'); // raw === 42500000n rawDecimalFixedPoint('unsigned', 64, 6)(42500000n); // same value ratioDecimalFixedPoint('unsigned', 64, 6)(1n, 4n); // 0.25 ``` Factories default to `'strict'` rounding and throw when inputs can't be represented exactly. Pass `'floor'`, `'ceil'`, `'trunc'`, or `'round'` to opt into lossy construction. Invalid shape parameters, out-of-range raw values, zero denominators, and malformed strings throw their respective `FIXED_POINTS` error codes. All constructed values are `Object.freeze`-d. ```ts const q1_15 = binaryFixedPoint('signed', 16, 15); q1_15('0.5'); // raw === 16384n (exact) q1_15('0.1'); // throws STRICT_MODE_PRECISION_LOSS q1_15('0.1', 'round'); // raw === 3277n ``` Also adds `isBinaryFixedPoint` / `assertIsBinaryFixedPoint` and decimal equivalents. Each shape parameter is independently optional; pass `undefined` to leave a field unconstrained. `assertIs*` is the source of truth and `is*` wraps it in `try/catch`. ```ts if (isBinaryFixedPoint(value, 'signed', 16, 15)) { value satisfies BinaryFixedPoint<'signed', 16, 15>; } assertIsBinaryFixedPoint(value, 'signed'); // narrows to BinaryFixedPoint<'signed', number, number> ``` The `@solana/fixed-points` README will be written in one pass at the end of this stack, once the full public API is in place.
f60f33b to
b5dbd9e
Compare
Contributor
|
🔎💬 Inkeep AI search and chat service is syncing content for source 'Solana Kit Docs' |
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.

This PR is part of the stack implementing the fixed-point number types proposed in #1545. It adds the first runtime code on top of the type and error scaffolding from previous PRs.
Adds three curried factory families per kind (outer call validates the shape once, inner call constructs values):
decimalFixedPoint/binaryFixedPointparse decimal strings.rawDecimalFixedPoint/rawBinaryFixedPointaccept a pre-scaled raw bigint.ratioDecimalFixedPoint/ratioBinaryFixedPointconstruct from anumerator / denominator.Factories default to
'strict'rounding and throw when inputs can't be represented exactly. Pass'floor','ceil','trunc', or'round'to opt into lossy construction. Invalid shape parameters, out-of-range raw values, zero denominators, and malformed strings throw their respectiveFIXED_POINTSerror codes. All constructed values areObject.freeze-d.Also adds
isBinaryFixedPoint/assertIsBinaryFixedPointand decimal equivalents. Each shape parameter is independently optional; passundefinedto leave a field unconstrained.assertIs*is the source of truth andis*wraps it intry/catch.The
@solana/fixed-pointsREADME will be written in one pass at the end of this stack, once the full public API is in place.