1 #include "paranoidnumber.h"
11 int64_t ParanoidNumber::g_count = 0;
13 ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_op(ADD), m_next_term(NULL), m_next_factor(NULL)
17 while (str[dp] != '\0' && str[dp] != '.')
22 while (str[end] != '\0')
26 for (int i = dp-1; i >= 0; --i)
28 ParanoidNumber b(str[i]-'0');
30 //Debug("m is %s", m.Str().c_str());
31 //Debug("Add %s", b.Str().c_str());
33 //Debug("Now at %s", Str().c_str());
37 for (int i = dp+1; i < end; ++i)
40 ParanoidNumber b(str[i]-'0');
41 //Debug("%s * %s", b.Str().c_str(), n.Str().c_str());
43 //Debug("b -> %s", b.Str().c_str());
44 //Debug("Add %s", b.Str().c_str());
46 //Debug("Now at %s", Str().c_str());
49 //Debug("Constructed {%s} from %s (%f)", Str().c_str(), str, ToDouble());
52 ParanoidNumber & ParanoidNumber::operator=(const ParanoidNumber & a)
58 if (a.m_next_term != NULL)
60 m_next_term = new ParanoidNumber(*(a.m_next_term));
62 if (a.m_next_factor != NULL)
64 m_next_factor = new ParanoidNumber(*(a.m_next_factor));
69 ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
71 if (m_next_factor == NULL && a.Floating())
73 if (ParanoidOp<digit_t>(m_value, a.m_value, ADD))
79 ParanoidNumber * nt = m_next_term;
80 ParanoidNumber * nf = m_next_factor;
83 if (m_next_factor != NULL)
85 if (m_next_factor->m_op == MULTIPLY)
86 ca /= (*m_next_factor);
88 ca *= (*m_next_factor);
103 m_next_term = new ParanoidNumber(a, ADD);
104 ParanoidNumber * t = m_next_term;
105 while (t->m_next_term != NULL)
108 //Debug("Simplify {%s} after add", Str().c_str());
113 ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
115 // this = v + t + (a)
117 if (m_next_factor == NULL && a.Floating())
119 if (ParanoidOp<digit_t>(m_value, a.m_value, ADD))
126 ParanoidNumber * nt = m_next_term;
127 ParanoidNumber * nf = m_next_factor;
129 ParanoidNumber ca(a, SUBTRACT);
130 if (m_next_factor != NULL)
132 if (m_next_factor->m_op == MULTIPLY)
133 ca /= (*m_next_factor);
135 ca *= (*m_next_factor);
139 m_next_factor = NULL;
150 m_next_term = new ParanoidNumber(a,SUBTRACT);
151 ParanoidNumber * t = m_next_term;
152 while (t->m_next_term != NULL)
158 //Debug("next term {%s}", m_next_term->Str().c_str());
160 //Debug("Simplify {%s} after sub", Str().c_str());
165 ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
170 //Debug("{%s} *= {%s}", Str().c_str(), a.Str().c_str());
171 // this = (vf + t) * (a)
172 if (a.Floating() && ParanoidOp<digit_t>(m_value, a.m_value, MULTIPLY))
174 if (m_next_term != NULL)
175 m_next_term->operator*=(a);
180 ParanoidNumber * t = this;
181 while (t->m_next_factor != NULL)
182 t = t->m_next_factor;
183 t->m_next_factor = new ParanoidNumber(a, MULTIPLY);
185 if (m_next_term != NULL)
186 m_next_term->operator*=(a);
188 //Debug("Simplify {%s}", Str().c_str());
190 //Debug("Simplified to {%s}", Str().c_str());
195 ParanoidNumber & ParanoidNumber::operator/=(const ParanoidNumber & a)
200 if (a.Floating() && ParanoidOp<digit_t>(m_value, a.m_value, DIVIDE))
202 if (m_next_term != NULL)
203 m_next_term->operator/=(a);
208 //Debug("Called %s /= %s", Str().c_str(), a.Str().c_str());
209 // this = (vf + t) * (a)
210 ParanoidNumber * t = this;
211 while (t->m_next_factor != NULL)
213 t = t->m_next_factor;
215 t->m_next_factor = new ParanoidNumber(a, DIVIDE);
217 if (m_next_term != NULL)
218 m_next_term->operator/=(a);
226 void ParanoidNumber::SimplifyTerms()
229 //Debug("Simplify {%s}", Str().c_str());
230 if (m_next_term == NULL)
232 //Debug("No terms!");
236 for (ParanoidNumber * a = this; a != NULL; a = a->m_next_term)
238 ParanoidNumber * b = a->m_next_term;
239 if (a->m_next_factor != NULL && !a->m_next_factor->Floating())
244 ParanoidNumber * bprev = a;
247 //Debug("Simplify factors of %s", b->Str().c_str());
248 b->SimplifyFactors();
249 if (b->m_next_factor != NULL && !b->m_next_factor->Floating())
256 bool simplify = false;
257 if (a->m_next_factor != NULL || b->m_next_factor != NULL)
259 digit_t aa(a->Head<digit_t>());
260 digit_t ab = (a->m_next_factor != NULL) ? a->m_next_factor->Head<digit_t>() : 1;
261 digit_t bc(b->Head<digit_t>());
262 digit_t bd = (b->m_next_factor != NULL) ? b->m_next_factor->Head<digit_t>() : 1;
263 Optype aop = (a->m_next_factor != NULL) ? a->m_next_factor->m_op : DIVIDE;
264 Optype cop = (b->m_next_factor != NULL) ? b->m_next_factor->m_op : DIVIDE;
265 simplify = CombineTerms<digit_t>(aa, aop, ab, bc, cop, bd);
269 if (a->m_next_factor != NULL)
270 a->m_next_factor->m_value = ab;
273 a->m_next_factor = b->m_next_factor;
274 b->m_next_factor = NULL;
275 a->m_next_factor->m_value = ab;
281 simplify = ParanoidOp<digit_t>(a->m_value, b->Head<digit_t>(), ADD);
285 bprev->m_next_term = b->m_next_term;
286 b->m_next_term = NULL;
297 void ParanoidNumber::SimplifyFactors()
300 //Debug("Simplify {%s}", Str().c_str());
301 if (m_next_factor == NULL)
303 //Debug("No factors!");
307 for (ParanoidNumber * a = this; a != NULL; a = a->m_next_factor)
309 if ((a->m_op != ADD || a->m_op != SUBTRACT) && a->m_next_term != NULL)
312 ParanoidNumber * bprev = a;
313 ParanoidNumber * b = a->m_next_factor;
317 if (b->m_next_term != NULL)
320 b = b->m_next_factor;
325 if (a->m_op == DIVIDE)
327 op = (b->m_op == DIVIDE) ? MULTIPLY : DIVIDE;
330 if (ParanoidOp<digit_t>(a->m_value, b->m_value, op))
333 bprev->m_next_factor = b->m_next_factor;
334 b->m_next_factor = NULL;
339 b = b->m_next_factor;
344 void ParanoidNumber::Simplify()
350 string ParanoidNumber::Str() const
354 s << (double)m_value;
356 if (m_next_factor != NULL)
359 result += OpChar(m_next_factor->m_op);
360 if (m_next_factor->m_next_term != NULL)
361 result += "(" + m_next_factor->Str() + ")";
363 result += m_next_factor->Str();
370 if (m_next_term != NULL)
373 result += OpChar(m_next_term->m_op);
374 result += m_next_term->Str();
380 bool TrustingOp<float>(float & a, const float & b, Optype op)
382 feclearexcept(FE_ALL_EXCEPT);
398 return !fetestexcept(FE_ALL_EXCEPT);
402 bool TrustingOp<double>(double & a, const double & b, Optype op)
404 feclearexcept(FE_ALL_EXCEPT);
420 return !fetestexcept(FE_ALL_EXCEPT);
424 bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op)
432 exact = (abs(sa) <= 127);
436 exact = (abs(sa) <= 127);
440 exact = (abs(sa) <= 127);
443 exact = (b != 0 && sa > b && sa % b == 0);