10 #define REAL_LONG_DOUBLE 2
12 #define REAL_RATIONAL 4
13 #define REAL_RATIONAL_ARBINT 5
14 #define REAL_MPFRCPP 6
16 #define REAL_PARANOIDNUMBER 8
20 #error "REALTYPE was not defined!"
23 #if REALTYPE == REAL_VFPU
27 #if REALTYPE == REAL_RATIONAL
31 #if REALTYPE == REAL_RATIONAL_ARBINT
37 #if REALTYPE == REAL_MPFRCPP
41 #if REALTYPE == REAL_IRRAM
42 #include "../contrib/iRRAM/include/iRRAM/lib.h"
45 #if REALTYPE == REAL_PARANOIDNUMBER
46 #include "paranoidnumber.h"
49 #if REALTYPE == REAL_GMPRAT
55 extern const char * g_real_name[];
57 #if REALTYPE == REAL_SINGLE
59 inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
60 #elif REALTYPE == REAL_DOUBLE
62 inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
63 #elif REALTYPE == REAL_LONG_DOUBLE
64 typedef long double Real;
65 inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
66 #elif REALTYPE == REAL_VFPU
67 typedef VFPU::VFloat Real;
68 inline float Float(const Real & r) {return r.m_value;}
69 inline double Double(const Real & r) {return r.m_value;}
70 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
71 #elif REALTYPE == REAL_RATIONAL
72 typedef Rational<int64_t> Real;
73 inline float Float(const Real & r) {return (float)r.ToDouble();}
74 inline double Double(const Real & r) {return r.ToDouble();}
75 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
76 #elif REALTYPE == REAL_RATIONAL_ARBINT
77 #define ARBINT Arbint // Set to Gmpint or Arbint here
79 typedef Rational<ARBINT> Real;
80 inline float Float(const Real & r) {return (float)r.ToDouble();}
81 inline double Double(const Real & r) {return r.ToDouble();}
82 inline int64_t Int64(const Real & r) {return r.ToInt64();}
83 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
84 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
85 #elif REALTYPE == REAL_MPFRCPP
86 typedef mpfr::mpreal Real;
87 inline double Double(const Real & r) {return r.toDouble();}
88 inline float Float(const Real & r) {return r.toDouble();}
89 inline int64_t Int64(const Real & r) {return r.toLong();}
90 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
91 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
92 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
93 #elif REALTYPE == REAL_IRRAM
94 typedef iRRAM::REAL Real;
95 inline double Double(const Real & r) {return r.as_double(53);}
96 inline float Float(const Real & r) {return r.as_double(53);}
97 inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
98 inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
99 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
100 #elif REALTYPE == REAL_PARANOIDNUMBER
101 typedef ParanoidNumber Real;
102 inline double Double(const Real & r) {return r.Digit();}
103 inline float Float(const Real & r) {return r.Digit();}
104 inline int64_t Int64(const Real & r) {return (int64_t)r.Digit();}
105 inline Real Sqrt(const Real & r) {return Real(sqrt(r.Digit()));}
106 inline Real RealFromStr(const char * str) {return Real(str);}
107 inline Real Abs(const Real & a) {return Real(fabs(a.Digit()));}
108 #elif REALTYPE == REAL_GMPRAT
110 inline double Double(const Real & r) {return r.ToDouble();}
111 inline float Float(const Real & r) {return (float)(r.ToDouble());}
112 inline int64_t Int64(const Real & r) {return (int64_t)r.ToDouble();}
113 inline Real Sqrt(const Real & r) {return Real(sqrt(r.ToDouble()));}
114 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
115 inline Real Abs(const Real & a) {return (a > Real(0)) ? a : Real(0)-a;}
118 #error "Type of Real unspecified."
121 // Allow us to call Float on the primative types
122 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
123 // Mostly in the testers.
124 inline float Float(double f) {return (float)f;}
125 inline float Float(long double f) {return (float)(f);}
126 inline double Double(float f) {return (double)f;}
127 inline double Double(double f) {return (double)f;}
128 inline double Double(long double f) {return (double)(f);}
129 inline double Sqrt(double f) {return sqrt(f);}
130 inline double Abs(double a) {return fabs(a);}
131 inline int64_t Int64(double a)
140 inline Real Power(const Real & a, int n)
144 return Power(Real(1)/a, -n);
147 for (int i = 0; i < n; ++i)
155 Vec2() : x(0.0), y(0.0) {}
156 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
157 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
158 #if REALTYPE != REAL_IRRAM
159 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
161 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
162 // Apparently iRRAM didn't implement -= for a constant argument
164 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
165 bool operator!=(const Vec2& other) const { return !(*this == other); }
167 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
168 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
169 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
170 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
171 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
173 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
174 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
175 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
176 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
178 const Real SquareLength() const { return (x*x + y*y); }
182 //TODO: Make sure there is actually a RealFromStr(const char * str) function
183 // Or this will recurse infinitely
184 // (If you remove this it will also break).
185 inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}
188 inline void DebugRealInfo()
190 Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REALTYPE, g_real_name[REALTYPE], sizeof(Real));
191 #if REALTYPE == REAL_PARANOIDNUMBER
192 #ifdef PARANOID_SIZE_LIMIT
193 Debug("Size limit of %d is being enforced", PARANOID_SIZE_LIMIT);