From 551e4998f40ad538525de4ea8df9912706a9bdb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 22 Jan 2020 14:45:02 +0100 Subject: [PATCH] =?UTF-8?q?[apps/calculation][apps/solver][poincare]=20Sim?= =?UTF-8?q?plify=20sign=20=3D/=E2=89=88=20computation=20between=20outputs?= =?UTF-8?q?=20in=20Calculation/Solver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/calculation/calculation.cpp | 8 +------- apps/solver/equation_store.cpp | 3 +-- poincare/include/poincare/expression.h | 2 +- poincare/src/expression.cpp | 18 +++++++----------- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index e1d3c8636..cbc29d537 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -296,15 +296,9 @@ Calculation::EqualSign Calculation::exactAndApproximateDisplayedOutputsAreEqual( * Store in the exactOutput. */ Poincare::ExceptionCheckpoint ecp; if (ExceptionRun(ecp)) { - constexpr int bufferSize = Constant::MaxSerializedExpressionSize + 30; - char buffer[bufferSize]; Preferences * preferences = Preferences::sharedPreferences(); - Expression exactOutputExpression = PoincareHelpers::ParseAndSimplify(exactOutputText(), context, false); - if (exactOutputExpression.isUninitialized()) { - exactOutputExpression = Undefined::Builder(); - } Preferences::ComplexFormat complexFormat = Expression::UpdatedComplexFormatWithTextInput(preferences->complexFormat(), m_inputText); - m_equalSign = exactOutputExpression.isEqualToItsApproximationLayout(approximateOutput(context, NumberOfSignificantDigits::UserDefined), buffer, bufferSize, complexFormat, preferences->angleUnit(), preferences->displayMode(), preferences->numberOfSignificantDigits(), context) ? EqualSign::Equal : EqualSign::Approximation; + m_equalSign = Expression::ParsedExpressionsAreEqual(exactOutputText(), approximateOutputText(NumberOfSignificantDigits::UserDefined), context, complexFormat, preferences->angleUnit()) ? EqualSign::Equal : EqualSign::Approximation; return m_equalSign; } else { /* Do not override m_equalSign in case there is enough room in the pool diff --git a/apps/solver/equation_store.cpp b/apps/solver/equation_store.cpp index 0691c77c0..494a91c75 100644 --- a/apps/solver/equation_store.cpp +++ b/apps/solver/equation_store.cpp @@ -218,8 +218,7 @@ EquationStore::Error EquationStore::exactSolve(Poincare::Context * context) { * Dutch exam mode to display only the approximate solutions. */ m_exactSolutionIdentity[solutionIndex] = forbidExactSolutions || strcmp(exactBuffer, approximateBuffer) == 0; if (!m_exactSolutionIdentity[solutionIndex]) { - char buffer[::Constant::MaxSerializedExpressionSize]; - m_exactSolutionEquality[solutionIndex] = exactSolutions[i].isEqualToItsApproximationLayout(exactSolutionsApproximations[i], buffer, ::Constant::MaxSerializedExpressionSize, preferences->complexFormat(), preferences->angleUnit(), preferences->displayMode(), preferences->numberOfSignificantDigits(), context); + m_exactSolutionEquality[solutionIndex] = Expression::ParsedExpressionsAreEqual(exactBuffer, approximateBuffer, context, updatedComplexFormat(context), preferences->angleUnit()); } solutionIndex++; } diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 07e649b12..929aa2a59 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -210,7 +210,7 @@ public: * same structures and all their nodes have same types and values (ie, * sqrt(pi^2) is NOT identical to pi). */ bool isIdenticalTo(const Expression e) const; - bool isEqualToItsApproximationLayout(Expression approximation, char * buffer, int bufferSize, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits, Context * context); + static bool ParsedExpressionsAreEqual(const char * e0, const char * e1, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit); /* Layout Helper */ Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const; diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 16c56fe7f..ae4d3c1a1 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -522,17 +522,13 @@ bool Expression::isIdenticalTo(const Expression e) const { return ExpressionNode::SimplificationOrder(node(), e.node(), true, true) == 0; } -bool Expression::isEqualToItsApproximationLayout(Expression approximation, char * buffer, int bufferSize, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits, Context * context) { - approximation.serialize(buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits); - /* Warning: we cannot use directly the the approximate expression but we have - * to re-serialize it because the number of stored significative - * numbers and the number of displayed significative numbers might not be - * identical. (For example, 0.000025 might be displayed "0.00003" and stored - * as Decimal(0.000025) and isEqualToItsApproximationLayout should return - * false) */ - Expression approximateOutput = Expression::ParseAndSimplify(buffer, context, complexFormat, angleUnit, true); - bool equal = isIdenticalTo(approximateOutput); - return equal; +bool Expression::ParsedExpressionsAreEqual(const char * e0, const char * e1, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) { + Expression exp0 = Expression::ParseAndSimplify(e0, context, complexFormat, angleUnit, false); + Expression exp1 = Expression::ParseAndSimplify(e1, context, complexFormat, angleUnit, false); + if (exp0.isUninitialized() || exp1.isUninitialized()) { + return false; + } + return exp0.isIdenticalTo(exp1); } /* Layout Helper */