1 #ifndef _PARANOIDNUMBER_H
2 #define _PARANOIDNUMBER_H
12 #include <cassert> // it's going to be ok
15 #define PARANOID_DIGIT_T float // we could theoretically replace this with a template
16 // but let's not do that...
20 typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, NOP} Optype;
21 inline Optype InverseOp(Optype op)
23 return ((op == ADD) ? SUBTRACT :
24 (op == SUBTRACT) ? ADD :
25 (op == MULTIPLY) ? DIVIDE :
26 (op == DIVIDE) ? MULTIPLY :
27 (op == NOP) ? NOP : NOP);
31 inline char OpChar(int op)
33 static char opch[] = {'+','-','*','/'};
34 return (op < NOP && op >= 0) ? opch[op] : '?';
38 /** Performs an operation, returning if the result was exact **/
39 // NOTE: DIFFERENT to ParanoidOp (although that wraps to this...)
40 template <class T> bool TrustingOp(T & a, const T & b, Optype op);
42 /** Performs an operation _only_ if the result would be exact **/
43 template <class T> bool ParanoidOp(T & a, const T & b, Optype op)
46 if (TrustingOp<T>(cpy, b, op))
53 template <> bool TrustingOp<float>(float & a, const float & b, Optype op);
54 template <> bool TrustingOp<double>(double & a, const double & b, Optype op);
55 template <> bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op);
59 * Idea: Perform regular floating point arithmetic but rearrange operations to only ever use exact results
60 * Memory Usage: O(all of it)
61 * CPU Usage: O(all of it)
62 * Accuracy: O(gives better result for 0.3+0.3+0.3, gives same result for everything else, or worse result)
64 * The ParanoidNumber basically stores 4 linked lists which can be split into two "dimensions"
65 * 1. Terms to ADD and terms to SUBTRACT
66 * 2. Factors to MULTIPLY and DIVIDE
67 * Because ADD and SUBTRACT are inverse operations and MULTIPLY and DIVIDE are inverse operations
68 * See paranoidnumber.cpp and the ParanoidNumber::Operation function
74 typedef PARANOID_DIGIT_T digit_t;
76 ParanoidNumber(PARANOID_DIGIT_T value=0) : m_value(value), m_cached_result(value), m_cache_valid(true), m_next()
79 assert(SanityCheck());
82 static ParanoidNumber * SafeConstruct(const ParanoidNumber & cpy)
84 ParanoidNumber * result = new ParanoidNumber(cpy);
85 assert(result != NULL);
86 assert(result->SanityCheck());
90 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_cached_result(cpy.m_cached_result), m_cache_valid(cpy.m_cache_valid), m_next()
93 for (int i = 0; i < NOP; ++i)
95 for (auto next : cpy.m_next[i])
97 if (next != NULL) // why would this ever be null
98 m_next[i].push_back(new ParanoidNumber(*next)); // famous last words...
101 assert(SanityCheck());
104 //ParanoidNumber(const char * str);
105 ParanoidNumber(const std::string & str);// : ParanoidNumber(str.c_str()) {}
107 virtual ~ParanoidNumber();
109 inline void Construct()
111 for (int i = 0; i < NOP; ++i)
116 bool SanityCheck(std::set<ParanoidNumber*> & visited) const;
117 bool SanityCheck() const
119 std::set<ParanoidNumber*> s;
120 return SanityCheck(s);
123 template <class T> T Convert() const;
124 digit_t GetFactors() const;
125 digit_t GetTerms() const;
127 // This function is declared const purely to trick the compiler.
128 // It is not actually const, and therefore, none of the other functions that call it are const either.
129 digit_t Digit() const;
131 // Like this one. It isn't const.
132 double ToDouble() const {return (double)Digit();}
134 // This one is probably const.
135 bool Floating() const
137 return NoFactors() && NoTerms();
139 bool Sunken() const {return !Floating();} // I could not resist...
141 bool NoFactors() const {return (m_next[MULTIPLY].size() == 0 && m_next[DIVIDE].size() == 0);}
142 bool NoTerms() const {return (m_next[ADD].size() == 0 && m_next[SUBTRACT].size() == 0);}
144 ParanoidNumber & operator+=(const ParanoidNumber & a);
145 ParanoidNumber & operator-=(const ParanoidNumber & a);
146 ParanoidNumber & operator*=(const ParanoidNumber & a);
147 ParanoidNumber & operator/=(const ParanoidNumber & a);
148 ParanoidNumber & operator=(const ParanoidNumber & a);
150 ParanoidNumber * OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
151 ParanoidNumber * OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
152 ParanoidNumber * TrivialOp(ParanoidNumber * b, Optype op);
153 ParanoidNumber * Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
154 bool Simplify(Optype op);
158 // None of these are actually const
159 bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
160 bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
161 bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
162 bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
163 bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
164 bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
166 ParanoidNumber operator-() const
168 ParanoidNumber neg(0);
173 ParanoidNumber operator+(const ParanoidNumber & a) const
175 ParanoidNumber result(*this);
179 ParanoidNumber operator-(const ParanoidNumber & a) const
181 ParanoidNumber result(*this);
185 ParanoidNumber operator*(const ParanoidNumber & a) const
187 ParanoidNumber result(*this);
191 ParanoidNumber operator/(const ParanoidNumber & a) const
193 ParanoidNumber result(*this);
198 std::string Str() const;
200 ParanoidNumber * CopyTerms()
202 ParanoidNumber * copy = new ParanoidNumber(*this);
205 copy->Simplify(SUBTRACT);
209 ParanoidNumber * CopyFactors()
211 ParanoidNumber * copy = new ParanoidNumber(*this);
213 copy->Simplify(MULTIPLY);
214 copy->Simplify(DIVIDE);
219 static int64_t Paranoia() {return g_count;}
221 std::string PStr() const;
224 static int64_t g_count;
226 void SimplifyTerms();
227 void SimplifyFactors();
233 digit_t m_cached_result;
235 std::vector<ParanoidNumber*> m_next[4];
242 T ParanoidNumber::Convert() const
244 if (!isnan(m_cached_result))
245 return (T)m_cached_result;
247 for (auto mul : m_next[MULTIPLY])
249 value *= mul->Convert<T>();
251 for (auto div : m_next[DIVIDE])
253 value /= div->Convert<T>();
255 for (auto add : m_next[ADD])
256 value += add->Convert<T>();
257 for (auto sub : m_next[SUBTRACT])
258 value -= sub->Convert<T>();
266 #endif //_PARANOIDNUMBER_H