Skip to content

fix: allow parentheses around the exponent in the unit parser (unit(\m ^ (1)\)) - #3681

Open
mvanhorn wants to merge 2 commits into
josdejong:developfrom
mvanhorn:fix/3620-fix-unit-parser-bracket-exponent
Open

mvanhorn wants to merge 2 commits into
josdejong:developfrom
mvanhorn:fix/3620-fix-unit-parser-bracket-exponent

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Jul 9, 2026

Copy link
Copy Markdown

Summary

Allow parentheses around the exponent in the unit parser (unit("m ^ (1)")).

Why this matters

unit("m ^ (1)") throws SyntaxError: In "km ^ (1)", "^" must be followed by a floating-point number. The internal Unit.parse parser (src/type/unit/Unit.js) already supports parentheses for grouping units (e.g. (m/s)^2), but it does not accept parentheses wrapping the exponent that follows a ^. When it encounters ^ (, parseNumber() returns null (a ( is not a digit/dot/sign) and the parser throws. Owner josdejong explicitly blessed a short-term fix on 2026-01-07: "if adding bracket support to the unit parser is simple to do I think we can do that, otherwise ... improve the error message." This is the simple bracket-support path.

Testing

Changes are scoped to the files named in the fix; added or updated tests where the project has a suite, and matched existing conventions.

Fixes #3620

…t-parser-bracket-exponent

# Conflicts:
#	HISTORY.md
@mvanhorn

Copy link
Copy Markdown
Author

Merged the latest develop into this branch (946d8c1) to clear the conflict. The only conflict was in HISTORY.md; I kept the new upstream entries and the #3620 line. The Unit tests (158) and the parse tests (324) pass locally, and eslint is clean on the touched files.

@gwhitney

gwhitney commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks so much for your interest in and contribution to mathjs. I'm not in a position to judge what's the best line to draw on what to implement in the Unit parser, given the architecture issues Jos discussed in #3620. For example, mathjs would accept nested parentheses in the exponent (m ^ ((2))) but it's not clear it makes sense for the Unit parser to. Also, your statement that the unit parser accepts (m/s)^2 is false -- in fact, we just merged a PR that documents that exponents must be applied to units individually, not to parenthesized subexpressions, in the Unit parser. So it might now seem more inconsistent that we would allow parentheses in the exponent.

So, to set realistic expectations: there won't be any action on this PR until Jos is able to review it. Given the things that are occupying him at the moment, it's difficult to predict when that might be. Thanks for your understanding.

This branch has not been deployed

No deployments
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.

unit("m ^ (1)") throws an error

2 participants