[poincare] Override setSign on all Numbers

This commit is contained in:
Léa Saviot
2018-08-20 11:26:07 +02:00
parent 60cd8a2fe4
commit 7d9c6618eb
6 changed files with 26 additions and 1 deletions

View File

@@ -12,7 +12,10 @@ namespace Poincare {
* - native_uint_t m_mantissa[] = { 1234 }
*/
class Decimal;
class DecimalNode : public NumberNode {
friend class Decimal;
public:
DecimalNode() :
m_negative(false),
@@ -44,6 +47,7 @@ public:
// Properties
Type type() const override { return Type::Decimal; }
Sign sign() const override { return m_negative ? Sign::Negative : Sign::Positive; }
Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override;
// Approximation
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
@@ -71,7 +75,7 @@ private:
constexpr static int k_maxBufferSize = PrintFloat::k_numberOfStoredSignificantDigits+1+1+1+1+4+1;
int convertToText(char * buffer, int bufferSize, Preferences::PrintFloatMode mode, int numberOfSignificantDigits) const;
template<typename T> Evaluation<T> templatedApproximate() const;
void setNegative(bool negative) { m_negative = negative; }
bool m_negative;
int m_exponent;
size_t m_numberOfDigitsInMantissa;
@@ -96,6 +100,7 @@ private:
DecimalNode * node() const override { return static_cast<DecimalNode *>(Number::node()); }
template <typename T> Decimal(T f);
Decimal(size_t size, Integer m, int e);
Expression setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const;
// Simplification
Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const;
Expression shallowBeautify(Context& context, Preferences::AngleUnit angleUnit) const;

View File

@@ -13,6 +13,7 @@ public:
InfinityNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
void setNegative(bool negative) { m_negative = negative; }
Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override;
// TreeNode
size_t size() const override { return sizeof(InfinityNode); }

View File

@@ -22,6 +22,7 @@ public:
// Properties
Type type() const override { return Type::Undefined; }
int polynomialDegree(char symbolName) const override;
Expression setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const override;
// Approximation
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {

View File

@@ -44,6 +44,10 @@ size_t DecimalNode::size() const {
return sizeof(DecimalNode)+ sizeof(native_uint_t)*m_numberOfDigitsInMantissa;
}
Expression DecimalNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
return Decimal(this).setSign(s, context, angleUnit);
}
int DecimalNode::simplificationOrderSameType(const ExpressionNode * e, bool canBeInterrupted) const {
assert(e->type() == Type::Decimal);
const DecimalNode * other = static_cast<const DecimalNode *>(e);
@@ -273,6 +277,12 @@ Decimal::Decimal(size_t size, Integer m, int e) : Number(TreePool::sharedPool()-
node()->setValue(m.node()->digits(), m.node()->numberOfDigits(), e, m.isNegative());
}
Expression Decimal::setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
Decimal result = *this;
result.node()->setNegative(s == ExpressionNode::Sign::Negative);
return result;
}
Expression Decimal::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const {
Expression e = Expression::defaultShallowReduce(context, angleUnit);
if (e.isUndefinedOrAllocationFailure()) {

View File

@@ -16,6 +16,10 @@ InfinityNode * InfinityNode::FailedAllocationStaticNode() {
return &failure;
}
Expression InfinityNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
return Infinity(s == Sign::Negative);
}
LayoutRef InfinityNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
char buffer[5];
int numberOfChars = serialize(buffer, 5, floatDisplayMode, numberOfSignificantDigits);

View File

@@ -19,6 +19,10 @@ int UndefinedNode::polynomialDegree(char symbolName) const {
return -1;
}
Expression UndefinedNode::setSign(Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
return Undefined();
}
LayoutRef UndefinedNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
char buffer[6];
int numberOfChars = PrintFloat::convertFloatToText<float>(NAN, buffer, 6, numberOfSignificantDigits, floatDisplayMode);