1 #ifndef _PARANOIDNUMBER_H
2 #define _PARANOIDNUMBER_H
12 #include <cassert> // it's going to be ok
15 #define PARANOID_DIGIT_T double // we could theoretically replace this with a template
16 // but let's not do that...
19 #define PARANOID_CACHE_RESULTS
21 //#define PARANOID_USE_ARENA
22 #define PARANOID_SIZE_LIMIT 0
27 typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, NOP} Optype;
28 inline Optype InverseOp(Optype op)
30 return ((op == ADD) ? SUBTRACT :
31 (op == SUBTRACT) ? ADD :
32 (op == MULTIPLY) ? DIVIDE :
33 (op == DIVIDE) ? MULTIPLY :
34 (op == NOP) ? NOP : NOP);
38 inline char OpChar(int op)
40 static char opch[] = {'+','-','*','/'};
41 return (op < NOP && op >= 0) ? opch[op] : '?';
45 /** Performs an operation, returning if the result was exact **/
46 // NOTE: DIFFERENT to ParanoidOp (although that wraps to this...)
47 template <class T> bool TrustingOp(T & a, const T & b, Optype op);
49 /** Performs an operation _only_ if the result would be exact **/
50 template <class T> bool ParanoidOp(T & a, const T & b, Optype op)
53 if (TrustingOp<T>(cpy, b, op))
60 template <> bool TrustingOp<float>(float & a, const float & b, Optype op);
61 template <> bool TrustingOp<double>(double & a, const double & b, Optype op);
62 template <> bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op);
66 * Idea: Perform regular floating point arithmetic but rearrange operations to only ever use exact results
67 * Memory Usage: O(all of it)
68 * CPU Usage: O(all of it)
69 * Accuracy: O(gives better result for 0.3+0.3+0.3, gives same result for everything else, or worse result)
71 * The ParanoidNumber basically stores 4 linked lists which can be split into two "dimensions"
72 * 1. Terms to ADD and terms to SUBTRACT
73 * 2. Factors to MULTIPLY and DIVIDE
74 * Because ADD and SUBTRACT are inverse operations and MULTIPLY and DIVIDE are inverse operations
75 * See paranoidnumber.cpp and the ParanoidNumber::Operation function
81 typedef PARANOID_DIGIT_T digit_t;
83 ParanoidNumber(PARANOID_DIGIT_T value=0) : m_value(value), m_next()
85 #ifdef PARANOID_SIZE_LIMIT
88 #ifdef PARANOID_CACHE_RESULTS
89 m_cached_result = value;
93 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_next()
96 #ifdef PARANOID_SIZE_LIMIT
99 #ifdef PARANOID_CACHE_RESULTS
100 m_cached_result = cpy.m_cached_result;
102 for (int i = 0; i < NOP; ++i)
104 for (auto next : cpy.m_next[i])
106 if (next != NULL) // why would this ever be null
107 m_next[i].push_back(new ParanoidNumber(*next)); // famous last words...
110 //assert(SanityCheck());
113 //ParanoidNumber(const char * str);
114 ParanoidNumber(const std::string & str);// : ParanoidNumber(str.c_str()) {}
116 virtual ~ParanoidNumber();
119 bool SanityCheck(std::set<ParanoidNumber*> & visited) const;
120 bool SanityCheck() const
122 std::set<ParanoidNumber*> s;
123 return SanityCheck(s);
126 template <class T> T Convert() const;
127 digit_t GetFactors() const;
128 digit_t GetTerms() const;
130 // This function is declared const purely to trick the compiler.
131 // It is not actually const, and therefore, none of the other functions that call it are const either.
132 digit_t Digit() const;
134 // Like this one. It isn't const.
135 double ToDouble() const {return (double)Digit();}
137 // This one is probably const.
138 bool Floating() const
140 return NoFactors() && NoTerms();
142 bool Sunken() const {return !Floating();} // I could not resist...
144 bool NoFactors() const {return (m_next[MULTIPLY].size() == 0 && m_next[DIVIDE].size() == 0);}
145 bool NoTerms() const {return (m_next[ADD].size() == 0 && m_next[SUBTRACT].size() == 0);}
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);
154 ParanoidNumber & operator+=(const digit_t & a);
155 ParanoidNumber & operator-=(const digit_t & a);
156 ParanoidNumber & operator*=(const digit_t & a);
157 ParanoidNumber & operator/=(const digit_t & a);
158 ParanoidNumber & operator=(const digit_t & a);
161 ParanoidNumber * OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
162 ParanoidNumber * OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
163 ParanoidNumber * TrivialOp(ParanoidNumber * b, Optype op);
164 ParanoidNumber * Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
165 bool Simplify(Optype op);
169 // None of these are actually const
170 bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
171 bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
172 bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
173 bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
174 bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
175 bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
177 ParanoidNumber operator-() const
179 ParanoidNumber neg(0);
184 ParanoidNumber operator+(const ParanoidNumber & a) const
186 ParanoidNumber result(*this);
190 ParanoidNumber operator-(const ParanoidNumber & a) const
192 ParanoidNumber result(*this);
196 ParanoidNumber operator*(const ParanoidNumber & a) const
198 ParanoidNumber result(*this);
202 ParanoidNumber operator/(const ParanoidNumber & a) const
204 ParanoidNumber result(*this);
209 std::string Str() const;
214 std::string PStr() const;
216 #ifdef PARANOID_USE_ARENA
217 void * operator new(size_t byes);
218 void operator delete(void * p);
219 #endif //PARANOID_USE_ARENA
224 void SimplifyTerms();
225 void SimplifyFactors();
228 #ifdef PARANOID_CACHE_RESULTS
229 digit_t m_cached_result;
231 std::vector<ParanoidNumber*> m_next[4];
232 #ifdef PARANOID_SIZE_LIMIT
234 #endif //PARANOID_SIZE_LIMIT
236 #ifdef PARANOID_USE_ARENA
240 Arena(int64_t block_size = 10000000);
243 void * allocate(size_t bytes);
244 void deallocate(void * p);
253 std::vector<Block> m_blocks;
254 int64_t m_block_size;
260 static Arena g_arena;
261 #endif //PARANOID_USE_ARENA
270 T ParanoidNumber::Convert() const
272 #ifdef PARANOID_CACHE_RESULTS
273 if (!isnan((float(m_cached_result))))
274 return (T)m_cached_result;
277 for (auto mul : m_next[MULTIPLY])
279 value *= mul->Convert<T>();
281 for (auto div : m_next[DIVIDE])
283 value /= div->Convert<T>();
285 for (auto add : m_next[ADD])
286 value += add->Convert<T>();
287 for (auto sub : m_next[SUBTRACT])
288 value -= sub->Convert<T>();
296 #endif //_PARANOIDNUMBER_H