[poincare] Unit: comparison can rely on the pointer addresses only when

the object are ordered in a table. Otherwise, the compiler is free to
order them as it wants.

This fixes the test on the device: 10_m^2→_mm×_km simplifies to 10×_km×_mm
This commit is contained in:
Émilie Feral
2020-06-04 14:51:09 +02:00
parent 6abc3b8c99
commit 9556ae3aa4
2 changed files with 4 additions and 2 deletions

View File

@@ -421,7 +421,7 @@ public:
Representative::Prefixable::Yes,
NegativePrefixes),
};
// TODO: find a better way to find defines these pointers
// TODO: find a better way to define these pointers
static_assert(sizeof(TimeRepresentatives)/sizeof(Representative) == 7, "The Unit::SecondRepresentative, Unit::HourRepresentative and so on might require to be fixed if the TimeRepresentatives table was changed.");
static const Representative constexpr * SecondRepresentative = &TimeRepresentatives[0];
static const Representative constexpr * MinuteRepresentative = &TimeRepresentatives[1];

View File

@@ -179,10 +179,12 @@ int UnitNode::simplificationOrderSameType(const ExpressionNode * e, bool ascendi
}
assert(type() == e->type());
const UnitNode * eNode = static_cast<const UnitNode *>(e);
// This works because dimensions are ordered in a table
const ptrdiff_t dimdiff = eNode->dimension() - m_dimension;
if (dimdiff != 0) {
return dimdiff;
}
// This works because reprensentatives are ordered in a table
const ptrdiff_t repdiff = eNode->representative() - m_representative;
if (repdiff != 0) {
/* We order representatives in the reverse order as how they're stored in
@@ -190,7 +192,7 @@ int UnitNode::simplificationOrderSameType(const ExpressionNode * e, bool ascendi
* year + month + days + hours + minutes + seconds. */
return -repdiff;
}
const ptrdiff_t prediff = eNode->prefix() - m_prefix;
const ptrdiff_t prediff = eNode->prefix()->exponent() - m_prefix->exponent();
return prediff;
}