Add loadsvg script command, fix ParanoidNumber size limiting*
[ipdf/code.git] / src / paranoidnumber.h
index b152922..041e445 100644 (file)
                                                                // but let's not do that...
                                                                
 
-#define PARANOID_CACHE_RESULTS
+//#define PARANOID_CACHE_RESULTS
 
 //#define PARANOID_USE_ARENA
-#define PARANOID_SIZE_LIMIT 0
+#define PARANOID_SIZE_LIMIT 1
 
 
+// Define to compare all ops against double ops and check within epsilon
+#define PARANOID_COMPARE_EPSILON 1e-6
+#define CompareForSanity(...) ParanoidNumber::CompareForSanityEx(__func__, __FILE__, __LINE__, __VA_ARGS__)
+
 namespace IPDF
 {
        typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, NOP} Optype;
@@ -83,7 +87,7 @@ namespace IPDF
                        ParanoidNumber(PARANOID_DIGIT_T value=0) : m_value(value), m_next()
                        {
                                #ifdef PARANOID_SIZE_LIMIT
-                                       m_size = 0;
+                                       m_size = 1;
                                #endif
                                #ifdef PARANOID_CACHE_RESULTS
                                        m_cached_result = value;
@@ -107,6 +111,9 @@ namespace IPDF
                                                        m_next[i].push_back(new ParanoidNumber(*next)); // famous last words...
                                        }
                                }
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       CompareForSanity(cpy.Digit(), cpy.Digit());
+                               #endif
                                //assert(SanityCheck());
                        }
                        
@@ -167,48 +174,76 @@ namespace IPDF
                        
                        
                        // None of these are actually const
-                       bool operator<(const ParanoidNumber & a) const {return ToDouble() < a.ToDouble();}
-                       bool operator<=(const ParanoidNumber & a) const {return this->operator<(a) || this->operator==(a);}
-                       bool operator>(const ParanoidNumber & a) const {return !(this->operator<=(a));}
-                       bool operator>=(const ParanoidNumber & a) const {return !(this->operator<(a));}
-                       bool operator==(const ParanoidNumber & a) const {return ToDouble() == a.ToDouble();}
-                       bool operator!=(const ParanoidNumber & a) const {return !(this->operator==(a));}
+                       bool operator<(const ParanoidNumber & a) const {return Digit() < a.Digit();}
+                       bool operator<=(const ParanoidNumber & a) const {return Digit() <= a.Digit();}
+                       bool operator>(const ParanoidNumber & a) const {return Digit() > a.Digit();}
+                       bool operator>=(const ParanoidNumber & a) const {return Digit() >= a.Digit();}
+                       bool operator==(const ParanoidNumber & a) const {return Digit() == a.Digit();}
+                       bool operator!=(const ParanoidNumber & a) const {return Digit() != a.Digit();}
                        
                        ParanoidNumber operator-() const
                        {
-                               ParanoidNumber neg(0);
-                               neg -= *this;
+                               ParanoidNumber neg(*this);
+                               neg.Negate();
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       neg.CompareForSanity(-Digit(), Digit());
+                               #endif
                                return neg;
                        }
                        
+                       void Negate();
+                       
+                       
                        ParanoidNumber operator+(const ParanoidNumber & a) const
                        {
                                ParanoidNumber result(*this);
                                result += a;
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       result.CompareForSanity(Digit()+a.Digit(), a.Digit());
+                               #endif
                                return result;
                        }
                        ParanoidNumber operator-(const ParanoidNumber & a) const
                        {
                                ParanoidNumber result(*this);
                                result -= a;
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       result.CompareForSanity(Digit()-a.Digit(), a.Digit());
+                               #endif
                                return result;
                        }
                        ParanoidNumber operator*(const ParanoidNumber & a) const
                        {
                                ParanoidNumber result(*this);
                                result *= a;
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       result.CompareForSanity(Digit()*a.Digit(), a.Digit());
+                               #endif
                                return result;
                        }
                        ParanoidNumber operator/(const ParanoidNumber & a) const
                        {
                                ParanoidNumber result(*this);
                                result /= a;
+                               #ifdef PARANOID_COMPARE_EPSILON
+                                       result.CompareForSanity(Digit()/a.Digit(), a.Digit());
+                               #endif
                                return result;
                        }
                        
                        std::string Str() const;
 
-                       
+                       inline void CompareForSanityEx(const char * func, const char * file, int line, const digit_t & compare, const digit_t & arg, const digit_t & eps = PARANOID_COMPARE_EPSILON)
+                       {
+                               if (!SanityCheck())
+                                       Fatal("This is insane!");
+                               if (fabs(Digit() - compare) > eps)
+                               {
+                                       Error("Called via %s(%lf) (%s:%d)", func, arg, file, line);
+                                       Error("Failed: %s", Str().c_str());
+                                       Fatal("This: %.30lf vs Expected: %.30lf", Digit(), compare);
+                               }
+                       }
 
                        
                        std::string PStr() const;

UCC git Repository :: git.ucc.asn.au