}
}
-ParanoidNumber::ParanoidNumber(const char * str) : m_value(0)
+ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_cached_result(0)
{
Construct();
int dp = 0;
ParanoidNumber & ParanoidNumber::operator=(const ParanoidNumber & a)
{
m_value = a.m_value;
+ m_cached_result = a.m_cached_result;
for (int i = 0; i < NOP; ++i)
{
for (unsigned j = 0; j < m_next[i].size() && j < a.m_next[i].size(); ++j)
ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
{
delete Operation(new ParanoidNumber(a), ADD);
+ Simplify(ADD);
+ Simplify(SUBTRACT);
return *this;
}
ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
{
delete Operation(new ParanoidNumber(a), SUBTRACT);
+ //Simplify(SUBTRACT);
+ //Simplify(ADD);
return *this;
}
// a + b
ParanoidNumber * ParanoidNumber::OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
{
-
+ m_cached_result = nan("");
if (Floating() && m_value == 0) // 0 + b = b
{
m_value = b->m_value;
- if (NoFactors() && b->NoFactors())
+ if ((NoFactors() && b->NoFactors())
+ || (GetFactors() == b->GetFactors()))
{
if (ParanoidOp<digit_t>(m_value, b->m_value, op))
{
}
}
-
bool parent = (merge_point == NULL);
if (parent)
{
- merge->m_next[*merge_op].push_back(b);
+ //merge->m_next[*merge_op].push_back(b);
+ m_next[op].push_back(b);
}
else
{
ParanoidNumber * ParanoidNumber::OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
{
-
+ m_cached_result = nan("");
if (Floating() && m_value == 0)
{
return b;
}
if (b->Floating() && b->m_value == 1)
return b;
+
+
if (NoTerms() && b->NoTerms())
{
b->m_next[DIVIDE].clear();
b->m_next[MULTIPLY].clear();
+
+
+
return b;
}
}
swap(m_next[op], next);
for (auto n : next)
{
- ParanoidNumber * result = Operation(n, op);
- if (result != NULL)
- delete result;
- else
- m_next[op].push_back(n);
+ delete Operation(n, op);
}
return (next.size() > m_next[op].size());
}
+bool ParanoidNumber::FullSimplify()
+{
+ bool result = false;
+ result |= Simplify(MULTIPLY);
+ result |= Simplify(DIVIDE);
+ result |= Simplify(ADD);
+ result |= Simplify(SUBTRACT);
+ return result;
+}
+
+
+ParanoidNumber::digit_t ParanoidNumber::Digit()
+{
+ if (!isnan(m_cached_result))
+ return m_cached_result;
+ m_cached_result = m_value;
+ for (auto mul : m_next[MULTIPLY])
+ {
+ m_cached_result *= mul->Digit();
+ }
+ for (auto div : m_next[DIVIDE])
+ {
+ m_cached_result /= div->Digit();
+ }
+ for (auto add : m_next[ADD])
+ m_cached_result += add->Digit();
+ for (auto sub : m_next[SUBTRACT])
+ m_cached_result -= sub->Digit();
+ return m_cached_result;
+
+}
+
+ParanoidNumber::digit_t ParanoidNumber::GetFactors()
+{
+ digit_t value = 1;
+ for (auto mul : m_next[MULTIPLY])
+ value *= mul->Digit();
+ for (auto div : m_next[DIVIDE])
+ value /= div->Digit();
+ return value;
+}
+
+ParanoidNumber::digit_t ParanoidNumber::GetTerms()
+{
+ digit_t value = 0;
+ for (auto add : m_next[ADD])
+ value += add->Digit();
+ for (auto sub : m_next[SUBTRACT])
+ value -= sub->Digit();
+ return value;
+}
}
public:
typedef PARANOID_DIGIT_T digit_t;
- ParanoidNumber(digit_t value=0) : m_value(value)
+ ParanoidNumber(digit_t value=0) : m_value(value), m_cached_result(value)
{
Construct();
}
- ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value)
+ ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_cached_result(cpy.m_cached_result)
{
Construct();
for (int i = 0; i < NOP; ++i)
template <class T> T Convert() const;
+ digit_t GetFactors();
+ digit_t GetTerms();
+
- double ToDouble() const {return Convert<double>();}
- digit_t Digit() const {return Convert<digit_t>();}
+ double ToDouble() {return (double)Digit();}
+ digit_t Digit();
bool Floating() const
{
ParanoidNumber * TrivialOp(ParanoidNumber * b, Optype op);
ParanoidNumber * Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
bool Simplify(Optype op);
+ bool FullSimplify();
-
- bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
- bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
- bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
- bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
- bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
- bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
+ bool operator<(ParanoidNumber & a) {return ToDouble() < a.ToDouble();}
+ bool operator<=(ParanoidNumber & a) {return this->operator<(a) || this->operator==(a);}
+ bool operator>(ParanoidNumber & a) {return !(this->operator<=(a));}
+ bool operator>=(ParanoidNumber & a) {return !(this->operator<(a));}
+ bool operator==(ParanoidNumber & a) {return ToDouble() == a.ToDouble();}
+ bool operator!=(ParanoidNumber & a) {return !(this->operator==(a));}
ParanoidNumber operator+(const ParanoidNumber & a) const
{
return copy;
}
-
+
static int64_t Paranoia() {return g_count;}
std::string PStr() const;
digit_t m_value;
Optype m_op;
std::vector<ParanoidNumber*> m_next[4];
-
- int m_size;
+ digit_t m_cached_result;
+ bool m_cache_valid;
};
+
+
+
template <class T>
T ParanoidNumber::Convert() const
{
+ if (!isnan(m_cached_result))
+ return (T)m_cached_result;
T value(m_value);
for (auto mul : m_next[MULTIPLY])
{
- value *= mul->Digit();
+ value *= mul->Convert<T>();
}
for (auto div : m_next[DIVIDE])
{
- value /= div->Digit();
+ value /= div->Convert<T>();
}
for (auto add : m_next[ADD])
- value += add->Digit();
+ value += add->Convert<T>();
for (auto sub : m_next[SUBTRACT])
- value -= sub->Digit();
+ value -= sub->Convert<T>();
return value;
}
+
+
}
#endif //_PARANOIDNUMBER_H