diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 9e1b80255..04888d3c2 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -32,6 +32,7 @@ class Expression : public TreeByReference { friend class DivisionRemainder; friend class Equal; friend class Factor; + friend class Factorial; friend class Sine; friend class Store; diff --git a/poincare/src/factorial.cpp b/poincare/src/factorial.cpp index 772bb5c8e..f9131c600 100644 --- a/poincare/src/factorial.cpp +++ b/poincare/src/factorial.cpp @@ -81,9 +81,11 @@ int FactorialNode::serialize(char * buffer, int bufferSize, Preferences::PrintFl } Expression Factorial::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; + } } #if MATRIX_EXACT_REDUCING if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { @@ -93,19 +95,24 @@ Expression Factorial::shallowReduce(Context & context, Preferences::AngleUnit an if (childAtIndex(0).type() == ExpressionNode::Type::Rational) { Rational r = static_cast(childAtIndex(0)); if (!r.integerDenominator().isOne() || r.sign() == ExpressionNode::Sign::Negative) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } if (Integer(k_maxOperandValue).isLowerThan(r.unsignedIntegerNumerator())) { return *this; } Rational fact = Rational(Integer::Factorial(r.unsignedIntegerNumerator())); assert(!fact.numeratorOrDenominatorIsInfinity()); // because fact < k_maxOperandValue! + replaceWithInPlace(fact); return fact; } if (childAtIndex(0).type() == ExpressionNode::Type::Symbol) { Symbol s = static_cast(childAtIndex(0)); if (s.name() == Ion::Charset::SmallPi || s.name() == Ion::Charset::Exponential) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } } return *this; @@ -117,7 +124,9 @@ Expression Factorial::shallowBeautify(Context & context, Preferences::AngleUnit || childAtIndex(0).type() == ExpressionNode::Type::Multiplication || childAtIndex(0).type() == ExpressionNode::Type::Power) { - return Factorial(Parenthesis(childAtIndex(0))); + Expression result = Factorial(Parenthesis(childAtIndex(0))); + replaceWithInPlace(result); + return result; } return *this; }