- // Attempt to comine two terms: a*b + c*d or a/b + c/d
- template <class T> bool CombineTerms(T & aa, Optype aop, T & bb, T & cc, Optype cop, T & dd)
- {
- T a(aa); T b(bb); T c(cc); T d(dd);
- if (aop == MULTIPLY && cop == MULTIPLY) // a*b + c*d
- {
-
- if ((ParanoidOp<T>(c, b, DIVIDE) || ParanoidOp(d, b, DIVIDE))
- && TrustingOp<T>(c, d, MULTIPLY) && TrustingOp<T>(a,c,ADD)
- && TrustingOp<T>(a, b, MULTIPLY)) // (a + (cd)/b) * b
- {
- aa = a;
- bb = 1;
- cc = 1;
- dd = 1;
- return true;
- }
- if ((ParanoidOp<T>(a, d, DIVIDE) || ParanoidOp(b, d, DIVIDE))
- && TrustingOp<T>(a, b, MULTIPLY) && TrustingOp<T>(a,c,ADD)
- && TrustingOp<T>(a, d, MULTIPLY)) // ((ab)/d + c)*d
- {
- aa = a;
- bb = 1;
- cc = 1;
- dd = 1;
- return true;
- }
- return false;
- }
- else if (aop == DIVIDE && cop == DIVIDE)
- {
-
-
- if (TrustingOp<T>(a, d, MULTIPLY) && TrustingOp<T>(c, b, MULTIPLY)
- && TrustingOp<T>(a, c, ADD) && TrustingOp<T>(b, d, MULTIPLY))
- {
- cc = 1;
- dd = 1;
- if (ParanoidOp<T>(a, b, DIVIDE))
- {
- aa = a;
- bb = 1;
- return true;
- }
- aa = a;
- bb = b;
- return true;
- }
- return false;
- }
- return false;
- }
-
+ /**
+ * A ParanoidNumber
+ * Idea: Perform regular floating point arithmetic but rearrange operations to only ever use exact results
+ * Memory Usage: O(all of it)
+ * CPU Usage: O(all of it)
+ * Accuracy: O(gives better result for 0.3+0.3+0.3, gives same result for everything else, or worse result)
+ *
+ * The ParanoidNumber basically stores 4 linked lists which can be split into two "dimensions"
+ * 1. Terms to ADD and terms to SUBTRACT
+ * 2. Factors to MULTIPLY and DIVIDE
+ * Because ADD and SUBTRACT are inverse operations and MULTIPLY and DIVIDE are inverse operations
+ * See paranoidnumber.cpp and the ParanoidNumber::Operation function
+ */