Skip to content
Open
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
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# unpublished changes since 15.2.0

- Fix: #3620 allow parentheses around exponents in unit parsing.
- Docs: fix the browser example `rocket_trajectory_optimization.html` (#3654).
Thanks @dvd101x.

Expand Down
13 changes: 13 additions & 0 deletions src/type/unit/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,24 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
skipWhitespace()
if (parseCharacter('^')) {
skipWhitespace()
const hasParentheses = parseCharacter('(')
if (hasParentheses) {
skipWhitespace()
}

const p = parseNumber()
if (p === null) {
// No valid number found for the power!
throw new SyntaxError('In "' + str + '", "^" must be followed by a floating-point number')
}

if (hasParentheses) {
skipWhitespace()
if (!parseCharacter(')')) {
throw new SyntaxError('Unmatched "(" in "' + text + '"')
}
}

power *= p
}

Expand Down
29 changes: 29 additions & 0 deletions test/unit-tests/type/unit/Unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,33 @@ describe('Unit', function () {
assert.strictEqual(unit1.units[0].power, 1)
})

it('should parse units with parenthesized powers correctly', function () {
let unit1 = math.unit('m ^ (1)')
assert.strictEqual(unit1.equals(math.unit('m')), true)
assert.strictEqual(unit1.units[0].unit.name, 'm')
assert.strictEqual(unit1.units[0].power, 1)

unit1 = Unit.parse('m^(-2)')
assert.strictEqual(unit1.units[0].unit.name, 'm')
assert.strictEqual(unit1.units[0].power, -2)

unit1 = Unit.parse('s^(0.5)')
assert.strictEqual(unit1.units[0].unit.name, 's')
assert.strictEqual(unit1.units[0].power, 0.5)

unit1 = Unit.parse('m ^ ( 2 )')
assert.strictEqual(unit1.units[0].unit.name, 'm')
assert.strictEqual(unit1.units[0].power, 2)

unit1 = Unit.parse('m^2')
assert.strictEqual(unit1.units[0].unit.name, 'm')
assert.strictEqual(unit1.units[0].power, 2)

unit1 = Unit.parse('m^-2')
assert.strictEqual(unit1.units[0].unit.name, 'm')
assert.strictEqual(unit1.units[0].power, -2)
})

it('should parse expressions with nested parentheses correctly', function () {
let unit1 = Unit.parse('8.314 kg (m^2 / (s^2 / (K^-1 / mol)))')
approxEqual(unit1.value, 8.314)
Expand Down Expand Up @@ -975,6 +1002,8 @@ describe('Unit', function () {
assert.throws(function () { Unit.parse('/meter') }, /Unexpected "\/"/)
assert.throws(function () { Unit.parse('1 */ s') }, /Unexpected "\/"/)
assert.throws(function () { Unit.parse('45 kg 34 m') }, /Unexpected "3"/)
assert.throws(function () { Unit.parse('m^()') }, SyntaxError)
assert.throws(function () { Unit.parse('m^(2') }, SyntaxError)
})

it('should throw an exception when parsing an invalid type of argument', function () {
Expand Down