From 1eb673acb4e722cb58b78fdb4fc24590972f5729 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Fri, 8 Nov 2024 19:18:52 +0530 Subject: [PATCH] BugFix:1455-Powers for unit 'in' are incorrect --- src/expression/parse.js | 2 +- test/unit-tests/expression/parse.test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/expression/parse.js b/src/expression/parse.js index d45a4a9d28..c709395071 100644 --- a/src/expression/parse.js +++ b/src/expression/parse.js @@ -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()) diff --git a/test/unit-tests/expression/parse.test.js b/test/unit-tests/expression/parse.test.js index 460ff09d7e..6090c1933f 100644 --- a/test/unit-tests/expression/parse.test.js +++ b/test/unit-tests/expression/parse.test.js @@ -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"/)