1 #ifndef _PARANOIDNUMBER_H
2 #define _PARANOIDNUMBER_H
13 #define PARANOID_DIGIT_T float // we could theoretically replace this with a template
14 // but let's not do that...
18 typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, NOP} Optype;
19 inline Optype InverseOp(Optype op)
21 return ((op == ADD) ? SUBTRACT :
22 (op == SUBTRACT) ? ADD :
23 (op == MULTIPLY) ? DIVIDE :
24 (op == DIVIDE) ? MULTIPLY :
25 (op == NOP) ? NOP : NOP);
29 inline char OpChar(int op)
31 static char opch[] = {'+','-','*','/'};
32 return (op < NOP && op >= 0) ? opch[op] : '?';
36 /** Performs an operation, returning if the result was exact **/
37 // NOTE: DIFFERENT to ParanoidOp (although that wraps to this...)
38 template <class T> bool TrustingOp(T & a, const T & b, Optype op);
40 /** Performs an operation _only_ if the result would be exact **/
41 template <class T> bool ParanoidOp(T & a, const T & b, Optype op)
44 if (TrustingOp<T>(cpy, b, op))
51 template <> bool TrustingOp<float>(float & a, const float & b, Optype op);
52 template <> bool TrustingOp<double>(double & a, const double & b, Optype op);
53 template <> bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op);
57 * Idea: Perform regular floating point arithmetic but rearrange operations to only ever use exact results
58 * Memory Usage: O(all of it)
59 * CPU Usage: O(all of it)
60 * Accuracy: O(gives better result for 0.3+0.3+0.3, gives same result for everything else, or worse result)
62 * The ParanoidNumber basically stores 4 linked lists which can be split into two "dimensions"
63 * 1. Terms to ADD and terms to SUBTRACT
64 * 2. Factors to MULTIPLY and DIVIDE
65 * Because ADD and SUBTRACT are inverse operations and MULTIPLY and DIVIDE are inverse operations
66 * See paranoidnumber.cpp and the ParanoidNumber::Operation function
72 typedef PARANOID_DIGIT_T digit_t;
74 ParanoidNumber(digit_t value=0) : m_value(value), m_cached_result(value)
79 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value), m_cached_result(cpy.m_cached_result)
82 for (int i = 0; i < NOP; ++i)
84 for (auto next : cpy.m_next[i])
85 m_next[i].push_back(new ParanoidNumber(*next));
89 ParanoidNumber(const char * str);
90 ParanoidNumber(const std::string & str) : ParanoidNumber(str.c_str()) {}
92 virtual ~ParanoidNumber();
94 inline void Construct()
100 template <class T> T Convert() const;
101 digit_t GetFactors();
105 double ToDouble() {return (double)Digit();}
108 bool Floating() const
110 return NoFactors() && NoTerms();
112 bool Sunken() const {return !Floating();} // I could not resist...
114 bool NoFactors() const {return (m_next[MULTIPLY].size() == 0 && m_next[DIVIDE].size() == 0);}
115 bool NoTerms() const {return (m_next[ADD].size() == 0 && m_next[SUBTRACT].size() == 0);}
117 ParanoidNumber & operator+=(const ParanoidNumber & a);
118 ParanoidNumber & operator-=(const ParanoidNumber & a);
119 ParanoidNumber & operator*=(const ParanoidNumber & a);
120 ParanoidNumber & operator/=(const ParanoidNumber & a);
121 ParanoidNumber & operator=(const ParanoidNumber & a);
123 ParanoidNumber * OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
124 ParanoidNumber * OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
125 ParanoidNumber * TrivialOp(ParanoidNumber * b, Optype op);
126 ParanoidNumber * Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
127 bool Simplify(Optype op);
130 bool operator<(ParanoidNumber & a) {return ToDouble() < a.ToDouble();}
131 bool operator<=(ParanoidNumber & a) {return this->operator<(a) || this->operator==(a);}
132 bool operator>(ParanoidNumber & a) {return !(this->operator<=(a));}
133 bool operator>=(ParanoidNumber & a) {return !(this->operator<(a));}
134 bool operator==(ParanoidNumber & a) {return ToDouble() == a.ToDouble();}
135 bool operator!=(ParanoidNumber & a) {return !(this->operator==(a));}
137 ParanoidNumber operator+(const ParanoidNumber & a) const
139 ParanoidNumber result(*this);
143 ParanoidNumber operator-(const ParanoidNumber & a) const
145 ParanoidNumber result(*this);
149 ParanoidNumber operator*(const ParanoidNumber & a) const
151 ParanoidNumber result(*this);
155 ParanoidNumber operator/(const ParanoidNumber & a) const
157 ParanoidNumber result(*this);
162 std::string Str() const;
164 ParanoidNumber * CopyTerms()
166 ParanoidNumber * copy = new ParanoidNumber(*this);
169 copy->Simplify(SUBTRACT);
173 ParanoidNumber * CopyFactors()
175 ParanoidNumber * copy = new ParanoidNumber(*this);
177 copy->Simplify(MULTIPLY);
178 copy->Simplify(DIVIDE);
183 static int64_t Paranoia() {return g_count;}
185 std::string PStr() const;
188 static int64_t g_count;
190 void SimplifyTerms();
191 void SimplifyFactors();
196 std::vector<ParanoidNumber*> m_next[4];
197 digit_t m_cached_result;
205 T ParanoidNumber::Convert() const
207 if (!isnan(m_cached_result))
208 return (T)m_cached_result;
210 for (auto mul : m_next[MULTIPLY])
212 value *= mul->Convert<T>();
214 for (auto div : m_next[DIVIDE])
216 value /= div->Convert<T>();
218 for (auto add : m_next[ADD])
219 value += add->Convert<T>();
220 for (auto sub : m_next[SUBTRACT])
221 value -= sub->Convert<T>();
229 #endif //_PARANOIDNUMBER_H