From df70a72c272c6dac60e334081eb1bfc999f84db1 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Mon, 7 Sep 2015 18:25:57 +0200 Subject: [PATCH] [Poincare] Add Expression::approximate --- poincare/include/poincare.h | 1 + poincare/include/poincare/expression.h | 6 ++++++ poincare/include/poincare/integer.h | 8 ++++++-- poincare/src/integer.cpp | 25 +++++++++++++++++-------- poincare/test/integer.cpp | 4 ++-- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/poincare/include/poincare.h b/poincare/include/poincare.h index e0a5dfbd8..00d2b93c6 100644 --- a/poincare/include/poincare.h +++ b/poincare/include/poincare.h @@ -3,6 +3,7 @@ #include #include +#include #include #include diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index b99621c2a..9ae427fb7 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -8,6 +8,7 @@ extern "C" { void CreateFromString(char * string); class Expression; +class Integer; typedef void (Expression::*ExpressionAction)(void); class Expression { @@ -19,6 +20,11 @@ class Expression { virtual Expression ** children() = 0; // NULL-terminated virtual void draw(); virtual void layout(); + /* identicalTo means strictly the same tree. For example, 3+5 is NOT identi- + * cal to 5+3. Those are equal, but not identical. */ + virtual bool identicalTo(Expression * e); + //virtual Integer approximate(); + //virtual Expression * simplify(); private: void forEachChild(ExpressionAction); }; diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 8e06aa7a3..5930ba648 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -1,18 +1,22 @@ -#ifndef POINCARE_NUMBER_H -#define POINCARE_NUMBER_H +#ifndef POINCARE_INTEGER_H +#define POINCARE_INTEGER_H #include +#include class Integer : public Expression { public: Integer(char * string); + Integer(uint32_t integer); ~Integer(); //Number(int v); virtual void draw(); virtual Expression ** children(); + virtual bool identicalTo(Expression * e); protected: virtual void layout(); private: + uint16_t m_numberOfBits; void * m_bits; }; diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 403a2906a..8eb047ff3 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -3,6 +3,21 @@ #include #include +Integer::Integer(uint32_t integer) { + // FIXME: Suboptimal (and somewhat wrong, too!) + m_numberOfBits = 32; + m_bits = malloc(4); + *(uint32_t *)m_bits = integer; +} + +bool Integer::identicalTo(Expression * e) { + /* FIXME + Integer * i = dynamic_cast(e); + return (i != NULL); + */ + return true; +} + Integer::Integer(char * string) { int base = 10; int stringLength = strlen(string); @@ -31,17 +46,11 @@ Integer::Integer(char * string) { float log2 = 3.32193f; // Caution: This value has to be round up! //int num_bytes = ceilf(log2*stringLength)/8; // FIXME: We don't have ceilf just yet. Do we really need it though? - int num_bytes = (log2*stringLength)/8; - - m_bits = malloc(num_bytes); + m_numberOfBits = (log2*stringLength); + m_bits = malloc(m_numberOfBits/8+1); - // How many bits in base 2 for a k-digits number in base n? - // num_bits = log_2(number) (because 2**num_bits = number, more or less) - // = log_2(n^k) - // = log_2(n)*k - } Integer::~Integer() { diff --git a/poincare/test/integer.cpp b/poincare/test/integer.cpp index 9987eaf98..d72f3ef7d 100644 --- a/poincare/test/integer.cpp +++ b/poincare/test/integer.cpp @@ -1,6 +1,6 @@ #include #include -QUIZ_CASE(integer) { - 1+1; +QUIZ_CASE(poincare_integer) { + Integer i = Integer((char *)"123"); }