From b58573bad33822817ee1b89a3968fb91622f32cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 31 Jan 2019 11:23:19 +0100 Subject: [PATCH] [poincare] Multiplication: do not apply rule 0*x --> 0 if x involves an infinite expression --- poincare/src/multiplication.cpp | 17 ++++++++++++++--- poincare/test/infinity.cpp | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index 5a2722981..0e01b53a4 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -434,13 +434,24 @@ Expression Multiplication::privateShallowReduce(Context & context, Preferences:: /* Step 5: If the first child is zero, the multiplication result is zero. We * do this after merging the rational children, because the merge takes care - * of turning 0*inf into undef. + * of turning 0*inf into undef. We still have to check that no other child + * involves an inifity expression to avoid reducing 0*e^(inf) to 0. * If the first child is 1, we remove it if there are other children. */ { const Expression c = childAtIndex(0); if (c.type() == ExpressionNode::Type::Rational && static_cast(c).isZero()) { - replaceWithInPlace(c); - return c; + // Check that other children don't match inf + bool infiniteFactor = false; + for (int i = 1; i < numberOfChildren(); i++) { + infiniteFactor = childAtIndex(i).recursivelyMatches([](const Expression e, Context & context, bool replaceSymbols) { return e.type() == ExpressionNode::Type::Infinity; }, context, true); + if (infiniteFactor) { + break; + } + } + if (!infiniteFactor) { + replaceWithInPlace(c); + return c; + } } if (c.type() == ExpressionNode::Type::Rational && static_cast(c).isOne() && numberOfChildren() > 1) { removeChildAtIndexInPlace(0); diff --git a/poincare/test/infinity.cpp b/poincare/test/infinity.cpp index aff3038ba..9eacc7a90 100644 --- a/poincare/test/infinity.cpp +++ b/poincare/test/infinity.cpp @@ -34,5 +34,7 @@ QUIZ_CASE(poincare_infinity) { assert_parsed_expression_simplify_to("(-inf)^R(2)", "inf*(-1)^R(2)"); assert_parsed_expression_simplify_to("inf^x", "inf^x"); assert_parsed_expression_simplify_to("1/inf+24", "24"); + assert_parsed_expression_simplify_to("X^(inf)/inf", "0*X^inf"); + assert_parsed_expression_simplify_to("ln(inf)*0", "0*ln(inf)"); }