From 891366c51d6efacfd110ba5da80ad64aada6e787 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Fri, 17 Jul 2020 10:15:25 +0200 Subject: [PATCH] [poincare/expression_node] Added Double type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Float and Float used to share the same expression type (Float), and the distinction was made by comparing their size. However, due to padding, their size could be the same, leading to some issues : - On the simulator, in Calculation, type 1_mm^3 and go to the additional outputs. One of the results would be -0.0003081979_µm^3. Change-Id: Ic8f9328bf462104776fbab636c34d0d152cd7e58 --- poincare/include/poincare/expression_node.h | 1 + poincare/include/poincare/float.h | 2 +- poincare/src/expression.cpp | 2 +- poincare/src/float.cpp | 2 +- poincare/src/number.cpp | 9 +++------ 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/poincare/include/poincare/expression_node.h b/poincare/include/poincare/expression_node.h index 79c6eaa88..853355a89 100644 --- a/poincare/include/poincare/expression_node.h +++ b/poincare/include/poincare/expression_node.h @@ -30,6 +30,7 @@ public: Rational, BasedInteger, Decimal, + Double, Float, Infinity, Multiplication, diff --git a/poincare/include/poincare/float.h b/poincare/include/poincare/float.h index 40f73ffed..400e3cc48 100644 --- a/poincare/include/poincare/float.h +++ b/poincare/include/poincare/float.h @@ -36,7 +36,7 @@ public: #endif // Properties - Type type() const override { return Type::Float; } + Type type() const override { return (sizeof(T) == sizeof(float)) ? Type::Float : Type::Double; } Sign sign(Context * context) const override { return m_value < 0 ? Sign::Negative : Sign::Positive; } Expression setSign(Sign s, ReductionContext reductionContext) override; int simplificationOrderSameType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const override; diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 62592f10c..a9032a621 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -167,7 +167,7 @@ bool Expression::deepIsMatrix(Context * context) const { } bool Expression::IsApproximate(const Expression e, Context * context) { - return e.type() == ExpressionNode::Type::Decimal || e.type() == ExpressionNode::Type::Float; + return e.type() == ExpressionNode::Type::Decimal || e.type() == ExpressionNode::Type::Float || e.type() == ExpressionNode::Type::Double; } bool Expression::IsRandom(const Expression e, Context * context) { diff --git a/poincare/src/float.cpp b/poincare/src/float.cpp index cdfd918d7..f26af803d 100644 --- a/poincare/src/float.cpp +++ b/poincare/src/float.cpp @@ -18,7 +18,7 @@ int FloatNode::simplificationOrderSameType(const ExpressionNode * e, bool asc if (!ascending) { return e->simplificationOrderSameType(this, true, canBeInterrupted, ignoreParentheses); } - assert(e->type() == ExpressionNode::Type::Float); + assert((e->type() == ExpressionNode::Type::Float && sizeof(T) == sizeof(float)) || (e->type() == ExpressionNode::Type::Double && sizeof(T) == sizeof(double))); const FloatNode * other = static_cast *>(e); if (value() < other->value()) { return -1; diff --git a/poincare/src/number.cpp b/poincare/src/number.cpp index 0bbd1c252..32e4f58f2 100644 --- a/poincare/src/number.cpp +++ b/poincare/src/number.cpp @@ -25,12 +25,9 @@ double NumberNode::doubleApproximation() const { assert(Number(this).sign() == Sign::Negative || Number(this).sign() == Sign::Positive); return Number(this).sign() == Sign::Negative ? -INFINITY : INFINITY; case Type::Float: - if (size() == sizeof(FloatNode)) { - return static_cast *>(this)->value(); - } else { - assert(size() == sizeof(FloatNode)); - return static_cast *>(this)->value(); - } + return static_cast *>(this)->value(); + case Type::Double: + return static_cast *>(this)->value(); case Type::Rational: return static_cast(this)->templatedApproximate(); case Type::BasedInteger: