[poincare] Implement comparison on decimal

Change-Id: I87cd42d9b1bbae0e0bb68cd2dcd6449dee9910a5
This commit is contained in:
Émilie Feral
2017-11-14 16:30:21 +01:00
parent 3bd3b964c0
commit a4e06e123f
2 changed files with 22 additions and 3 deletions

View File

@@ -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 */

View File

@@ -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<const Decimal *>(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 {