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

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