Paranoia is setting in
[ipdf/code.git] / src / real.h
1 #ifndef _REAL_H
2 #define _REAL_H
3
4 #include "common.h"
5 #include <cmath>
6
7
8 #define REAL_SINGLE 0
9 #define REAL_DOUBLE 1
10 #define REAL_LONG_DOUBLE 2
11 #define REAL_VFPU 3
12 #define REAL_RATIONAL 4
13 #define REAL_RATIONAL_ARBINT 5
14 #define REAL_MPFRCPP 6
15 #define REAL_IRRAM 7
16
17 #ifndef REALTYPE
18         #error "REALTYPE was not defined!"
19 #endif
20
21 #if REALTYPE == REAL_VFPU
22         #include "vfpu.h"
23 #endif
24
25 #if REALTYPE == REAL_RATIONAL
26         #include "rational.h"
27 #endif //REALTYPE
28
29 #if REALTYPE == REAL_RATIONAL_ARBINT
30         #include "rational.h"
31         #include "arbint.h"
32         #include "gmpint.h"
33 #endif //REALTYPE
34
35 #if REALTYPE == REAL_MPFRCPP
36         #include <mpreal.h>
37 #endif //REALTYPE
38
39 #if REALTYPE == REAL_IRRAM
40         #include "../contrib/iRRAM/include/iRRAM/lib.h"
41 #endif
42
43 namespace IPDF
44 {       
45         extern const char * g_real_name[];
46
47 #if REALTYPE == REAL_SINGLE
48         typedef float Real;
49 #elif REALTYPE == REAL_DOUBLE
50         typedef double Real;
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
63         
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);}
82 #else
83         #error "Type of Real unspecified."
84 #endif //REALTYPE
85
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;}
97         
98         inline Real Power(const Real & a, int n)
99         {
100                 if (n < 0)
101                 {
102                         return Power(Real(1)/a, -n);
103                 }
104                 Real r(1);
105                 for (int i = 0; i < n; ++i)
106                         r *= a;
107                 return r;
108         }
109         struct Vec2
110         {
111                 Real x;
112                 Real y;
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) {}
118                 #else
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
121                 #endif
122                 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
123                 bool operator!=(const Vec2& other) const { return !(*this == other); }
124                 
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; }
130
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); }
135
136                 const Real SquareLength() const { return (x*x + y*y); }
137         
138         };
139
140
141
142
143 }
144
145 #endif //_REAL_H

UCC git Repository :: git.ucc.asn.au