Skip to content

Commit 88faac7

Browse files
authored
Fixed constants not being reduced
Constants were reinserted in group CB during multiplication. This was noticed in the case of nerdamer('sqrt(2)*sqrt(3)*sqrt(8)')
1 parent 0ce8044 commit 88faac7

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

nerdamer.core.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,13 +1550,20 @@ var nerdamer = (function(imports) {
15501550
if(existing) {
15511551
//remove because the symbol may have changed
15521552
symbol = _.multiply(remove(this.symbols, key), symbol);
1553+
if(symbol.isConstant()) {
1554+
this.multiplier = this.multiplier.multiply(symbol.multiplier);
1555+
symbol = new Symbol(1); //the dirty work gets done down the line when it detects 1
1556+
}
1557+
// if(this.group === CB && symbol.isConstant()) {
1558+
// remove(this.symbols, key);
1559+
// this.multiplier = this.multiplier.multiply(symbol.multiplier);
1560+
// this.length--;
1561+
// }
15531562
if(this.length === 0) this.convert(N);
15541563
this.length--;
15551564
//clean up
15561565
}
15571566

1558-
//move the multiplier to the upper symbol
1559-
15601567
//don't insert the symbol if it's 1
15611568
if(!symbol.isOne(true)) {
15621569
this.symbols[key] = symbol;
@@ -1566,7 +1573,7 @@ var nerdamer = (function(imports) {
15661573
this.negate(); //put back the sign
15671574
}
15681575
}
1569-
1576+
15701577
//update the hash
15711578
if(this.group === CP || this.group === CB) {
15721579
this.updateHash();
@@ -3108,7 +3115,7 @@ var nerdamer = (function(imports) {
31083115
if(a.group === CB && b.group === PL && a.value === b.value) {
31093116
var t = a; a = b; b = t;//swap
31103117
}
3111-
3118+
31123119
var g1 = a.group,
31133120
g2 = b.group,
31143121
bnum = b.multiplier.num,
@@ -3173,7 +3180,6 @@ var nerdamer = (function(imports) {
31733180
//the sign for b is floating around. Remember we are assuming that the odd variable will carry
31743181
//the sign but this isn't true if they're equals symbols
31753182
result.multiplier = result.multiplier.multiply(b.multiplier);
3176-
31773183
}
31783184
else if(g1 === CB && a.isLinear()){
31793185
if(g2 === CB) b.distributeExponent();
@@ -3237,6 +3243,7 @@ var nerdamer = (function(imports) {
32373243
result.multiplier = result.multiplier.multiply(m).multiply(sign);
32383244
}
32393245

3246+
32403247
//back convert group P to a simpler group N if possible
32413248
if(result.group === P && isInt(result.power.toDecimal())) result = result.convert(N);
32423249

0 commit comments

Comments
 (0)