From b0d94f6eadc5d0189ae23410763542180bf3f630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 13 Apr 2018 15:53:18 +0200 Subject: [PATCH] [poincare] Decimal: templatize Decimal constructor on float/double --- apps/regression/model/cubic_model.cpp | 4 ++-- apps/regression/model/quadratic_model.cpp | 2 +- apps/regression/model/quartic_model.cpp | 6 +++--- poincare/include/poincare/decimal.h | 2 +- poincare/src/decimal.cpp | 10 +++++++--- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/regression/model/cubic_model.cpp b/apps/regression/model/cubic_model.cpp index 5a2e4671e..c39a3b170 100644 --- a/apps/regression/model/cubic_model.cpp +++ b/apps/regression/model/cubic_model.cpp @@ -47,14 +47,14 @@ Expression * CubicModel::simplifiedExpression(double * modelCoefficients, Poinca new Decimal(a), new Power( new Symbol('x'), - new Decimal(3), + new Decimal(3.0), false), false); Expression * bx2Expression = new Multiplication( new Decimal(b), new Power( new Symbol('x'), - new Decimal(2), + new Decimal(2.0), false), false); Expression * cxExpression = new Multiplication( diff --git a/apps/regression/model/quadratic_model.cpp b/apps/regression/model/quadratic_model.cpp index 33ec1ddfa..a3b2f9b77 100644 --- a/apps/regression/model/quadratic_model.cpp +++ b/apps/regression/model/quadratic_model.cpp @@ -38,7 +38,7 @@ Expression * QuadraticModel::simplifiedExpression(double * modelCoefficients, Po new Decimal(a), new Power( new Symbol('x'), - new Decimal(2), + new Decimal(2.0), false), false); Expression * bxExpression = new Multiplication( diff --git a/apps/regression/model/quartic_model.cpp b/apps/regression/model/quartic_model.cpp index 883a29f4e..0ab65ca78 100644 --- a/apps/regression/model/quartic_model.cpp +++ b/apps/regression/model/quartic_model.cpp @@ -56,21 +56,21 @@ Expression * QuarticModel::simplifiedExpression(double * modelCoefficients, Poin new Decimal(a), new Power( new Symbol('x'), - new Decimal(4), + new Decimal(4.0), false), false); Expression * bx3Expression = new Multiplication( new Decimal(b), new Power( new Symbol('x'), - new Decimal(3), + new Decimal(3.0), false), false); Expression * cx2Expression = new Multiplication( new Decimal(c), new Power( new Symbol('x'), - new Decimal(2), + new Decimal(2.0), false), false); Expression * dxExpression = new Multiplication( diff --git a/poincare/include/poincare/decimal.h b/poincare/include/poincare/decimal.h index 52f25d4ec..33892903f 100644 --- a/poincare/include/poincare/decimal.h +++ b/poincare/include/poincare/decimal.h @@ -16,7 +16,7 @@ public: static int exponent(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, const char * exponent, int exponentLength, bool exponentNegative); static Integer mantissa(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, bool negative); Decimal(Integer mantissa, int exponent); - Decimal(double f); + template Decimal(T f); int exponent() const { return m_exponent; } Integer mantissa() const { return m_mantissa; } // Expression subclassing diff --git a/poincare/src/decimal.cpp b/poincare/src/decimal.cpp index 305049255..96506dad3 100644 --- a/poincare/src/decimal.cpp +++ b/poincare/src/decimal.cpp @@ -75,9 +75,10 @@ Decimal::Decimal(Integer mantissa, int exponent) : { } -Decimal::Decimal(double f) { - m_exponent = IEEE754::exponentBase10(f); - int64_t mantissaf = std::round(f * std::pow(10.0, -m_exponent+PrintFloat::k_numberOfStoredSignificantDigits+1)); +template +Decimal::Decimal(T f) { + m_exponent = IEEE754::exponentBase10(f); + int64_t mantissaf = std::round((double)f * std::pow((double)10.0, (double)(-m_exponent+PrintFloat::k_numberOfStoredSignificantDigits+1))); m_mantissa = Integer(mantissaf); } @@ -275,4 +276,7 @@ int Decimal::simplificationOrderSameType(const Expression * e, bool canBeInterru return ((int)sign())*unsignedComparison; } +template Decimal::Decimal(double); +template Decimal::Decimal(float); + }