#include <sstream>
#include <fenv.h>
#include "log.h"
+#include <cassert>
#include <iostream>
using namespace std;
namespace IPDF
{
-
+int64_t ParanoidNumber::g_count = 0;
+
+
+ParanoidNumber::~ParanoidNumber()
+{
+ g_count--;
+ for (int i = 0; i < NOP; ++i)
+ delete m_next[i];
+}
-ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_op(ADD), m_next_term(NULL), m_next_factor(NULL)
+ParanoidNumber::ParanoidNumber(const char * str) : m_value(0)
{
+ Construct();
int dp = 0;
int end = 0;
while (str[dp] != '\0' && str[dp] != '.')
}
while (str[end] != '\0')
++end;
-
ParanoidNumber m(1);
for (int i = dp-1; i >= 0; --i)
{
ParanoidNumber b(str[i]-'0');
b*=m;
- //Debug("m is %s", m.Str().c_str());
- //Debug("Add %s", b.Str().c_str());
this->operator+=(b);
- //Debug("Now at %s", Str().c_str());
m*=10;
}
ParanoidNumber n(1);
for (int i = dp+1; i < end; ++i)
{
+ Debug("{%s} /= 10", n.Str().c_str());
n/=10;
+ Debug("{%s}", n.Str().c_str());
ParanoidNumber b(str[i]-'0');
- //Debug("%s * %s", b.Str().c_str(), n.Str().c_str());
b*=n;
- //Debug("b -> %s", b.Str().c_str());
- //Debug("Add %s", b.Str().c_str());
+ Debug("{%s} += {%s}", Str().c_str(), b.Str().c_str());
this->operator+=(b);
- //Debug("Now at %s", Str().c_str());
-
}
- Debug("Constructed {%s} from %s (%f)", Str().c_str(), str, ToDouble());
}
ParanoidNumber & ParanoidNumber::operator=(const ParanoidNumber & a)
{
- //TODO: Optimise
- delete m_next_term;
- delete m_next_factor;
- m_op = a.m_op;
- if (a.m_next_term != NULL)
- {
- m_next_term = new ParanoidNumber(*(a.m_next_term));
- }
- if (a.m_next_factor != NULL)
+ m_value = a.m_value;
+ for (int i = 0; i < NOP; ++i)
{
- m_next_factor = new ParanoidNumber(*(a.m_next_factor));
- }
+ if (a.m_next[i] == NULL)
+ {
+ if (m_next[i] != NULL)
+ delete m_next[i];
+ m_next[i] = NULL;
+ continue;
+ }
+
+ if (m_next[i] != NULL)
+ {
+ m_next[i]->operator=(*(a.m_next[i]));
+ }
+ else
+ {
+ m_next[i] = new ParanoidNumber(*(a.m_next[i]));
+ }
+ }
return *this;
}
-ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
+
+string ParanoidNumber::Str() const
{
- // this = v + t + (a)
- // -> v + (a) + t
-
- ParanoidNumber * nt = m_next_term;
- ParanoidNumber * nf = m_next_factor;
+ string result("");
+ stringstream s;
+ s << (double)m_value;
+ result += s.str();
+ if (m_next[MULTIPLY] != NULL)
+ {
+ result += "*";
+ if (m_next[MULTIPLY]->m_next[ADD] != NULL || m_next[MULTIPLY]->m_next[SUBTRACT] != NULL)
+ result += "(" + m_next[MULTIPLY]->Str() + ")";
+ else
+ result += m_next[MULTIPLY]->Str();
+ }
+ if (m_next[DIVIDE] != NULL)
+ {
+ result += "/";
+ if (m_next[DIVIDE]->m_next[ADD] != NULL || m_next[DIVIDE]->m_next[SUBTRACT] != NULL)
+ result += "(" + m_next[DIVIDE]->Str() + ")";
+ else
+ result += m_next[DIVIDE]->Str();
+ }
- ParanoidNumber ca(a);
- if (m_next_factor != NULL)
+ if (m_next[ADD] != NULL)
{
- if (m_next_factor->m_op == MULTIPLY)
- ca /= (*m_next_factor);
+ result += "+";
+ if (m_next[ADD]->m_next[MULTIPLY] != NULL || m_next[ADD]->m_next[DIVIDE] != NULL)
+ result += "(" + m_next[ADD]->Str() + ")";
else
- ca *= (*m_next_factor);
-
- if (ca.Floating())
- {
- m_next_factor = NULL;
- m_next_term = NULL;
- operator+=(ca);
- m_next_factor = nf;
- m_next_term = nt;
- Simplify();
- return *this;
- }
-
+ result += m_next[ADD]->Str();
+ }
+ if (m_next[SUBTRACT] != NULL)
+ {
+ result += "-";
+ if (m_next[SUBTRACT]->m_next[MULTIPLY] != NULL || m_next[SUBTRACT]->m_next[DIVIDE] != NULL)
+ result += "(" + m_next[SUBTRACT]->Str() + ")";
+ else
+ result += m_next[SUBTRACT]->Str();
}
- m_next_term = new ParanoidNumber(a, ADD);
- ParanoidNumber * t = m_next_term;
- while (t->m_next_term != NULL)
- t = t->m_next_term;
- t->m_next_term = nt;
- //Debug("Simplify {%s} after add", Str().c_str());
- Simplify();
- return *this;
+
+ return result;
}
-ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
+template <>
+bool TrustingOp<float>(float & a, const float & b, Optype op)
{
- // this = v + t + (a)
- // -> v + (a) + t
-
- ParanoidNumber * nt = m_next_term;
- ParanoidNumber * nf = m_next_factor;
-
- ParanoidNumber ca(a, SUBTRACT);
- if (m_next_factor != NULL)
+ feclearexcept(FE_ALL_EXCEPT);
+ switch (op)
{
- if (m_next_factor->m_op == MULTIPLY)
- ca /= (*m_next_factor);
- else
- ca *= (*m_next_factor);
-
- if (ca.Floating())
- {
- m_next_factor = NULL;
- m_next_term = NULL;
- operator-=(ca);
- m_next_factor = nf;
- m_next_term = nt;
- Simplify();
- return *this;
- }
-
+ case ADD:
+ a += b;
+ break;
+ case SUBTRACT:
+ a -= b;
+ break;
+ case MULTIPLY:
+ a *= b;
+ break;
+ case DIVIDE:
+ a /= b;
+ break;
+ case NOP:
+ break;
}
-
- m_next_term = new ParanoidNumber(a,SUBTRACT);
- ParanoidNumber * t = m_next_term;
- while (t->m_next_term != NULL)
+ return !fetestexcept(FE_ALL_EXCEPT);
+}
+
+template <>
+bool TrustingOp<double>(double & a, const double & b, Optype op)
+{
+ feclearexcept(FE_ALL_EXCEPT);
+ switch (op)
{
- t->m_op = SUBTRACT;
- t = t->m_next_term;
+ case ADD:
+ a += b;
+ break;
+ case SUBTRACT:
+ a -= b;
+ break;
+ case MULTIPLY:
+ a *= b;
+ break;
+ case DIVIDE:
+ a /= b;
+ break;
+ case NOP:
+ break;
}
- t->m_op = SUBTRACT;
- //Debug("next term {%s}", m_next_term->Str().c_str());
- t->m_next_term = nt;
- //Debug("Simplify {%s} after sub", Str().c_str());
- Simplify();
- return *this;
+ return !fetestexcept(FE_ALL_EXCEPT);
}
-ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
+template <>
+bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op)
{
- Debug("{%s} *= {%s}", Str().c_str(), a.Str().c_str());
- // this = (vf + t) * (a)
- ParanoidNumber * nf = m_next_factor;
- m_next_factor = new ParanoidNumber(a, MULTIPLY);
- ParanoidNumber * t = m_next_factor;
- while (t->m_next_factor != NULL)
- t = t->m_next_factor;
- t->m_next_factor = nf;
- if (m_next_term != NULL)
- m_next_term->operator*=(a);
- //Debug("Simplify after mul");
- Debug("Simplify {%s}", Str().c_str());
- Simplify();
+ int16_t sa(a);
+ bool exact = true;
+ switch (op)
+ {
+ case ADD:
+ sa += b;
+ exact = (abs(sa) <= 127);
+ break;
+ case SUBTRACT:
+ sa -= b;
+ exact = (abs(sa) <= 127);
+ break;
+ case MULTIPLY:
+ sa *= b;
+ exact = (abs(sa) <= 127);
+ break;
+ case DIVIDE:
+ exact = (b != 0 && sa > b && sa % b == 0);
+ sa /= b;
+ break;
+ case NOP:
+ break;
+ }
+ a = (int8_t)(sa);
+ return exact;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), ADD);
return *this;
}
-ParanoidNumber & ParanoidNumber::operator/=(const ParanoidNumber & a)
+ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
{
- //Debug("Called %s /= %s", Str().c_str(), a.Str().c_str());
- // this = (vf + t) * (a)
- ParanoidNumber * nf = m_next_factor;
- m_next_factor = new ParanoidNumber(a, DIVIDE);
- ParanoidNumber * t = m_next_factor;
- while (t->m_next_factor != NULL)
- t = t->m_next_factor;
- t->m_next_factor = nf;
- if (m_next_term != NULL)
- m_next_term->operator/=(a);
- Simplify();
+ delete Operation(new ParanoidNumber(a), SUBTRACT);
return *this;
}
+ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), MULTIPLY);
+ return *this;
+}
-void ParanoidNumber::SimplifyTerms()
-{
- //Debug("Simplify {%s}", Str().c_str());
- if (m_next_term == NULL)
- {
- //Debug("No terms!");
- return;
- }
+ParanoidNumber & ParanoidNumber::operator/=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), DIVIDE);
+ return *this;
+}
- for (ParanoidNumber * a = this; a != NULL; a = a->m_next_term)
+/**
+ * Performs the operation on a with argument b (a += b, a -= b, a *= b, a /= b)
+ * @returns b if b can safely be deleted
+ * @returns NULL if b has been merged with a
+ * append indicates that b should be merged
+ */
+ParanoidNumber * ParanoidNumber::Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** parent)
+{
+ if (b == NULL)
+ return NULL;
+
+ Optype invop = InverseOp(op); // inverse of p
+ ParanoidNumber * append_at = this;
+
+ if (Floating())
{
- ParanoidNumber * bprev = a;
- ParanoidNumber * b = a->m_next_term;
- while (b != NULL)
+ if ((op == ADD || op == SUBTRACT) && (m_value == 0))
{
- //Debug("Simplify factors of %s", b->Str().c_str());
- b->SimplifyFactors();
- if (b->m_next_factor != NULL)
- {
- bprev = b;
- b = b->m_next_term;
- continue;
- }
- float f = a->m_value;
- feclearexcept(FE_ALL_EXCEPT);
- switch (b->m_op)
+ m_value = b->m_value;
+ for (int i = 0; i < NOP; ++i)
{
- case ADD:
- f += b->m_value;
- break;
- case SUBTRACT:
- f -= b->m_value;
- break;
- default:
- Fatal("Unexpected %c in term list...", OpChar(b->m_op));
- break;
+ m_next[i] = b->m_next[i];
+ b->m_next[i] = NULL;
}
- if (!fetestexcept(FE_ALL_EXCEPT))
+ return b;
+ }
+ if ((op == MULTIPLY) && (m_value == 1))
+ {
+ m_value = b->m_value;
+ for (int i = 0; i < NOP; ++i)
{
- a->m_value = f;
- bprev->m_next_term = b->m_next_term;
- b->m_next_term = NULL;
- delete b;
- b = bprev;
+ m_next[i] = b->m_next[i];
+ b->m_next[i] = NULL;
}
- bprev = b;
- b = b->m_next_term;
+ return b;
+ return b;
}
+
}
-}
-
-void ParanoidNumber::SimplifyFactors()
-{
- //Debug("Simplify {%s}", Str().c_str());
- if (m_next_factor == NULL)
+
+ if (b->Floating())
{
- //Debug("No factors!");
- return;
+ if ((op == ADD || op == SUBTRACT) && (b->m_value == 0))
+ return b;
+ if ((op == MULTIPLY || op == DIVIDE) && (b->m_value == 1))
+ return b;
}
-
- for (ParanoidNumber * a = this; a != NULL; a = a->m_next_factor)
+
+ // Operation can be applied directly to the m_value of this and b
+ // ie: op is + or - and this and b have no * or / children
+ // or: op is * or / and this and b have no + or - children
+ if (Pure(op) && (b->Pure(op)))
{
- ParanoidNumber * bprev = a;
- ParanoidNumber * b = a->m_next_factor;
- while (b != NULL)
- {
- b->SimplifyTerms();
- if (b->m_next_term != NULL)
+ if (ParanoidOp<digit_t>(m_value, b->m_value, op)) // op applied successfully...
+ {
+ Simplify(op);
+ Simplify(invop);
+ for (int i = 0; i < NOP; ++i) // Try applying b's children to this
{
- bprev = b;
- b = b->m_next_factor;
- continue;
+ delete Operation(b->m_next[i], Optype(i));
+ b->m_next[i] = NULL;
}
- float f = a->m_value;
- feclearexcept(FE_ALL_EXCEPT);
- switch (b->m_op)
+ return b; // can delete b
+ }
+ }
+
+ // Try to simplify the cases:
+ // a + b*c == (a/c + b)*c
+ // a + b/c == (a*c + b)/c
+ else if ((op == ADD || op == SUBTRACT) &&
+ (Pure(op) || b->Pure(op)))
+ {
+
+ Debug("Simplify: {%s} %c {%s}", Str().c_str(), OpChar(op), b->Str().c_str());
+ Optype adj[] = {MULTIPLY, DIVIDE};
+ for (int i = 0; i < 2; ++i)
+ {
+
+ Optype f = adj[i];
+ Optype invf = InverseOp(f);
+
+ Debug("Try %c", OpChar(f));
+
+ if (m_next[f] == NULL && b->m_next[f] == NULL)
+ continue;
+
+ ParanoidNumber * tmp_a = new ParanoidNumber(*this);
+ ParanoidNumber * tmp_b = new ParanoidNumber(*b);
+
+
+ ParanoidNumber * af = (tmp_a->m_next[f] != NULL) ? new ParanoidNumber(*(tmp_a->m_next[f])) : NULL;
+ ParanoidNumber * bf = (tmp_b->m_next[f] != NULL) ? new ParanoidNumber(*(tmp_b->m_next[f])) : NULL;
+
+ Debug("{%s} %c {%s}", tmp_a->Str().c_str(), OpChar(op), tmp_b->Str().c_str());
+ Debug("{%s} %c {%s}", tmp_a->Str().c_str(), OpChar(op), tmp_b->Str().c_str());
+ if (tmp_a->Operation(af, invf) != af || tmp_b->Operation(bf, invf) != bf)
{
- case MULTIPLY:
- if (a->m_op != DIVIDE)
- f *= b->m_value;
- else
- f /= b->m_value;
- break;
- case DIVIDE:
- if (a->m_op != DIVIDE)
- f /= b->m_value;
- else
- f *= b->m_value;
- break;
- default:
- Fatal("Unexpected %c in factor list...",OpChar(b->m_op));
- break;
+ delete af;
+ delete bf;
+ delete tmp_a;
+ delete tmp_b;
+ continue;
}
- if (!fetestexcept(FE_ALL_EXCEPT))
- {
-
- a->m_value = f;
- bprev->m_next_factor = b->m_next_factor;
- b->m_next_factor = NULL;
- delete b;
- b = bprev;
+ Debug("{%s} %c {%s}", tmp_a->Str().c_str(), OpChar(op), tmp_b->Str().c_str());
+
+ if (tmp_a->Operation(bf, invf) == bf && tmp_b->Operation(af, invf) == af) // a / c simplifies
+ {
+ if (tmp_a->Operation(tmp_b, op) != NULL) // (a/c) + b simplifies
+ {
+ this->operator=(*tmp_a);
+ if (bf != NULL)
+ delete Operation(bf, f);
+ if (af != NULL)
+ delete Operation(af, f);
+ delete tmp_a;
+ delete tmp_b;
+ return b; // It simplified after all!
+ }
+ else
+ {
+ tmp_b = NULL;
+ delete af;
+ delete bf;
+ }
}
- //else
- //Debug("Failed to simplify %f %c %f", a->m_value, OpChar(b->m_op), b->m_value);
- bprev = b;
- b = b->m_next_factor;
+ //Debug("tmp_a : %s", tmp_a->PStr().c_str());
+ //Debug("tmp_b : %s", tmp_b->PStr().c_str());
+ delete tmp_a;
+ delete tmp_b;
}
}
+
+ // See if operation can be applied to children of this in the same dimension
+ {
+ // (a / b) / c = a / (b*c)
+ // (a * b) / c = a * (b/c)
+ // (a / b) * c = a / (b/c)
+ // (a * b) * c = a * (b*c)
+ // (a + b) + c = a + (b+c)
+ // (a - b) + c = a - (b-c)
+ // (a + b) - c = a + (b-c)
+ // (a - b) - c = a - (b+c)
+ Optype fwd(op);
+ Optype rev(invop);
+ if (op == DIVIDE || op == SUBTRACT)
+ {
+ fwd = invop;
+ rev = op;
+ }
+ // opposite direction first (because ideally things will cancel each other out...)
+ if (m_next[invop] != NULL && m_next[invop]->Operation(b, rev, &append_at) != NULL)
+ return b;
+ // forward direction
+ if (m_next[op] != NULL && m_next[op]->Operation(b, fwd, &append_at) != NULL)
+ return b;
+ }
+
+ // At this point, we have no choice but to merge 'b' with this ParanoidNumber
+
+ // we are a child; the merge operation needs to be applied by the root, so leave
+ if (parent != NULL)
+ {
+ if (m_next[op] == NULL)
+ *parent = this; // last element in list
+ return NULL;
+ }
+
+ append_at->m_next[op] = b; // Merge with b
+
+ // MULTIPLY and DIVIDE operations need to be performed on each term in the ADD/SUBTRACT dimension
+ if (op == DIVIDE || op == MULTIPLY)
+ {
+ // apply the operation to each term
+ if (m_next[ADD] != NULL) delete m_next[ADD]->Operation(new ParanoidNumber(*b), op);
+ if (m_next[SUBTRACT] != NULL) delete m_next[SUBTRACT]->Operation(new ParanoidNumber(*b), op);
+
+ // try and simplify this by adding the terms (you never know...)
+ Simplify(ADD);
+ Simplify(SUBTRACT);
+ }
+ // failed to simplify
+ return NULL;
}
-void ParanoidNumber::Simplify()
-{
- SimplifyFactors();
- SimplifyTerms();
-}
-
-string ParanoidNumber::Str() const
+bool ParanoidNumber::Simplify(Optype op)
{
- string result("");
- stringstream s;
-
- s << m_value;
-
- if (m_next_factor != NULL)
+ ParanoidNumber * n = m_next[op];
+ m_next[op] = NULL;
+ if (Operation(n, Optype(op)))
{
- result += OpChar(m_op);
- result += "(";
- result += s.str();
- result += m_next_factor->Str();
- result += ")";
+ delete n;
+ return true;
}
else
{
- result += OpChar(m_op);
- result += s.str();
+ m_next[op] = n;
+ return false;
}
-
- if (m_next_term != NULL)
+}
+
+string ParanoidNumber::PStr() const
+{
+ stringstream s;
+ for (int i = 0; i < NOP; ++i)
{
- result += " ";
- result += m_next_term->Str();
+ Optype f = Optype(i);
+ s << this << OpChar(f) << m_next[f] << "\n";
}
- return result;
+ return s.str();
}
+
+
+
+
}