Add MPFRC++ mpreal type
[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
16 #ifndef REAL
17         #error "REAL was not defined!"
18 #endif
19
20 #if REAL == REAL_VFPU
21         #include "vfpu.h"
22 #endif
23
24 #if REAL == REAL_RATIONAL
25         #include "rational.h"
26 #endif //REAL
27
28 #if REAL == REAL_RATIONAL_ARBINT
29         #include "rational.h"
30         #include "arbint.h"
31         #include "gmpint.h"
32 #endif //REAL
33
34 #if REAL == REAL_MPFRCPP
35         #include <mpreal.h>
36 #endif //REAL
37
38 namespace IPDF
39 {       
40         extern const char * g_real_name[];
41
42 #if REAL == REAL_SINGLE
43         typedef float Real;
44 #elif REAL == REAL_DOUBLE
45         typedef double Real;
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
58         
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());}
71 #else
72         #error "Type of Real unspecified."
73 #endif //REAL
74
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;}
87         
88         inline Real Power(const Real & a, int n)
89         {
90                 if (n < 0)
91                 {
92                         return Power(Real(1)/a, -n);
93                 }
94                 Real r(1);
95                 for (int i = 0; i < n; ++i)
96                         r *= a;
97                 return r;
98         }
99         struct Vec2
100         {
101                 Real x;
102                 Real y;
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) {}
107         
108                 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
109                 bool operator!=(const Vec2& other) const { return !(*this == other); }
110                 
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; }
116
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); }
121
122                 const Real SquareLength() const { return (x*x + y*y); }
123         
124         };
125
126
127 }
128
129 #endif //_REAL_H

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