10 #define REAL_LONG_DOUBLE 2
12 #define REAL_RATIONAL 4
13 #define REAL_RATIONAL_ARBINT 5
14 #define REAL_MPFRCPP 6
18 #error "REALTYPE was not defined!"
21 #if REALTYPE == REAL_VFPU
25 #if REALTYPE == REAL_RATIONAL
29 #if REALTYPE == REAL_RATIONAL_ARBINT
35 #if REALTYPE == REAL_MPFRCPP
39 #if REALTYPE == REAL_IRRAM
40 #include "../contrib/iRRAM/include/iRRAM/lib.h"
45 extern const char * g_real_name[];
47 #if REALTYPE == REAL_SINGLE
49 inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
50 #elif REALTYPE == REAL_DOUBLE
52 inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
53 #elif REALTYPE == REAL_LONG_DOUBLE
54 typedef long double Real;
55 inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
56 #elif REALTYPE == REAL_VFPU
57 typedef VFPU::VFloat Real;
58 inline float Float(const Real & r) {return r.m_value;}
59 inline double Double(const Real & r) {return r.m_value;}
60 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
61 #elif REALTYPE == REAL_RATIONAL
62 typedef Rational<int64_t> Real;
63 inline float Float(const Real & r) {return (float)r.ToDouble();}
64 inline double Double(const Real & r) {return r.ToDouble();}
65 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
66 #elif REALTYPE == REAL_RATIONAL_ARBINT
67 #define ARBINT Gmpint // Set to Gmpint or Arbint here
69 typedef Rational<ARBINT> Real;
70 inline float Float(const Real & r) {return (float)r.ToDouble();}
71 inline double Double(const Real & r) {return r.ToDouble();}
72 inline int64_t Int64(const Real & r) {return r.ToInt64();}
73 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
74 #elif REALTYPE == REAL_MPFRCPP
75 typedef mpfr::mpreal Real;
76 inline double Double(const Real & r) {return r.toDouble();}
77 inline float Float(const Real & r) {return r.toDouble();}
78 inline int64_t Int64(const Real & r) {return r.toLong();}
79 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
80 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
81 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
82 #elif REALTYPE == REAL_IRRAM
83 typedef iRRAM::REAL Real;
84 inline double Double(const Real & r) {return r.as_double(53);}
85 inline float Float(const Real & r) {return r.as_double(53);}
86 inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
87 inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
88 inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
90 #error "Type of Real unspecified."
93 // Allow us to call Float on the primative types
94 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
95 // Mostly in the testers.
96 inline float Float(double f) {return (float)f;}
97 inline float Float(long double f) {return (float)(f);}
98 inline double Double(float f) {return (double)f;}
99 inline double Double(double f) {return (double)f;}
100 inline double Double(long double f) {return (double)(f);}
101 inline double Sqrt(double f) {return sqrt(f);}
102 inline double Abs(double a) {return fabs(a);}
103 inline int64_t Int64(double a){return (int64_t)a;}
105 inline Real Power(const Real & a, int n)
109 return Power(Real(1)/a, -n);
112 for (int i = 0; i < n; ++i)
120 Vec2() : x(0), y(0) {}
121 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
122 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
123 #if REALTYPE != REAL_IRRAM
124 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
126 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
127 // Apparently iRRAM didn't implement -= for a constant argument
129 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
130 bool operator!=(const Vec2& other) const { return !(*this == other); }
132 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
133 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
134 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
135 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
136 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
138 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
139 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
140 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
141 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
143 const Real SquareLength() const { return (x*x + y*y); }
147 inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}