diff --git a/poincare/include/poincare/decimal.h b/poincare/include/poincare/decimal.h index 0a022597a..5009bf082 100644 --- a/poincare/include/poincare/decimal.h +++ b/poincare/include/poincare/decimal.h @@ -17,10 +17,13 @@ public: static Integer mantissa(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, bool negative); Decimal(Integer mantissa, int exponent); Decimal(double f); + int exponent() const { return m_exponent; } + Integer mantissa() const { return m_mantissa; } // Expression subclassing Type type() const override; Expression * clone() const override; int writeTextInBuffer(char * buffer, int bufferSize) const override; + Sign sign() const override { return m_mantissa.isNegative() ? Sign::Negative : Sign::Positive; } private: int numberOfDigitsInMantissaWithoutSign() const; /* Comparison */ diff --git a/poincare/src/decimal.cpp b/poincare/src/decimal.cpp index 9087441a9..732b8cee4 100644 --- a/poincare/src/decimal.cpp +++ b/poincare/src/decimal.cpp @@ -228,9 +228,25 @@ Expression * Decimal::shallowBeautify(Context & context, AngleUnit angleUnit) { } int Decimal::simplificationOrderSameType(const Expression * e) const { - // We should not get there are decimal are turned into Rational before simplification - assert(false); - return 0; + assert(e->type() == Type::Decimal); + const Decimal * other = static_cast(e); + if (sign() == Sign::Negative && other->sign() == Sign::Positive) { + return -1; + } + if (sign() == Sign::Positive && other->sign() == Sign::Negative) { + return 1; + } + assert(sign() == other->sign()); + int unsignedComparison = 0; + if (exponent() < other->exponent()) { + unsignedComparison = -1; + } else if (exponent() > other->exponent()) { + unsignedComparison = 1; + } else { + assert(exponent() == other->exponent()); + unsignedComparison = Integer::NaturalOrder(mantissa(), other->mantissa()); + } + return ((int)sign())*unsignedComparison; } int Decimal::numberOfDigitsInMantissaWithoutSign() const {