Skip to content

fix(SparseMatrix): handle boolean and bigint zero values correctly#3672

Open
JSap0914 wants to merge 1 commit into
josdejong:developfrom
JSap0914:fix/sparse-boolean-zero-value
Open

fix(SparseMatrix): handle boolean and bigint zero values correctly#3672
JSap0914 wants to merge 1 commit into
josdejong:developfrom
JSap0914:fix/sparse-boolean-zero-value

Conversation

@JSap0914

Copy link
Copy Markdown

Closes #3609

Bug

SparseMatrix calls typed.convert(0, datatype) to find the zero element for its datatype. For 'boolean' and 'bigint' no such conversion is registered, so construction throws immediately:

sparse([[true, false]], 'boolean').valueOf()
// Error: There are no conversions to boolean defined.

sparse([[1n, 0n, 2n]], 'bigint').valueOf()
// Error: Cannot convert 0 to bigint

Additionally, _toArray (used by valueOf() / toArray()) initialises every position with the JS literal 0, so even if construction succeeded the un-stored false positions would be returned as 0 rather than false.

Fix

  • Added _zeroValue(datatype) helper: calls typed.convert(0, datatype) and falls back to false for 'boolean' and 0n for 'bigint'.
  • Replaced all five typed.convert(0, …) call sites in SparseMatrix.js with _zeroValue.
  • toArray() and valueOf() now pass the datatype-specific zero into _toArray.
  • _toArray accepts an optional zero parameter (default 0) used for array initialisation.

Verification

Before After
sparse([[true,false]],'boolean').valueOf() throws [[true, false]]
sparse([[1n,0n,2n]],'bigint').valueOf() throws [[1n, 0n, 2n]]
Full unit test suite 6652 passing, 3 failing 6655 passing, 0 failing

No existing tests were modified.

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
Copilot AI review requested due to automatic review settings June 28, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sparse() silently converts false entries to 0

2 participants