diff --git a/poincare/include/poincare/unit.h b/poincare/include/poincare/unit.h index 2c06c898a..2f2340074 100644 --- a/poincare/include/poincare/unit.h +++ b/poincare/include/poincare/unit.h @@ -11,9 +11,6 @@ public: /* The units having the same physical dimension are grouped together. * Each such group has a standard representative with a standard prefix. * - * A standard unit is a derived unit, when defined from base units - * or otherwise a base unit (if no definition is provided). - * * Each representative has * - a root symbol * - a definition @@ -22,6 +19,9 @@ public: * allowed for that representative, one may get a symbol and an Expression. */ + // There are 7 base units from which all other units are derived. + static constexpr size_t NumberOfBaseUnits = 7; + class Prefix { public: constexpr Prefix(const char * symbol, int8_t exponent) : @@ -74,7 +74,7 @@ public: class Dimension { public: template - using Vector = T[7]; + using Vector = T[NumberOfBaseUnits]; template constexpr Dimension(const Representative (&representatives)[N], const Prefix * stdRepresentativePrefix) : m_representatives(representatives), @@ -220,6 +220,7 @@ public: GigaPrefix, TeraPrefix, }; + static constexpr size_t NumberOfBaseUnits = UnitNode::NumberOfBaseUnits; static constexpr const Representative TimeRepresentatives[] = { Representative("s", nullptr, diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index af16b61ff..6292243cc 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -303,14 +303,14 @@ Expression Multiplication::shallowReduce(ExpressionNode::ReductionContext reduct } static void ExponentsCopy(Unit::Dimension::Vector &dst, const Unit::Dimension::Vector &src) { - for (int i = 0; i < 7; i++) { + for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) { dst[i] = src[i]; } } static void ExponentsMetrics(const Unit::Dimension::Vector &exponents, size_t & supportSize, Integer & norm) { assert(supportSize == 0 && norm.isZero()); - for (int i = 0; i < 7; i++) { + for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) { Integer unsignedExponent = exponents[i]; unsignedExponent.setNegative(false); if (!unsignedExponent.isZero()) { @@ -346,7 +346,7 @@ static void ExponentsOfBaseUnits(const Expression units, Unit::Dimension::Vector // Fill the exponents array with the unit's exponent const int indexInTable = static_cast(factor).dimension() - Unit::DimensionTable; - assert(0 <= indexInTable && indexInTable < 7); + assert(0 <= indexInTable && indexInTable < Unit::NumberOfBaseUnits); exponents[indexInTable] = exponent; } } @@ -368,7 +368,7 @@ static bool CanSimplifyUnitProduct( Integer(0), Integer(0), }; - for (int i = 0; i < 7; i++) { + for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) { simplifiedExponents[i] = operationOnExponents(unitsExponents[i], entryUnitExponents[i]); } size_t simplifiedSupportSize = 0; @@ -445,7 +445,7 @@ Expression Multiplication::shallowBeautify(ExpressionNode::ReductionContext redu Integer bestUnitNorm(0); size_t bestRemainderSupportSize = unitsSupportSize - 1; Integer bestRemainderNorm = unitsNorm; - for (const Unit::Dimension * dim = Unit::DimensionTable + 7; dim < Unit::DimensionTableUpperBound; dim++) { + for (const Unit::Dimension * dim = Unit::DimensionTable + Unit::NumberOfBaseUnits; dim < Unit::DimensionTableUpperBound; dim++) { Unit entryUnit = Unit::Builder(dim, dim->stdRepresentative(), dim->stdRepresentativePrefix()); Unit::Dimension::Vector entryUnitExponents = { Integer(0),