Fix compiling with non doubles
[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
15 #ifndef REAL
16         #error "REAL was not defined!"
17 #endif
18
19 #if REAL == REAL_VFPU
20         #include "vfpu.h"
21 #endif
22
23 #if REAL == REAL_RATIONAL
24         #include "rational.h"
25 #endif //REAL
26
27 #if REAL == REAL_RATIONAL_ARBINT
28         #include "rational.h"
29         #include "arbint.h"
30         #include "gmpint.h"
31 #endif //REAL
32
33 namespace IPDF
34 {       
35         extern const char * g_real_name[];
36
37 #if REAL == REAL_SINGLE
38         typedef float Real;
39 #elif REAL == REAL_DOUBLE
40         typedef double Real;
41 #elif REAL == REAL_LONG_DOUBLE
42         typedef long double Real;
43 #elif REAL == REAL_VFPU
44         typedef VFPU::VFloat Real;
45         inline float Float(const Real & r) {return r.m_value;}
46         inline double Double(const Real & r) {return r.m_value;}
47 #elif REAL == REAL_RATIONAL
48         typedef Rational<int64_t> Real;
49         inline float Float(const Real & r) {return (float)r.ToDouble();}
50         inline double Double(const Real & r) {return r.ToDouble();}
51 #elif REAL == REAL_RATIONAL_ARBINT
52         #define ARBINT Gmpint // Set to Gmpint or Arbint here
53         
54         typedef Rational<ARBINT> Real;
55         inline float Float(const Real & r) {return (float)r.ToDouble();}
56         inline double Double(const Real & r) {return r.ToDouble();}
57         inline int64_t Int64(const Real & r) {return r.ToInt64();}
58         inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
59
60 #else
61         #error "Type of Real unspecified."
62 #endif //REAL
63
64         // Allow us to call Float on the primative types
65         // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
66         // Mostly in the testers.
67         inline float Float(float f) {return (float)f;}
68         inline float Float(double f) {return (float)f;}
69         inline float Float(long double f) {return (float)(f);}
70         inline double Double(float f) {return (double)f;}
71         inline double Double(double f) {return (double)f;}
72         inline double Double(long double f) {return (double)(f);}
73         inline double Sqrt(double f) {return sqrt(f);}
74         inline double Abs(double a) {return fabs(a);}
75         inline int64_t Int64(double a){return (int64_t)a;}
76         
77         inline Real Power(const Real & a, int n)
78         {
79                 if (n < 0)
80                 {
81                         return Power(Real(1)/a, -n);
82                 }
83                 Real r(1);
84                 for (int i = 0; i < n; ++i)
85                         r *= a;
86                 return r;
87         }
88         struct Vec2
89         {
90                 Real x;
91                 Real y;
92                 Vec2() : x(0), y(0) {}
93                 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
94                 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
95                 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
96         
97                 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
98                 bool operator!=(const Vec2& other) const { return !(*this == other); }
99                 
100                 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
101                 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
102                 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
103                 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
104                 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
105
106                 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
107                 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
108                 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
109                 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
110
111                 const Real SquareLength() const { return (x*x + y*y); }
112         
113         };
114
115
116 }
117
118 #endif //_REAL_H

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