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)
79 ParanoidNumber(const ParanoidNumber & cpy) : m_value(cpy.m_value)
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;
102 double ToDouble() const {return Convert<double>();}
103 digit_t Digit() const {return Convert<digit_t>();}
105 bool Floating() const
107 return NoFactors() && NoTerms();
109 bool Sunken() const {return !Floating();} // I could not resist...
111 bool NoFactors() const {return (m_next[MULTIPLY].size() == 0 && m_next[DIVIDE].size() == 0);}
112 bool NoTerms() const {return (m_next[ADD].size() == 0 && m_next[SUBTRACT].size() == 0);}
114 ParanoidNumber & operator+=(const ParanoidNumber & a);
115 ParanoidNumber & operator-=(const ParanoidNumber & a);
116 ParanoidNumber & operator*=(const ParanoidNumber & a);
117 ParanoidNumber & operator/=(const ParanoidNumber & a);
118 ParanoidNumber & operator=(const ParanoidNumber & a);
120 ParanoidNumber * OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
121 ParanoidNumber * OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
122 ParanoidNumber * TrivialOp(ParanoidNumber * b, Optype op);
123 ParanoidNumber * Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point = NULL, Optype * mop = NULL);
124 bool Simplify(Optype op);
127 bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
128 bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
129 bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
130 bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
131 bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
132 bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
134 ParanoidNumber operator+(const ParanoidNumber & a) const
136 ParanoidNumber result(*this);
140 ParanoidNumber operator-(const ParanoidNumber & a) const
142 ParanoidNumber result(*this);
146 ParanoidNumber operator*(const ParanoidNumber & a) const
148 ParanoidNumber result(*this);
152 ParanoidNumber operator/(const ParanoidNumber & a) const
154 ParanoidNumber result(*this);
159 std::string Str() const;
161 ParanoidNumber * CopyTerms()
163 ParanoidNumber * copy = new ParanoidNumber(*this);
166 copy->Simplify(SUBTRACT);
170 ParanoidNumber * CopyFactors()
172 ParanoidNumber * copy = new ParanoidNumber(*this);
174 copy->Simplify(MULTIPLY);
175 copy->Simplify(DIVIDE);
180 static int64_t Paranoia() {return g_count;}
182 std::string PStr() const;
185 static int64_t g_count;
187 void SimplifyTerms();
188 void SimplifyFactors();
193 std::vector<ParanoidNumber*> m_next[4];
199 T ParanoidNumber::Convert() const
202 for (auto mul : m_next[MULTIPLY])
204 value *= mul->Digit();
206 for (auto div : m_next[DIVIDE])
208 value /= div->Digit();
210 for (auto add : m_next[ADD])
211 value += add->Digit();
212 for (auto sub : m_next[SUBTRACT])
213 value -= sub->Digit();
219 #endif //_PARANOIDNUMBER_H