1 #ifndef _PARANOIDNUMBER_H
2 #define _PARANOIDNUMBER_H
11 #define PARANOID_DIGIT_T float // 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
46 if ((ParanoidOp<T>(c, b, DIVIDE) || ParanoidOp(d, b, DIVIDE))
47 && TrustingOp<T>(c, d, MULTIPLY) && TrustingOp<T>(a,c,ADD)
48 && TrustingOp<T>(a, b, MULTIPLY)) // (a + (cd)/b) * b
56 if ((ParanoidOp<T>(a, d, DIVIDE) || ParanoidOp(b, d, DIVIDE))
57 && TrustingOp<T>(a, b, MULTIPLY) && TrustingOp<T>(a,c,ADD)
58 && TrustingOp<T>(a, d, MULTIPLY)) // ((ab)/d + c)*d
68 else if (aop == DIVIDE && cop == DIVIDE)
72 if (TrustingOp<T>(a, d, MULTIPLY) && TrustingOp<T>(c, b, MULTIPLY)
73 && TrustingOp<T>(a, c, ADD) && TrustingOp<T>(b, d, MULTIPLY))
77 if (ParanoidOp<T>(a, b, DIVIDE))
96 typedef PARANOID_DIGIT_T digit_t;
98 ParanoidNumber(digit_t value=0, Optype type = ADD) : m_value(value), m_op(type), m_next_term(NULL), m_next_factor(NULL)
103 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_op(cpy.m_op), m_next_term(NULL), m_next_factor(NULL)
105 if (cpy.m_next_term != NULL)
107 m_next_term = new ParanoidNumber(*(cpy.m_next_term));
109 if (cpy.m_next_factor != NULL)
111 m_next_factor = new ParanoidNumber(*(cpy.m_next_factor));
116 ParanoidNumber(const ParanoidNumber & cpy, Optype type) : ParanoidNumber(cpy)
121 ParanoidNumber(const char * str);
122 ParanoidNumber(const std::string & str) : ParanoidNumber(str.c_str()) {Construct();}
124 virtual ~ParanoidNumber()
126 if (m_next_term != NULL)
128 if (m_next_factor != NULL)
129 delete m_next_factor;
133 inline void Construct() {g_count++;}
136 template <class T> T Convert() const;
137 template <class T> T AddTerms() const;
138 template <class T> T MultiplyFactors() const;
139 template <class T> T Head() const {return (m_op == SUBTRACT) ? T(-m_value) : T(m_value);}
144 double ToDouble() const {return Convert<double>();}
145 float ToFloat() const {return Convert<float>();}
146 digit_t Digit() const {return Convert<digit_t>();}
148 bool Floating() const {return (m_next_term == NULL && m_next_factor == NULL);}
149 bool Sunken() const {return !Floating();} // I could not resist...
151 ParanoidNumber & operator+=(const ParanoidNumber & a);
152 ParanoidNumber & operator-=(const ParanoidNumber & a);
153 ParanoidNumber & operator*=(const ParanoidNumber & a);
154 ParanoidNumber & operator/=(const ParanoidNumber & a);
155 ParanoidNumber & operator=(const ParanoidNumber & a);
158 bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
159 bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
160 bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
161 bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
162 bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
163 bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
165 ParanoidNumber operator+(const ParanoidNumber & a) const
167 ParanoidNumber result(*this);
171 ParanoidNumber operator-(const ParanoidNumber & a) const
173 ParanoidNumber result(*this);
177 ParanoidNumber operator*(const ParanoidNumber & a) const
179 ParanoidNumber result(*this);
183 ParanoidNumber operator/(const ParanoidNumber & a) const
185 ParanoidNumber result(*this);
190 std::string Str() const;
191 static char OpChar(Optype op)
193 static char opch[] = {'+','-','*','/'};
194 return opch[(int)op];
197 static int64_t Paranoia() {return g_count;}
200 static int64_t g_count;
202 void SimplifyTerms();
203 void SimplifyFactors();
208 ParanoidNumber * m_next_term;
209 ParanoidNumber * m_next_factor;
213 T ParanoidNumber::AddTerms() const
216 for (ParanoidNumber * a = m_next_term; a != NULL; a = a->m_next_term)
218 value += a->Head<T>() * a->MultiplyFactors<T>();
224 T ParanoidNumber::MultiplyFactors() const
227 for (ParanoidNumber * a = m_next_factor; a != NULL; a = a->m_next_factor)
229 if (a->m_op == DIVIDE)
230 value /= (a->Head<T>() + a->AddTerms<T>());
232 value *= (a->Head<T>() + a->AddTerms<T>());
240 T ParanoidNumber::Convert() const
242 return Head<T>() * MultiplyFactors<T>() + AddTerms<T>();
249 #endif //_PARANOIDNUMBER_H