From 7d9c6618eb600e323b3dc00fc52a3dff9aedec7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 20 Aug 2018 11:26:07 +0200 Subject: [PATCH] [poincare] Override setSign on all Numbers --- poincare/include/poincare/decimal.h | 7 ++++++- poincare/include/poincare/infinity.h | 1 + poincare/include/poincare/undefined.h | 1 + poincare/src/decimal.cpp | 10 ++++++++++ poincare/src/infinity.cpp | 4 ++++ poincare/src/undefined.cpp | 4 ++++ 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/poincare/include/poincare/decimal.h b/poincare/include/poincare/decimal.h index 692f5ea4c..c2692a7db 100644 --- a/poincare/include/poincare/decimal.h +++ b/poincare/include/poincare/decimal.h @@ -12,7 +12,10 @@ namespace Poincare { * - native_uint_t m_mantissa[] = { 1234 } */ +class Decimal; + class DecimalNode : public NumberNode { + friend class Decimal; public: DecimalNode() : m_negative(false), @@ -44,6 +47,7 @@ public: // Properties Type type() const override { return Type::Decimal; } Sign sign() const override { return m_negative ? Sign::Negative : Sign::Positive; } + Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override; // Approximation Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { @@ -71,7 +75,7 @@ private: constexpr static int k_maxBufferSize = PrintFloat::k_numberOfStoredSignificantDigits+1+1+1+1+4+1; int convertToText(char * buffer, int bufferSize, Preferences::PrintFloatMode mode, int numberOfSignificantDigits) const; template Evaluation templatedApproximate() const; - + void setNegative(bool negative) { m_negative = negative; } bool m_negative; int m_exponent; size_t m_numberOfDigitsInMantissa; @@ -96,6 +100,7 @@ private: DecimalNode * node() const override { return static_cast(Number::node()); } template Decimal(T f); Decimal(size_t size, Integer m, int e); + Expression setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const; // Simplification Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const; Expression shallowBeautify(Context& context, Preferences::AngleUnit angleUnit) const; diff --git a/poincare/include/poincare/infinity.h b/poincare/include/poincare/infinity.h index f90d688f6..c97522c62 100644 --- a/poincare/include/poincare/infinity.h +++ b/poincare/include/poincare/infinity.h @@ -13,6 +13,7 @@ public: InfinityNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); } void setNegative(bool negative) { m_negative = negative; } + Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override; // TreeNode size_t size() const override { return sizeof(InfinityNode); } diff --git a/poincare/include/poincare/undefined.h b/poincare/include/poincare/undefined.h index e91b50120..6860489a3 100644 --- a/poincare/include/poincare/undefined.h +++ b/poincare/include/poincare/undefined.h @@ -22,6 +22,7 @@ public: // Properties Type type() const override { return Type::Undefined; } int polynomialDegree(char symbolName) const override; + Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override; // Approximation Evaluation approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override { diff --git a/poincare/src/decimal.cpp b/poincare/src/decimal.cpp index bb7a194b1..876895d1c 100644 --- a/poincare/src/decimal.cpp +++ b/poincare/src/decimal.cpp @@ -44,6 +44,10 @@ size_t DecimalNode::size() const { return sizeof(DecimalNode)+ sizeof(native_uint_t)*m_numberOfDigitsInMantissa; } +Expression DecimalNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const { + return Decimal(this).setSign(s, context, angleUnit); +} + int DecimalNode::simplificationOrderSameType(const ExpressionNode * e, bool canBeInterrupted) const { assert(e->type() == Type::Decimal); const DecimalNode * other = static_cast(e); @@ -273,6 +277,12 @@ Decimal::Decimal(size_t size, Integer m, int e) : Number(TreePool::sharedPool()- node()->setValue(m.node()->digits(), m.node()->numberOfDigits(), e, m.isNegative()); } +Expression Decimal::setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const { + Decimal result = *this; + result.node()->setNegative(s == ExpressionNode::Sign::Negative); + return result; +} + Expression Decimal::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const { Expression e = Expression::defaultShallowReduce(context, angleUnit); if (e.isUndefinedOrAllocationFailure()) { diff --git a/poincare/src/infinity.cpp b/poincare/src/infinity.cpp index 37d9b77c1..478ead8a9 100644 --- a/poincare/src/infinity.cpp +++ b/poincare/src/infinity.cpp @@ -16,6 +16,10 @@ InfinityNode * InfinityNode::FailedAllocationStaticNode() { return &failure; } +Expression InfinityNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const { + return Infinity(s == Sign::Negative); +} + LayoutRef InfinityNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { char buffer[5]; int numberOfChars = serialize(buffer, 5, floatDisplayMode, numberOfSignificantDigits); diff --git a/poincare/src/undefined.cpp b/poincare/src/undefined.cpp index 50df4fc84..c0e1de8dc 100644 --- a/poincare/src/undefined.cpp +++ b/poincare/src/undefined.cpp @@ -19,6 +19,10 @@ int UndefinedNode::polynomialDegree(char symbolName) const { return -1; } +Expression UndefinedNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const { + return Undefined(); +} + LayoutRef UndefinedNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { char buffer[6]; int numberOfChars = PrintFloat::convertFloatToText(NAN, buffer, 6, numberOfSignificantDigits, floatDisplayMode);