Skip to content

Commit f6a0657

Browse files
authored
fix: #1455 implicit multiplication of a fraction with unit 'in' is incorrect (#3315)
1 parent ab2bc16 commit f6a0657

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/expression/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
11221122
getTokenSkipNewline(state)
11231123

11241124
// Match the "symbol" part of the pattern, or a left parenthesis
1125-
if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(') {
1125+
if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(' || state.token === 'in') {
11261126
// We've matched the pattern "number / number symbol".
11271127
// Rewind once and build the "number / number" node; the symbol will be consumed later
11281128
Object.assign(state, tokenStates.pop())

test/unit-tests/expression/parse.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,15 @@ describe('parse', function () {
14801480
assert.strictEqual(parseAndStringifyWithParens('8.314 J/mol K'), '(8.314 J) / (mol K)')
14811481
})
14821482

1483+
it('should handle precedence with implicit multiplication, division, and the "in" operator', function () {
1484+
assert.strictEqual(parseAndStringifyWithParens('1/2 in'), '(1 / 2) in')
1485+
assert.strictEqual(parseAndStringifyWithParens('1/2 kg'), '(1 / 2) kg')
1486+
assert.strictEqual(parseAndStringifyWithParens('3 kg in lb'), '(3 kg) in lb')
1487+
assert.strictEqual(parseAndStringifyWithParens('2 m / 1 s'), '(2 m) / (1 s)')
1488+
assert.strictEqual(parseAndStringifyWithParens('5 / 10 in'), '(5 / 10) in')
1489+
assert.strictEqual(parseAndStringifyWithParens('10 lb + 1/2 lb'), '(10 lb) + ((1 / 2) lb)')
1490+
})
1491+
14831492
it('should throw an error when having an implicit multiplication between two numbers', function () {
14841493
assert.throws(function () { math.parse('2 3') }, /Unexpected part "3"/)
14851494
assert.throws(function () { math.parse('2 * 3 4') }, /Unexpected part "4"/)

0 commit comments

Comments
 (0)