Skip to content

Commit

Permalink
fix: #1455 implicit multiplication of a fraction with unit 'in' is in…
Browse files Browse the repository at this point in the history
…correct (#3315)
  • Loading branch information
nkumawat34 authored Nov 13, 2024
1 parent ab2bc16 commit f6a0657
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit f6a0657

Please sign in to comment.