Skip to content

Cannot index Array/Matrix with bigint #3360

@gwhitney

Description

@gwhitney

Describe the bug
A bigint value is rejected as an Array or Matrix index in mathjs expressions. This ban is particularly onerous when the number default is bigint.

To Reproduce

const math = create(all, {number: 'bigint'}
console.log(math.evaluate('[4,5,6][2]')

Rather than logging 5n, this throws an error with message TypeError: Dimension must be an Array, Matrix, number, string, or Range

Discussion
While I understand that JavaScript does not allow bigint array indices, the mathjs expression language aspires to mathematical accuracy and logic. From such a perspective, there is no reason to disparage bigint indices; in fact, they are in some sense more natural, as array/matrix indices must be integers, and bigints only represent integers. Furthermore, when doing high-precision integer arithmetic, the {number: 'bigint'} configuration is very useful, but it is currently practically very cumbersome to use because of the need to convert indices to number explicitly.

Recommendation
Indexing with bigints be supported. Unless there is any objection, I will develop a PR to this effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:expressionsIssues about the expression parser, variable scoping etc.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions