Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix:1455-Powers for unit 'in' are incorrect #3315

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/expression/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
getTokenSkipNewline(state)

// Match the "symbol" part of the pattern, or a left parenthesis
if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(') {
if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(' || state.token === 'in') {
// We've matched the pattern "number / number symbol".
// Rewind once and build the "number / number" node; the symbol will be consumed later
Object.assign(state, tokenStates.pop())
Expand Down
9 changes: 9 additions & 0 deletions test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,15 @@ describe('parse', function () {
assert.strictEqual(parseAndStringifyWithParens('8.314 J/mol K'), '(8.314 J) / (mol K)')
})

it('should handle precedence with implicit multiplication, division, and the "in" operator', function () {
assert.strictEqual(parseAndStringifyWithParens('1/2 in'), '(1 / 2) in')
assert.strictEqual(parseAndStringifyWithParens('1/2 kg'), '(1 / 2) kg')
assert.strictEqual(parseAndStringifyWithParens('3 kg in lb'), '(3 kg) in lb')
assert.strictEqual(parseAndStringifyWithParens('2 m / 1 s'), '(2 m) / (1 s)')
assert.strictEqual(parseAndStringifyWithParens('5 / 10 in'), '(5 / 10) in')
assert.strictEqual(parseAndStringifyWithParens('10 lb + 1/2 lb'), '(10 lb) + ((1 / 2) lb)')
})

it('should throw an error when having an implicit multiplication between two numbers', function () {
assert.throws(function () { math.parse('2 3') }, /Unexpected part "3"/)
assert.throws(function () { math.parse('2 * 3 4') }, /Unexpected part "4"/)
Expand Down