From 6fd3a155df42297a1cd4ac6f5a1749f59132ecac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 20 Dec 2018 17:25:40 +0100 Subject: [PATCH] [calculation] Take into account the complex format 'real' --- apps/calculation/calculation.cpp | 7 +++++++ apps/calculation/test/calculation_store.cpp | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index 9684f13bd..a88ec8c4b 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -53,6 +53,13 @@ void Calculation::setContent(const char * c, Context * context, Expression ansEx PoincareHelpers::ParseAndSimplifyAndApproximate(m_inputText, &exactOutput, &approximateOutput, *context); PoincareHelpers::Serialize(exactOutput, m_exactOutputText, sizeof(m_exactOutputText)); PoincareHelpers::Serialize(approximateOutput, m_approximateOutputText, sizeof(m_approximateOutputText)); + /* Check ComplexFormat: if complex format is real and the input text doesn't + * contain any i complex, both approximate and exact result are set to + * Undefined if the approximate output is not a pure real.*/ + if (Preferences::sharedPreferences()->complexFormat() == Preferences::ComplexFormat::Real && strchr(m_inputText, Ion::Charset::IComplex) == nullptr && strchr(m_approximateOutputText, Ion::Charset::IComplex) != nullptr) { + strlcpy(m_exactOutputText, Undefined::Name(), Constant::MaxSerializedExpressionSize); + strlcpy(m_approximateOutputText, Undefined::Name(), Constant::MaxSerializedExpressionSize); + } } KDCoordinate Calculation::height(Context * context) { diff --git a/apps/calculation/test/calculation_store.cpp b/apps/calculation/test/calculation_store.cpp index 6e43fd542..f7679cc31 100644 --- a/apps/calculation/test/calculation_store.cpp +++ b/apps/calculation/test/calculation_store.cpp @@ -113,3 +113,24 @@ QUIZ_CASE(calculation_display_exact_approximate) { assertCalculationDisplay("3+x>f(x)", true, false, ::Calculation::Calculation::EqualSign::Unknown, "3+x", nullptr, &globalContext, &store); Ion::Storage::sharedStorage()->recordNamed("f.func").destroy(); } + +QUIZ_CASE(calculation_complex_format) { + Shared::GlobalContext globalContext; + CalculationStore store; + + Poincare::Preferences::sharedPreferences()->setComplexFormat(Poincare::Preferences::ComplexFormat::Real); + assertCalculationDisplay("1+I", false, true, ::Calculation::Calculation::EqualSign::Unknown, nullptr, "1+I", &globalContext, &store); + assertCalculationDisplay("R(-1)", true, false, ::Calculation::Calculation::EqualSign::Unknown, "undef", nullptr, &globalContext, &store); + assertCalculationDisplay("ln(-2)", true, false, ::Calculation::Calculation::EqualSign::Unknown, "undef", nullptr, &globalContext, &store); + + Poincare::Preferences::sharedPreferences()->setComplexFormat(Poincare::Preferences::ComplexFormat::Cartesian); + assertCalculationDisplay("1+I", false, true, ::Calculation::Calculation::EqualSign::Unknown, nullptr, "1+I", &globalContext, &store); + assertCalculationDisplay("R(-1)", false, true, ::Calculation::Calculation::EqualSign::Unknown, nullptr, "I", &globalContext, &store); + assertCalculationDisplay("ln(-2)", false, false, ::Calculation::Calculation::EqualSign::Approximation, "ln(-2)", nullptr, &globalContext, &store); + + Poincare::Preferences::sharedPreferences()->setComplexFormat(Poincare::Preferences::ComplexFormat::Polar); + assertCalculationDisplay("1+I", false, false, ::Calculation::Calculation::EqualSign::Approximation, "R(2)*X^(P/4*I)", nullptr, &globalContext, &store); + assertCalculationDisplay("R(-1)", false, false, ::Calculation::Calculation::EqualSign::Approximation, "X^(P/2*I)", nullptr, &globalContext, &store); + assertCalculationDisplay("ln(-2)", false, false, ::Calculation::Calculation::EqualSign::Approximation, "ln(-2)", nullptr, &globalContext, &store); + Poincare::Preferences::sharedPreferences()->setComplexFormat(Poincare::Preferences::ComplexFormat::Cartesian); +}