[poincare] Add a method isPositive on expressions

Change-Id: I3eb0064f8d64678326e74216517e0104eaa007fe
This commit is contained in:
Émilie Feral
2017-10-06 17:06:23 +02:00
parent 6524ab9286
commit d855ee8364
5 changed files with 16 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ class AbsoluteValue : public StaticHierarchy<1> {
public:
Type type() const override;
Expression * clone() const override;
bool isPositive() const override { return true; }
private:
template<typename T> static Complex<T> computeOnComplex(const Complex<T> c, AngleUnit angleUnit);
virtual Evaluation<float> * privateEvaluate(SinglePrecision p, Context& context, AngleUnit angleUnit) const override {

View File

@@ -95,6 +95,9 @@ public:
static Expression * parse(char const * string);
virtual ~Expression() = default;
virtual Expression * clone() const = 0;
/* If isPositive is false, the expression has no sign (it does have to be
* negative) */
virtual bool isPositive() const { return false; }
/* Poor man's RTTI */
virtual Type type() const = 0;

View File

@@ -13,6 +13,7 @@ class Power : public StaticHierarchy<2> {
public:
Type type() const override;
Expression * clone() const override;
bool isPositive() const override;
template<typename T> static Complex<T> compute(const Complex<T> c, const Complex<T> d);
private:
constexpr static float k_maxNumberOfSteps = 10000.0f;

View File

@@ -24,6 +24,7 @@ public:
// Expression subclassing
Type type() const override;
Expression * clone() const override;
bool isPositive() const override { return !isNegative(); }
// Basic test
bool isZero() const { return m_numerator.isZero(); }

View File

@@ -23,6 +23,16 @@ Expression * Power::clone() const {
return new Power(m_operands, true);
}
bool Power::isPositive() const {
if (operand(1)->type() == Type::Rational) {
const Rational * r = static_cast<const Rational *>(operand(1));
if (r->denominator().isOne() && Integer::Division(r->numerator(), Integer(2)).remainder.isZero()) {
return true;
}
}
return false;
}
template<typename T>
Complex<T> Power::compute(const Complex<T> c, const Complex<T> d) {
if (d.b() != 0) {