diff --git a/poincare/include/poincare/number.h b/poincare/include/poincare/number.h index 9f0b884de..1854f26a8 100644 --- a/poincare/include/poincare/number.h +++ b/poincare/include/poincare/number.h @@ -31,6 +31,8 @@ public: NumberNode * numberNode() const { assert(!isAllocationFailure()); return static_cast(node()); } /* Return either a IntegerReference, a DecimalReference or an InfiniteReference. */ static NumberReference Integer(const char * digits, size_t length, bool negative); + /* Return either a DecimalInteger or an InfiniteReference. */ + template static NumberReference Decimal(T f); /* This set of Functions return either a RationalReference or a FloatReference * or InfiniteReference in case of overflow. DecimalReference are not taken into * account as it is not an internal node - it will always be turned into a diff --git a/poincare/src/complex.cpp b/poincare/src/complex.cpp index 0a49178c3..920b8dbac 100644 --- a/poincare/src/complex.cpp +++ b/poincare/src/complex.cpp @@ -42,17 +42,6 @@ T ComplexNode::toScalar() const { return NAN; } -template -static ExpressionReference CreateDecimal(T f) { - if (std::isnan(f)) { - return UndefinedReference(); - } - if (std::isinf(f)) { - return InfiniteReference(f < 0.0); - } - return DecimalReference(f); -} - template ExpressionReference ComplexNode::complexToExpression(Preferences::ComplexFormat complexFormat) const { if (std::isnan(this->real()) || std::isnan(this->imag())) { @@ -64,15 +53,15 @@ ExpressionReference ComplexNode::complexToExpression(Preferences::ComplexForm ExpressionReference real(nullptr); ExpressionReference imag(nullptr); if (this->real() != 0 || this->imag() == 0) { - real = CreateDecimal(this->real()); + real = NumberReference::Decimal(this->real()); } if (this->imag() != 0) { if (this->imag() == 1.0 || this->imag() == -1) { imag = SymbolReference(Ion::Charset::IComplex); } else if (this->imag() > 0) { - imag = MultiplicationReference(CreateDecimal(this->imag()), SymbolReference(Ion::Charset::IComplex)); + imag = MultiplicationReference(NumberReference::Decimal(this->imag()), SymbolReference(Ion::Charset::IComplex)); } else { - imag = MultiplicationReference(CreateDecimal(-this->imag()), SymbolReference(Ion::Charset::IComplex)); + imag = MultiplicationReference(NumberReference::Decimal(-this->imag()), SymbolReference(Ion::Charset::IComplex)); } } if (!imag.isDefined()) { @@ -98,7 +87,7 @@ ExpressionReference ComplexNode::complexToExpression(Preferences::ComplexForm T r = std::abs(*this); T th = std::arg(*this); if (r != 1 || th == 0) { - norm = CreateDecimal(r); + norm = NumberReference::Decimal(r); } if (r != 0 && th != 0) { ExpressionReference arg(nullptr); @@ -107,9 +96,9 @@ ExpressionReference ComplexNode::complexToExpression(Preferences::ComplexForm } else if (th == -1.0) { arg = OppositeReference(SymbolReference(Ion::Charset::IComplex)); } else if (th > 0) { - arg = MultiplicationReference(CreateDecimal(th), SymbolReference(Ion::Charset::IComplex)); + arg = MultiplicationReference(NumberReference::Decimal(th), SymbolReference(Ion::Charset::IComplex)); } else { - arg = OppositeRefrence(MultiplicationReference(CreateDecimal(-th), SymbolReference(Ion::Charset::IComplex))); + arg = OppositeRefrence(MultiplicationReference(NumberReference::Decimal(-th), SymbolReference(Ion::Charset::IComplex))); } exp = PowerReference(SymbolReference(Ion::Charset::Exponential), arg); } diff --git a/poincare/src/number.cpp b/poincare/src/number.cpp index 5fff5d946..29f8a5c88 100644 --- a/poincare/src/number.cpp +++ b/poincare/src/number.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -55,11 +56,21 @@ NumberReference NumberReference::Integer(const char * digits, size_t length, boo negative = true; digits++; } - /*if (length > Decimal::k_maxExponentLength) { + if (length > Decimal::k_maxExponentLength) { return InfinityReference(negative); } int exponent = Decimal::Exponent(digits, length, nullptr, 0, nullptr, 0, negative); - return DecimalReference(digits, length, nullptr, 0, negative, exponent);*/ + return DecimalReference(digits, length, nullptr, 0, negative, exponent); +} + +template static NumberReference NumberReference::Decimal(T f) { + if (std::isnan(f)) { + return UndefinedReference(); + } + if (std::isinf(f)) { + return InfiniteReference(f < 0.0); + } + return DecimalReference(f); } NumberReference NumberReference::BinaryOperation(const NumberReference i, const NumberReference j, IntegerBinaryOperation integerOp, RationalBinaryOperation rationalOp, DoubleBinaryOperation doubleOp) {