mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[poincare/integer] Integer::isExtractable
This factorizes and fixes wrong isExtractable checks
This commit is contained in:
@@ -125,8 +125,10 @@ public:
|
||||
bool isZero() const { return (numberOfDigits() == 0); };
|
||||
bool isEven() const { return ((digit(0) & 1) == 0); }
|
||||
|
||||
constexpr static int k_maxExtractableInteger = 0x7FFFFFFF;
|
||||
int extractedInt() const { assert(numberOfDigits() == 0 || (numberOfDigits() <= 1 && digit(0) <= k_maxExtractableInteger)); return numberOfDigits() == 0 ? 0 : (m_negative ? -digit(0) : digit(0)); }
|
||||
bool isExtractable() const {
|
||||
return numberOfDigits() == 0 || (numberOfDigits() <= 1 && digit(0) <= k_maxExtractableInteger);
|
||||
}
|
||||
int extractedInt() const { assert(isExtractable()); return numberOfDigits() == 0 ? 0 : (m_negative ? -digit(0) : digit(0)); }
|
||||
|
||||
// Comparison
|
||||
static int NaturalOrder(const Integer & i, const Integer & j);
|
||||
@@ -152,6 +154,7 @@ public:
|
||||
constexpr static int k_maxNumberOfDigits = 32;
|
||||
private:
|
||||
constexpr static int k_maxNumberOfDigitsBase10 = 308; // (2^32)^k_maxNumberOfDigits ~ 1E308
|
||||
constexpr static int k_maxExtractableInteger = 0x7FFFFFFF;
|
||||
|
||||
// Constructors
|
||||
Integer(native_uint_t * digits, uint16_t numberOfDigits, bool negative);
|
||||
|
||||
@@ -73,7 +73,7 @@ int PowerNode::polynomialDegree(Context * context, const char * symbolName) cons
|
||||
return -1;
|
||||
}
|
||||
Integer numeratorInt = r->signedNumerator();
|
||||
if (Integer::NaturalOrder(numeratorInt, Integer(Integer::k_maxExtractableInteger)) > 0) {
|
||||
if (!numeratorInt.isExtractable()) {
|
||||
return -1;
|
||||
}
|
||||
op0Deg *= numeratorInt.extractedInt();
|
||||
@@ -360,7 +360,7 @@ int Power::getPolynomialCoefficients(Context * context, const char * symbolName,
|
||||
return -1;
|
||||
}
|
||||
Integer num = r.unsignedIntegerNumerator();
|
||||
if (Integer::NaturalOrder(num, Integer(Integer::k_maxExtractableInteger)) > 0) {
|
||||
if (!num.isExtractable()) {
|
||||
return -1;
|
||||
}
|
||||
int n = num.extractedInt();
|
||||
|
||||
@@ -350,7 +350,7 @@ void Unit::ChooseBestMultipleForValue(Expression * units, double * value, bool t
|
||||
firstFactor = firstFactor.childAtIndex(0);
|
||||
assert(exp.type() == ExpressionNode::Type::Rational && static_cast<Rational &>(exp).isInteger());
|
||||
Integer expInt = static_cast<Rational &>(exp).signedIntegerNumerator();
|
||||
if (expInt.isLowerThan(Integer(Integer::k_maxExtractableInteger))) {
|
||||
if (expInt.isExtractable()) {
|
||||
exponent = expInt.extractedInt();
|
||||
} else {
|
||||
// The exponent is too large to be extracted, so do not try to use it.
|
||||
|
||||
Reference in New Issue
Block a user