10 #define REAL_LONG_DOUBLE 2
12 #define REAL_RATIONAL 4
13 #define REAL_RATIONAL_ARBINT 5
14 #define REAL_MPFRCPP 6
17 #error "REAL was not defined!"
24 #if REAL == REAL_RATIONAL
28 #if REAL == REAL_RATIONAL_ARBINT
34 #if REAL == REAL_MPFRCPP
40 extern const char * g_real_name[];
42 #if REAL == REAL_SINGLE
44 #elif REAL == REAL_DOUBLE
46 #elif REAL == REAL_LONG_DOUBLE
47 typedef long double Real;
48 #elif REAL == REAL_VFPU
49 typedef VFPU::VFloat Real;
50 inline float Float(const Real & r) {return r.m_value;}
51 inline double Double(const Real & r) {return r.m_value;}
52 #elif REAL == REAL_RATIONAL
53 typedef Rational<int64_t> Real;
54 inline float Float(const Real & r) {return (float)r.ToDouble();}
55 inline double Double(const Real & r) {return r.ToDouble();}
56 #elif REAL == REAL_RATIONAL_ARBINT
57 #define ARBINT Gmpint // Set to Gmpint or Arbint here
59 typedef Rational<ARBINT> Real;
60 inline float Float(const Real & r) {return (float)r.ToDouble();}
61 inline double Double(const Real & r) {return r.ToDouble();}
62 inline int64_t Int64(const Real & r) {return r.ToInt64();}
63 inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
64 #elif REAL == REAL_MPFRCPP
65 typedef mpfr::mpreal Real;
66 inline double Double(const Real & r) {return r.toDouble();}
67 inline float Float(const Real & r) {return r.toDouble();}
68 inline int64_t Int64(const Real & r) {return r.toLong();}
69 inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
70 inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
72 #error "Type of Real unspecified."
75 // Allow us to call Float on the primative types
76 // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
77 // Mostly in the testers.
78 inline float Float(float f) {return (float)f;}
79 inline float Float(double f) {return (float)f;}
80 inline float Float(long double f) {return (float)(f);}
81 inline double Double(float f) {return (double)f;}
82 inline double Double(double f) {return (double)f;}
83 inline double Double(long double f) {return (double)(f);}
84 inline double Sqrt(double f) {return sqrt(f);}
85 inline double Abs(double a) {return fabs(a);}
86 inline int64_t Int64(double a){return (int64_t)a;}
88 inline Real Power(const Real & a, int n)
92 return Power(Real(1)/a, -n);
95 for (int i = 0; i < n; ++i)
103 Vec2() : x(0), y(0) {}
104 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
105 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
106 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
108 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
109 bool operator!=(const Vec2& other) const { return !(*this == other); }
111 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
112 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
113 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
114 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
115 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
117 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
118 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
119 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
120 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
122 const Real SquareLength() const { return (x*x + y*y); }