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 #define XSTR(x) STR(x)
25 #pragma message "REALTYPE = " XSTR(REALTYPE)
27 #if REALTYPE == REAL_VFPU
31 #if REALTYPE == REAL_RATIONAL
35 #if REALTYPE == REAL_RATIONAL_ARBINT
41 #if REALTYPE == REAL_MPFRCPP
45 #if REALTYPE == REAL_IRRAM
46 #include "../contrib/iRRAM/include/iRRAM/lib.h"
49 #if REALTYPE == REAL_PARANOIDNUMBER
50 #include "paranoidnumber.h"
53 #if REALTYPE == REAL_GMPRAT
59 extern const char * g_real_name[];
61 #if REALTYPE == REAL_SINGLE
63 inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
64 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
65 #elif REALTYPE == REAL_DOUBLE
67 inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
68 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
69 #elif REALTYPE == REAL_LONG_DOUBLE
70 typedef long double Real;
71 inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
72 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
73 #elif REALTYPE == REAL_VFPU
74 typedef VFPU::VFloat Real;
75 inline float Float(const Real & r) {return r.m_value;}
76 inline double Double(const Real & r) {return r.m_value;}
77 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
78 #elif REALTYPE == REAL_RATIONAL
79 typedef Rational<int64_t> Real;
80 inline float Float(const Real & r) {return (float)r.ToDouble();}
81 inline double Double(const Real & r) {return r.ToDouble();}
82 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
83 #elif REALTYPE == REAL_RATIONAL_ARBINT
84 #define ARBINT Arbint // Set to Gmpint or Arbint here
86 typedef Rational<ARBINT> Real;
87 inline float Float(const Real & r) {return (float)r.ToDouble();}
88 inline double Double(const Real & r) {return r.ToDouble();}
89 inline int64_t Int64(const Real & r) {return r.ToInt64();}
90 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
91 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
92 #elif REALTYPE == REAL_MPFRCPP
93 typedef mpfr::mpreal Real;
94 inline double Double(const Real & r) {return r.toDouble();}
95 inline float Float(const Real & r) {return r.toDouble();}
96 inline int64_t Int64(const Real & r) {return r.toLong();}
97 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
98 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
99 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
100 #elif REALTYPE == REAL_IRRAM
101 typedef iRRAM::REAL Real;
102 inline double Double(const Real & r) {return r.as_double(53);}
103 inline float Float(const Real & r) {return r.as_double(53);}
104 inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
105 inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
106 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
107 #elif REALTYPE == REAL_PARANOIDNUMBER
108 typedef ParanoidNumber Real;
109 inline double Double(const Real & r) {return r.Digit();}
110 inline float Float(const Real & r) {return r.Digit();}
111 inline int64_t Int64(const Real & r) {return (int64_t)r.Digit();}
112 inline Real Sqrt(const Real & r) {return Real(sqrt(r.Digit()));}
113 inline Real RealFromStr(const char * str) {return Real(str);}
114 inline Real Abs(const Real & a) {return Real(fabs(a.Digit()));}
115 #elif REALTYPE == REAL_GMPRAT
117 inline double Double(const Real & r) {return r.ToDouble();}
118 inline float Float(const Real & r) {return (float)(r.ToDouble());}
119 inline int64_t Int64(const Real & r) {return (int64_t)r.ToDouble();}
120 inline Real Sqrt(const Real & r) {return Real(sqrt(r.ToDouble()));}
121 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
122 inline Real Abs(const Real & a) {return (a > Real(0)) ? a : Real(0)-a;}
123 inline std::string Str(const Real & a) {return a.Str();}
126 #error "Type of Real unspecified."
129 // Allow us to call Float on the primative types
130 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
131 // Mostly in the testers.
132 inline float Float(double f) {return (float)f;}
133 inline float Float(long double f) {return (float)(f);}
134 inline double Double(float f) {return (double)f;}
135 inline double Double(double f) {return (double)f;}
136 inline double Double(long double f) {return (double)(f);}
137 inline double Sqrt(double f) {return sqrt(f);}
138 inline double Abs(double a) {return fabs(a);}
141 inline int64_t Int64(double a)
150 inline Real Power(const Real & a, int n)
154 return Power(Real(1)/a, -n);
157 for (int i = 0; i < n; ++i)
165 Vec2() : x(0.0), y(0.0) {}
166 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
167 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
168 #if REALTYPE != REAL_IRRAM
169 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
171 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
172 // Apparently iRRAM didn't implement -= for a constant argument
174 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
175 bool operator!=(const Vec2& other) const { return !(*this == other); }
177 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
178 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
179 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
180 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
181 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
183 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
184 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
185 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
186 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
188 const Real SquareLength() const { return (x*x + y*y); }
192 //TODO: Make sure there is actually a RealFromStr(const char * str) function
193 // Or this will recurse infinitely
194 // (If you remove this it will also break).
195 inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}
198 inline void DebugRealInfo()
200 Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REALTYPE, g_real_name[REALTYPE], sizeof(Real));
201 #if REALTYPE == REAL_PARANOIDNUMBER
202 #ifdef PARANOID_SIZE_LIMIT
203 Debug("Size limit of %d is being enforced", PARANOID_SIZE_LIMIT);