1 #ifndef _PARANOIDNUMBER_H
2 #define _PARANOIDNUMBER_H
11 #define PARANOID_DIGIT_T int8_t // we could theoretically replace this with a template
12 // but let's not do that...
16 typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE} Optype;
18 /** Performs an operation, returning if the result was exact **/
19 // NOTE: DIFFERENT to ParanoidOp (although that wraps to this...)
20 template <class T> bool TrustingOp(T & a, const T & b, Optype op);
22 /** Performs an operation _only_ if the result would be exact **/
23 template <class T> bool ParanoidOp(T & a, const T & b, Optype op)
26 if (TrustingOp<T>(cpy, b, op))
35 template <> bool TrustingOp<float>(float & a, const float & b, Optype op);
36 template <> bool TrustingOp<double>(double & a, const double & b, Optype op);
37 template <> bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op);
39 // Attempt to comine two terms: a*b + c*d or a/b + c/d
40 template <class T> bool CombineTerms(T & aa, Optype aop, T & bb, T & cc, Optype cop, T & dd)
42 T a(aa); T b(bb); T c(cc); T d(dd);
43 if (aop == MULTIPLY && cop == MULTIPLY) // a*b + c*d
45 if ((ParanoidOp<T>(c, b, DIVIDE) || ParanoidOp(d, b, DIVIDE))
46 && TrustingOp<T>(c, d, MULTIPLY) && TrustingOp<T>(a,c,ADD)
47 && TrustingOp<T>(a, b, MULTIPLY)) // (a + (cd)/b) * b
55 if ((ParanoidOp<T>(a, d, DIVIDE) || ParanoidOp(b, d, DIVIDE))
56 && TrustingOp<T>(a, b, MULTIPLY) && TrustingOp<T>(a,c,ADD)
57 && TrustingOp<T>(a, d, MULTIPLY)) // ((ab)/d + c)*d
67 else if (aop == DIVIDE && cop == DIVIDE)
69 if (TrustingOp<T>(a, d, MULTIPLY) && TrustingOp<T>(c, b, MULTIPLY)
70 && TrustingOp<T>(a, c, ADD) && TrustingOp<T>(b, d, MULTIPLY))
74 if (ParanoidOp<T>(a, b, DIVIDE))
93 typedef PARANOID_DIGIT_T digit_t;
95 ParanoidNumber(digit_t value=0, Optype type = ADD) : m_value(value), m_op(type), m_next_term(NULL), m_next_factor(NULL)
100 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_op(cpy.m_op), m_next_term(NULL), m_next_factor(NULL)
102 if (cpy.m_next_term != NULL)
104 m_next_term = new ParanoidNumber(*(cpy.m_next_term));
106 if (cpy.m_next_factor != NULL)
108 m_next_factor = new ParanoidNumber(*(cpy.m_next_factor));
113 ParanoidNumber(const ParanoidNumber & cpy, Optype type) : ParanoidNumber(cpy)
118 ParanoidNumber(const char * str);
119 ParanoidNumber(const std::string & str) : ParanoidNumber(str.c_str()) {Construct();}
121 virtual ~ParanoidNumber()
123 if (m_next_term != NULL)
125 if (m_next_factor != NULL)
126 delete m_next_factor;
130 inline void Construct() {g_count++;}
133 template <class T> T Convert() const;
134 template <class T> T AddTerms() const;
135 template <class T> T MultiplyFactors() const;
136 template <class T> T Head() const {return (m_op == SUBTRACT) ? T(-m_value) : T(m_value);}
141 double ToDouble() const {return Convert<double>();}
142 float ToFloat() const {return Convert<float>();}
143 digit_t Digit() const {return Convert<digit_t>();}
145 bool Floating() const {return (m_next_term == NULL && m_next_factor == NULL);}
146 bool Sunken() const {return !Floating();} // I could not resist...
148 ParanoidNumber & operator+=(const ParanoidNumber & a);
149 ParanoidNumber & operator-=(const ParanoidNumber & a);
150 ParanoidNumber & operator*=(const ParanoidNumber & a);
151 ParanoidNumber & operator/=(const ParanoidNumber & a);
152 ParanoidNumber & operator=(const ParanoidNumber & a);
155 bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
156 bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
157 bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
158 bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
159 bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
160 bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
162 ParanoidNumber operator+(const ParanoidNumber & a) const
164 ParanoidNumber result(*this);
168 ParanoidNumber operator-(const ParanoidNumber & a) const
170 ParanoidNumber result(*this);
174 ParanoidNumber operator*(const ParanoidNumber & a) const
176 ParanoidNumber result(*this);
180 ParanoidNumber operator/(const ParanoidNumber & a) const
182 ParanoidNumber result(*this);
187 std::string Str() const;
188 static char OpChar(Optype op)
190 static char opch[] = {'+','-','*','/'};
191 return opch[(int)op];
194 static int64_t Paranoia() {return g_count;}
197 static int64_t g_count;
199 void SimplifyTerms();
200 void SimplifyFactors();
205 ParanoidNumber * m_next_term;
206 ParanoidNumber * m_next_factor;
210 T ParanoidNumber::AddTerms() const
213 for (ParanoidNumber * a = m_next_term; a != NULL; a = a->m_next_term)
215 value += a->Head<T>() * a->MultiplyFactors<T>();
221 T ParanoidNumber::MultiplyFactors() const
224 for (ParanoidNumber * a = m_next_factor; a != NULL; a = a->m_next_factor)
226 if (a->m_op == DIVIDE)
227 value /= (a->Head<T>() + a->AddTerms<T>());
229 value *= (a->Head<T>() + a->AddTerms<T>());
237 T ParanoidNumber::Convert() const
239 return Head<T>() * MultiplyFactors<T>() + AddTerms<T>();
246 #endif //_PARANOIDNUMBER_H