fix(SparseMatrix): handle boolean and bigint zero values correctly#3672
Open
JSap0914 wants to merge 1 commit into
Open
fix(SparseMatrix): handle boolean and bigint zero values correctly#3672JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
SparseMatrix used typed.convert(0, datatype) to determine the zero element
for a given datatype. This throws for 'boolean' ('There are no conversions
to boolean defined') and 'bigint' ('Cannot convert 0 to bigint').
Additionally, _toArray (used by valueOf() / toArray()) filled every position
with the JS literal 0 regardless of the matrix's datatype, so even if
construction had succeeded the un-stored 'false' positions would be returned
as 0 instead of false.
Fix:
- Add _zeroValue(datatype) helper: tries typed.convert(0, datatype) and falls
back to false for 'boolean' and 0n for 'bigint'.
- Replace all five typed.convert(0, …) call sites with _zeroValue.
- Pass the datatype-specific zero to _toArray from toArray() and valueOf().
- Accept an optional zero parameter in _toArray (default 0) for fill.
Fixes josdejong#3609
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.
Closes #3609
Bug
SparseMatrixcallstyped.convert(0, datatype)to find the zero element for its datatype. For'boolean'and'bigint'no such conversion is registered, so construction throws immediately:Additionally,
_toArray(used byvalueOf()/toArray()) initialises every position with the JS literal0, so even if construction succeeded the un-storedfalsepositions would be returned as0rather thanfalse.Fix
_zeroValue(datatype)helper: callstyped.convert(0, datatype)and falls back tofalsefor'boolean'and0nfor'bigint'.typed.convert(0, …)call sites inSparseMatrix.jswith_zeroValue.toArray()andvalueOf()now pass the datatype-specific zero into_toArray._toArrayaccepts an optionalzeroparameter (default0) used for array initialisation.Verification
sparse([[true,false]],'boolean').valueOf()[[true, false]]sparse([[1n,0n,2n]],'bigint').valueOf()[[1n, 0n, 2n]]No existing tests were modified.