From d5ceea9042ff1d200c8a9bdd1aace8b2d38b590e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 31 Aug 2018 15:47:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=99poincare]=20Update=20DivisionQuotient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poincare/include/poincare/division_quotient.h | 1 - poincare/include/poincare/expression.h | 1 + poincare/src/division_quotient.cpp | 28 +++++++++++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/poincare/include/poincare/division_quotient.h b/poincare/include/poincare/division_quotient.h index 8c6365f46..4d192f41e 100644 --- a/poincare/include/poincare/division_quotient.h +++ b/poincare/include/poincare/division_quotient.h @@ -50,4 +50,3 @@ public: } #endif - diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index b29f0350d..8e9aa48e4 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -28,6 +28,7 @@ class Expression : public TreeByReference { friend class Derivative; friend class Determinant; friend class Division; + friend class DivisionQuotient; friend class Sine; friend class Tangent; diff --git a/poincare/src/division_quotient.cpp b/poincare/src/division_quotient.cpp index 8a08c0f4a..2d7d81cb7 100644 --- a/poincare/src/division_quotient.cpp +++ b/poincare/src/division_quotient.cpp @@ -38,27 +38,35 @@ Evaluation DivisionQuotientNode::templatedApproximate(Context& context, Prefe } Expression DivisionQuotient::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } Expression c0 = childAtIndex(0); Expression c1 = childAtIndex(1); #if MATRIX_EXACT_REDUCING if (c0.type() == ExpressionNode::Type::Matrix || c1.type() == ExpressionNode::Type::Matrix) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } #endif if (c0.type() == ExpressionNode::Type::Rational) { Rational r0 = static_cast(c0); if (!r0.integerDenominator().isOne()) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } } if (c1.type() == ExpressionNode::Type::Rational) { Rational r1 = static_cast(c1); if (!r1.integerDenominator().isOne()) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } } if (c0.type() != ExpressionNode::Type::Rational || c1.type() != ExpressionNode::Type::Rational) { @@ -70,11 +78,15 @@ Expression DivisionQuotient::shallowReduce(Context & context, Preferences::Angle Integer a = r0.signedIntegerNumerator(); Integer b = r1.signedIntegerNumerator(); if (b.isZero()) { - return Infinity(a.isNegative()); + Expression result = Infinity(a.isNegative()); + replaceWithInPlace(result); + return result; } Integer result = Integer::Division(a, b).quotient; assert(!result.isInfinity()); - return Rational(result); + Expression rationalResult = Rational(result); + replaceWithInPlace(rationalResult); + return rationalResult; } }