Turtles all the way down
[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 #define REAL_PARANOIDNUMBER 8
17 #define REAL_GMPRAT 9
18
19 #ifndef REALTYPE
20         #error "REALTYPE was not defined!"
21 #endif
22
23 #define XSTR(x) STR(x)
24 #define STR(x) #x
25 //#pragma message "REALTYPE = " XSTR(REALTYPE)
26
27 #if REALTYPE == REAL_VFPU
28         #include "vfpu.h"
29 #endif
30
31 #if REALTYPE == REAL_RATIONAL
32         #include "rational.h"
33 #endif //REALTYPE
34
35 #if REALTYPE == REAL_RATIONAL_ARBINT
36         #include "rational.h"
37         #include "arbint.h"
38         #include "gmpint.h"
39 #endif //REALTYPE
40
41 #if REALTYPE == REAL_MPFRCPP
42         #include <mpreal.h>
43 #endif //REALTYPE
44
45 #if REALTYPE == REAL_IRRAM
46         #include "../contrib/iRRAM/include/iRRAM/lib.h"
47 #endif
48
49 #if REALTYPE == REAL_PARANOIDNUMBER
50         #include "paranoidnumber.h"
51 #endif
52
53 #if REALTYPE == REAL_GMPRAT
54         #include "gmprat.h"
55 #endif 
56
57 namespace IPDF
58 {       
59         extern const char * g_real_name[];
60
61 #if REALTYPE == REAL_SINGLE
62         typedef float Real;
63         inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
64         //inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
65 #elif REALTYPE == REAL_DOUBLE
66         typedef double Real;
67         inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
68         //inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
69 #elif REALTYPE == REAL_LONG_DOUBLE
70         typedef long double Real;
71         inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
72         inline std::string Str(const Real & a) {std::stringstream s; s << a; return s.str();}
73 #elif REALTYPE == REAL_VFPU
74         typedef VFPU::VFloat Real;
75         inline float Float(const Real & r) {return r.m_value;}
76         inline double Double(const Real & r) {return r.m_value;}
77         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
78 #elif REALTYPE == REAL_RATIONAL
79         typedef Rational<int64_t> Real;
80         inline float Float(const Real & r) {return (float)r.ToDouble();}
81         inline double Double(const Real & r) {return r.ToDouble();}
82         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
83 #elif REALTYPE == REAL_RATIONAL_ARBINT
84         #define ARBINT Arbint // Set to Gmpint or Arbint here
85         
86         typedef Rational<ARBINT> Real;
87         inline float Float(const Real & r) {return (float)r.ToDouble();}
88         inline double Double(const Real & r) {return r.ToDouble();}
89         inline int64_t Int64(const Real & r) {return r.ToInt64();}
90         inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
91         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
92 #elif REALTYPE == REAL_MPFRCPP
93         typedef mpfr::mpreal Real;
94         inline double Double(const Real & r) {return r.toDouble();}
95         inline float Float(const Real & r) {return r.toDouble();}
96         inline int64_t Int64(const Real & r) {return r.toLong();}
97         inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
98         inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
99         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
100 #elif REALTYPE == REAL_IRRAM
101         typedef iRRAM::REAL Real;
102         inline double Double(const Real & r) {return r.as_double(53);}
103         inline float Float(const Real & r) {return r.as_double(53);}
104         inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
105         inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
106         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
107 #elif REALTYPE == REAL_PARANOIDNUMBER
108         typedef ParanoidNumber Real;
109         inline double Double(const Real & r) {return r.Digit();}
110         inline float Float(const Real & r) {return r.Digit();}
111         inline int64_t Int64(const Real & r) {return (int64_t)r.Digit();}
112         inline Real Sqrt(const Real & r) {return Real(sqrt(r.Digit()));}
113         inline Real RealFromStr(const char * str) {return Real(str);}   
114         inline Real Abs(const Real & a) {return Real(fabs(a.Digit()));}
115 #elif REALTYPE == REAL_GMPRAT
116         typedef Gmprat Real;
117         inline double Double(const Real & r) {return r.ToDouble();}
118         inline float Float(const Real & r) {return (float)(r.ToDouble());}
119         inline int64_t Int64(const Real & r) {return (int64_t)r.ToDouble();}
120         inline Real Sqrt(const Real & r) {return Real(sqrt(r.ToDouble()));}
121         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
122         inline Real Abs(const Real & a) {return (a > Real(0)) ? a : Real(0)-a;}
123         inline std::string Str(const Real & a) {return a.Str();}
124         
125 #else
126         #error "Type of Real unspecified."
127 #endif //REALTYPE
128
129         // Allow us to call Float on the primative types
130         // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
131         // Mostly in the testers.
132         inline float Float(double f) {return (float)f;}
133         inline float Float(long double f) {return (float)(f);}
134         inline double Double(float f) {return (double)f;}
135         inline double Double(double f) {return (double)f;}
136         inline double Double(long double f) {return (double)(f);}
137         inline double Sqrt(double f) {return sqrt(f);}
138         inline double Abs(double a) {return fabs(a);}
139         inline double Log10(double a) {return log(a)/log(10.0);}
140         inline size_t Size(double a) {return sizeof(a);}
141         inline size_t Size(float a) {return sizeof(a);}
142         
143
144         inline int64_t Int64(double a)
145         {
146                 if (a < INT64_MIN)
147                         return INT64_MIN;
148                 if (a > INT64_MAX)
149                         return INT64_MAX;
150                 return (int64_t)(a);
151         }
152         
153         inline Real Power(const Real & a, int n)
154         {
155                 if (n < 0)
156                 {
157                         return Power(Real(1)/a, -n);
158                 }
159                 Real r(1);
160                 for (int i = 0; i < n; ++i)
161                         r *= a;
162                 return r;
163         }
164         struct Vec2
165         {
166                 Real x;
167                 Real y;
168                 Vec2() : x(0.0), y(0.0) {}
169                 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
170                 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
171                 #if REALTYPE != REAL_IRRAM
172                 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
173                 #else
174                 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
175                 // Apparently iRRAM didn't implement -= for a constant argument
176                 #endif
177                 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
178                 bool operator!=(const Vec2& other) const { return !(*this == other); }
179                 
180                 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
181                 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
182                 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
183                 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
184                 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
185
186                 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
187                 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
188                 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
189                 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
190
191                 const Real SquareLength() const { return (x*x + y*y); }
192         
193         };
194
195         //TODO: Make sure there is actually a RealFromStr(const char * str) function
196         //              Or this will recurse infinitely
197         //              (If you remove this it will also break).
198         inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}
199
200
201         inline void DebugRealInfo() 
202         {
203                 Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REALTYPE, g_real_name[REALTYPE], sizeof(Real));
204                 #if REALTYPE == REAL_PARANOIDNUMBER
205                         #ifdef PARANOID_SIZE_LIMIT
206                                 Debug("Size limit of %d is being enforced", PARANOID_SIZE_LIMIT);
207                         #endif
208                 #endif
209         }
210 }
211
212 #endif //_REAL_H

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