From 6b80fd8882debfe3dc47e8680158cf5ca77041b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 24 Oct 2017 17:18:24 +0200 Subject: [PATCH] [poincare] Repair Multiplication::immediateBeautify Change-Id: I470eaa774cbb247daf6cf5a97b8f32fcab8de1d4 --- poincare/src/multiplication.cpp | 47 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index f364eafe5..f1cb1b367 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -242,37 +242,40 @@ Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angl Expression * denominatorOperand = const_cast(p->operand(0)); p->detachOperand(denominatorOperand); removeOperand(p, true); - Multiplication * numeratorOperand = (Multiplication *)clone(); + + Expression * numeratorOperand = clone(); const Expression * divOperands[2] = {numeratorOperand, denominatorOperand}; Division * d = new Division(divOperands, false); /* We want 1/3*Pi*(ln(2))^-1 -> Pi/(3ln(2)) and not ((1/3)Pi)/ln(2)*/ - if (numeratorOperand->operand(0)->type() == Type::Rational && !static_cast(numeratorOperand->operand(0))->denominator().isOne()) { + if (numeratorOperand->operand(0)->type() == Type::Rational) { Rational * r = static_cast((Expression *)numeratorOperand->operand(0)); - if (denominatorOperand->type() == Type::Multiplication) { - const Expression * integerDenominator[1] = {new Rational(r->denominator())}; - static_cast(denominatorOperand)->addOperands(integerDenominator, 1); - static_cast(denominatorOperand)->sortChildren(); - } else { - const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()}; - Multiplication * m = new Multiplication(multOperands, 2, false); - denominatorOperand->replaceWith(m, true); - } - if ((!r->numerator().isMinusOne() && !r->numerator().isOne()) || numeratorOperand->numberOfOperands() == 1) { - numeratorOperand->replaceOperand(r, new Rational(r->numerator()), true); - } else { - if (r->numerator().isOne()) { - numeratorOperand->removeOperand(r, true); + if (!r->denominator().isOne()) { + if (denominatorOperand->type() == Type::Multiplication) { + const Expression * integerDenominator[1] = {new Rational(r->denominator())}; + static_cast(denominatorOperand)->addOperands(integerDenominator, 1); + static_cast(denominatorOperand)->sortChildren(); } else { - const Expression * oppOperand[1] = {numeratorOperand->clone()}; - Opposite * o = new Opposite(oppOperand, false); - numeratorOperand->replaceWith(o, true); + const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()}; + Multiplication * m = new Multiplication(multOperands, 2, false); + denominatorOperand->replaceWith(m, true); } } + if (!r->numerator().isMinusOne() || numeratorOperand->numberOfOperands() == 1) { + numeratorOperand->replaceOperand(r, new Rational(r->numerator()), true); + numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit); + } else { + ((Multiplication *)numeratorOperand)->removeOperand(r, true); + numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit); + const Expression * oppOperand[1] = {numeratorOperand->clone()}; + Opposite * o = new Opposite(oppOperand, false); + numeratorOperand = numeratorOperand->replaceWith(o, true); + } + } else { + numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit); } - Expression * newNumeratorOperand = numeratorOperand->squashUnaryHierarchy(); // Delete parenthesis unnecessary on numerator - if (newNumeratorOperand->type() == Type::Parenthesis) { - newNumeratorOperand->replaceWith((Expression *)newNumeratorOperand->operand(0), true); + if (numeratorOperand->type() == Type::Parenthesis) { + numeratorOperand->replaceWith((Expression *)numeratorOperand->operand(0), true); } replaceWith(d, true); return d->immediateBeautify(context, angleUnit);