11 #define REAL_LONG_DOUBLE 2
13 #define REAL_RATIONAL 4
14 #define REAL_RATIONAL_ARBINT 5
15 #define REAL_MPFRCPP 6
17 #define REAL_PARANOIDNUMBER 8
21 #error "REALTYPE was not defined!"
24 #define XSTR(x) STR(x)
26 //#pragma message "REALTYPE = " XSTR(REALTYPE)
28 #if REALTYPE == REAL_VFPU
32 #if REALTYPE == REAL_RATIONAL
36 #if REALTYPE == REAL_RATIONAL_ARBINT
42 #if REALTYPE == REAL_MPFRCPP
46 #if REALTYPE == REAL_IRRAM
47 #include "../contrib/iRRAM/include/iRRAM/lib.h"
50 #if REALTYPE == REAL_PARANOIDNUMBER
51 #include "paranoidnumber.h"
54 #if REALTYPE == REAL_GMPRAT
60 extern const char * g_real_name[];
62 #if REALTYPE == REAL_SINGLE
64 inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
65 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
66 #elif REALTYPE == REAL_DOUBLE
68 inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
69 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
70 #elif REALTYPE == REAL_LONG_DOUBLE
71 typedef long double Real;
72 inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
73 inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
74 #elif REALTYPE == REAL_VFPU
75 typedef VFPU::VFloat Real;
76 inline float Float(const Real & r) {return r.m_value;}
77 inline double Double(const Real & r) {return r.m_value;}
78 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
79 #elif REALTYPE == REAL_RATIONAL
80 typedef Rational<int64_t> Real;
81 inline float Float(const Real & r) {return (float)r.ToDouble();}
82 inline double Double(const Real & r) {return r.ToDouble();}
83 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
84 #elif REALTYPE == REAL_RATIONAL_ARBINT
85 #define ARBINT Arbint // Set to Gmpint or Arbint here
87 typedef Rational<ARBINT> Real;
88 inline float Float(const Real & r) {return (float)r.ToDouble();}
89 inline double Double(const Real & r) {return r.ToDouble();}
90 inline int64_t Int64(const Real & r) {return r.ToInt64();}
91 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
92 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
93 #elif REALTYPE == REAL_MPFRCPP
94 typedef mpfr::mpreal Real;
95 inline double Double(const Real & r) {return r.toDouble();}
96 inline float Float(const Real & r) {return r.toDouble();}
97 inline int64_t Int64(const Real & r) {return r.toLong();}
98 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
99 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
100 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
101 #elif REALTYPE == REAL_IRRAM
102 typedef iRRAM::REAL Real;
103 inline double Double(const Real & r) {return r.as_double(53);}
104 inline float Float(const Real & r) {return r.as_double(53);}
105 inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
106 inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
107 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
108 #elif REALTYPE == REAL_PARANOIDNUMBER
109 typedef ParanoidNumber Real;
110 inline double Double(const Real & r) {return r.Digit();}
111 inline float Float(const Real & r) {return r.Digit();}
112 inline int64_t Int64(const Real & r) {return (int64_t)r.Digit();}
113 inline Real Sqrt(const Real & r) {return Real(sqrt(r.Digit()));}
114 inline Real RealFromStr(const char * str) {return Real(str);}
115 inline Real Abs(const Real & a) {return Real(fabs(a.Digit()));}
116 #elif REALTYPE == REAL_GMPRAT
118 inline double Double(const Real & r) {return r.ToDouble();}
119 inline float Float(const Real & r) {return (float)(r.ToDouble());}
120 inline int64_t Int64(const Real & r) {return (int64_t)r.ToDouble();}
121 inline Real Sqrt(const Real & r) {return Real(sqrt(r.ToDouble()));}
122 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
123 inline Real Abs(const Real & a) {return (a > Real(0)) ? a : Real(0)-a;}
127 #error "Type of Real unspecified."
130 // Allow us to call Float on the primative types
131 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
132 // Mostly in the testers.
133 inline float Float(double f) {return (float)f;}
134 inline float Float(long double f) {return (float)(f);}
135 inline double Double(float f) {return (double)f;}
136 inline double Double(double f) {return (double)f;}
137 inline double Double(long double f) {return (double)(f);}
138 inline double Sqrt(double f) {return sqrt(f);}
139 inline double Abs(double a) {return fabs(a);}
140 inline double Log10(double a) {return log(a)/log(10.0);}
141 inline size_t Size(double a) {return sizeof(a);}
142 inline size_t Size(float a) {return sizeof(a);}
144 // Don't cause an exception
145 inline float ClampFloat(double d)
147 float f = (fabs(d) < FLT_MAX) ? ((fabs(d) > FLT_MIN) ? (float)d : FLT_MIN) : FLT_MAX;
148 return copysign(f, d);
151 inline int64_t Int64(double a)
160 inline Real Power(const Real & a, int n)
164 return Power(Real(1)/a, -n);
167 for (int i = 0; i < n; ++i)
175 Vec2() : x(0.0), y(0.0) {}
176 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
177 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
178 #if REALTYPE != REAL_IRRAM
179 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
181 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
182 // Apparently iRRAM didn't implement -= for a constant argument
184 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
185 bool operator!=(const Vec2& other) const { return !(*this == other); }
187 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
188 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
189 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
190 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
191 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
193 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
194 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
195 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
196 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
198 const Real SquareLength() const { return (x*x + y*y); }
202 //TODO: Make sure there is actually a RealFromStr(const char * str) function
203 // Or this will recurse infinitely
204 // (If you remove this it will also break).
205 inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}
208 // things stolen from wikipedia and googling
209 inline const char * HumanScale(double f)
212 return "RATHER SMALL";
214 return "Plank Length";
216 return "Turtles all the way";
218 return "More turtles";
224 return "To find things";
226 return "It is pretty difficult";
234 return "(Classical) Electron";
236 return "Inter atomic (still)";
238 return "Inter atomic";
250 return "4004 Transistor";
262 return "Football Field";
272 return "Between Earth and Moon";
274 return "Solar System";
276 return "Distance to nearest Star";
282 return "A bigger Universe";
284 return "Really big things";
286 return "Almost as big as...";
288 return "Wolfram's Magestic Ego";
293 inline void DebugRealInfo()
295 Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REALTYPE, g_real_name[REALTYPE], sizeof(Real));
296 #if REALTYPE == REAL_PARANOIDNUMBER
297 #ifdef PARANOID_SIZE_LIMIT
298 Debug("Size limit of %d is being enforced", PARANOID_SIZE_LIMIT);