From 5c82e6367423164fd1e7fa33a98fe899ba443686 Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Thu, 30 May 2019 00:25:41 -0400 Subject: [PATCH] Add parenthesis for exponents of negatives and fractions --- kas.js | 5 ++++- src/nodes.js | 5 ++++- test.html | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/kas.js b/kas.js index d5e6610..542e17d 100644 --- a/kas.js +++ b/kas.js @@ -2976,7 +2976,10 @@ _.extend(Pow.prototype, { print: function() { var base = this.base.print(); - if (this.base instanceof Seq || this.base instanceof Pow) { + if (this.base instanceof Seq || + this.base instanceof Pow || + this.base instanceof Rational && !this.base.isSimple() || + this.base instanceof Num && this.base.isNegative()) { base = "(" + base + ")"; } return base + "^(" + this.exp.print() + ")"; diff --git a/src/nodes.js b/src/nodes.js index 3bd3af4..0bf1d58 100644 --- a/src/nodes.js +++ b/src/nodes.js @@ -1443,7 +1443,10 @@ _.extend(Pow.prototype, { print: function() { var base = this.base.print(); - if (this.base instanceof Seq || this.base instanceof Pow) { + if (this.base instanceof Seq || + this.base instanceof Pow || + this.base instanceof Rational && !this.base.isSimple() || + this.base instanceof Num && this.base.isNegative()) { base = "(" + base + ")"; } return base + "^(" + this.exp.print() + ")"; diff --git a/test.html b/test.html index 6efcd03..0782d51 100644 --- a/test.html +++ b/test.html @@ -224,6 +224,8 @@

print(assert, "x^{a}", "x^(a)"); print(assert, "x^{ab}", "x^(a*b)"); + print(assert, "(2/5)^2", "(2/5)^(2)"); + print(assert, "(-3)^2", "(-3)^(2)"); }); QUnit.test("square root", function(assert) {