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 #elif REALTYPE == REAL_DOUBLE
51 #elif REALTYPE == REAL_LONG_DOUBLE
52 typedef long double Real;
53 #elif REALTYPE == REAL_VFPU
54 typedef VFPU::VFloat Real;
55 inline float Float(const Real & r) {return r.m_value;}
56 inline double Double(const Real & r) {return r.m_value;}
57 #elif REALTYPE == REAL_RATIONAL
58 typedef Rational<int64_t> Real;
59 inline float Float(const Real & r) {return (float)r.ToDouble();}
60 inline double Double(const Real & r) {return r.ToDouble();}
61 #elif REALTYPE == REAL_RATIONAL_ARBINT
62 #define ARBINT Gmpint // Set to Gmpint or Arbint here
64 typedef Rational<ARBINT> Real;
65 inline float Float(const Real & r) {return (float)r.ToDouble();}
66 inline double Double(const Real & r) {return r.ToDouble();}
67 inline int64_t Int64(const Real & r) {return r.ToInt64();}
68 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
69 #elif REALTYPE == REAL_MPFRCPP
70 typedef mpfr::mpreal Real;
71 inline double Double(const Real & r) {return r.toDouble();}
72 inline float Float(const Real & r) {return r.toDouble();}
73 inline int64_t Int64(const Real & r) {return r.toLong();}
74 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
75 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
76 #elif REALTYPE == REAL_IRRAM
77 typedef iRRAM::REAL Real;
78 inline double Double(const Real & r) {return r.as_double(53);}
79 inline float Float(const Real & r) {return r.as_double(53);}
80 inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
81 inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
83 #error "Type of Real unspecified."
86 // Allow us to call Float on the primative types
87 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
88 // Mostly in the testers.
89 inline float Float(double f) {return (float)f;}
90 inline float Float(long double f) {return (float)(f);}
91 inline double Double(float f) {return (double)f;}
92 inline double Double(double f) {return (double)f;}
93 inline double Double(long double f) {return (double)(f);}
94 inline double Sqrt(double f) {return sqrt(f);}
95 inline double Abs(double a) {return fabs(a);}
96 inline int64_t Int64(double a){return (int64_t)a;}
98 inline Real Power(const Real & a, int n)
102 return Power(Real(1)/a, -n);
105 for (int i = 0; i < n; ++i)
113 Vec2() : x(0), y(0) {}
114 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
115 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
116 #if REALTYPE != REAL_IRRAM
117 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
119 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
120 // Apparently iRRAM didn't implement -= for a constant argument
122 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
123 bool operator!=(const Vec2& other) const { return !(*this == other); }
125 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
126 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
127 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
128 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
129 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
131 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
132 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
133 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
134 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
136 const Real SquareLength() const { return (x*x + y*y); }