[Poincare] Add Expression::approximate

This commit is contained in:
Romain Goyet
2015-09-07 18:25:57 +02:00
parent a26ef6194f
commit df70a72c27
5 changed files with 32 additions and 12 deletions

View File

@@ -3,6 +3,7 @@
#include <poincare/expression.h>
#include <poincare/fraction.h>
#include <poincare/integer.h>
#include <poincare/number.h>
#include <poincare/power.h>

View File

@@ -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);
};

View File

@@ -1,18 +1,22 @@
#ifndef POINCARE_NUMBER_H
#define POINCARE_NUMBER_H
#ifndef POINCARE_INTEGER_H
#define POINCARE_INTEGER_H
#include <poincare/expression.h>
#include <stdint.h>
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;
};

View File

@@ -3,6 +3,21 @@
#include <string.h>
#include <stdlib.h>
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<Integer *>(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() {

View File

@@ -1,6 +1,6 @@
#include <quiz.h>
#include <poincare.h>
QUIZ_CASE(integer) {
1+1;
QUIZ_CASE(poincare_integer) {
Integer i = Integer((char *)"123");
}