CHROMA
bigfloat.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Remez algorithm for finding nth roots
4  */
5 
6 #ifndef __bigfloat_h__
7 #define __bigfloat_h__
8 
9 #include "chromabase.h"
10 #include <gmp.h>
11 
12 namespace Chroma
13 {
14 
15  //! Bigfloat
16  /*! @ingroup monomial
17  *
18  * Simple C++ wrapper for multiprecision datatype used for Remez
19  * algorithm
20  *
21  */
22  class bigfloat
23  {
24  public:
25  bigfloat() { mpf_init(x); }
26  bigfloat(const bigfloat& y) { mpf_init_set(x, y.x); }
27  bigfloat(const unsigned long u) { mpf_init_set_ui(x, u); }
28  bigfloat(const long i) { mpf_init_set_si(x, i); }
29  bigfloat(const int i) {mpf_init_set_si(x,(long)i);}
30  bigfloat(const double d) { mpf_init_set_d(x, toDouble(d)); }
31  bigfloat(const Real32& d) { mpf_init_set_d(x, toDouble(d)); }
32  bigfloat(const Real64& d) { mpf_init_set_d(x, toDouble(d)); }
33  bigfloat(const char *str) { mpf_init_set_str(x, str, 10); }
34  ~bigfloat(void) { mpf_clear(x); }
35  operator const double (void) const { return (double)(mpf_get_d(x)); }
36  static void setDefaultPrecision(unsigned long dprec) {
37  unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
38  mpf_set_default_prec(bprec);
39  }
40 
41  void setPrecision(unsigned long dprec) {
42  unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
43  mpf_set_prec(x,bprec);
44  }
45 
46  unsigned long getPrecision(void) const { return mpf_get_prec(x); }
47 
48  unsigned long getDefaultPrecision(void) const { return mpf_get_default_prec(); }
49 
51  mpf_set(x, y.x);
52  return *this;
53  }
54 
55  bigfloat& operator=(const unsigned long y) {
56  mpf_set_ui(x, y);
57  return *this;
58  }
59 
60  bigfloat& operator=(const signed long y) {
61  mpf_set_si(x, y);
62  return *this;
63  }
64 
65  bigfloat& operator=(const double y) {
66  mpf_set_d(x, toDouble(y));
67  return *this;
68  }
69 
70  bigfloat& operator=(const Real32& y) {
71  mpf_set_d(x, toDouble(y));
72  return *this;
73  }
74 
75  bigfloat& operator=(const Real64& y) {
76  mpf_set_d(x, toDouble(y));
77  return *this;
78  }
79 
80  size_t write(void);
81  size_t read(void);
82 
83  /* Arithmetic Functions */
84 
85  bigfloat& operator+=(const bigfloat& y) { return *this = *this + y; }
86  bigfloat& operator-=(const bigfloat& y) { return *this = *this - y; }
87  bigfloat& operator*=(const bigfloat& y) { return *this = *this * y; }
88  bigfloat& operator/=(const bigfloat& y) { return *this = *this / y; }
89 
90  friend bigfloat operator+(const bigfloat& x, const bigfloat& y) {
91  bigfloat a;
92  mpf_add(a.x,x.x,y.x);
93  return a;
94  }
95 
96  friend bigfloat operator+(const bigfloat& x, const unsigned long y) {
97  bigfloat a;
98  mpf_add_ui(a.x,x.x,y);
99  return a;
100  }
101 
102  friend bigfloat operator-(const bigfloat& x, const bigfloat& y) {
103  bigfloat a;
104  mpf_sub(a.x,x.x,y.x);
105  return a;
106  }
107 
108  friend bigfloat operator-(const unsigned long x, const bigfloat& y) {
109  bigfloat a;
110  mpf_ui_sub(a.x,x,y.x);
111  return a;
112  }
113 
114  friend bigfloat operator-(const bigfloat& x, const unsigned long y) {
115  bigfloat a;
116  mpf_sub_ui(a.x,x.x,y);
117  return a;
118  }
119 
120  friend bigfloat operator-(const bigfloat& x) {
121  bigfloat a;
122  mpf_neg(a.x,x.x);
123  return a;
124  }
125 
126  friend bigfloat operator*(const bigfloat& x, const bigfloat& y) {
127  bigfloat a;
128  mpf_mul(a.x,x.x,y.x);
129  return a;
130  }
131 
132  friend bigfloat operator*(const bigfloat& x, const unsigned long y) {
133  bigfloat a;
134  mpf_mul_ui(a.x,x.x,y);
135  return a;
136  }
137 
138  friend bigfloat operator/(const bigfloat& x, const bigfloat& y){
139  bigfloat a;
140  mpf_div(a.x,x.x,y.x);
141  return a;
142  }
143 
144  friend bigfloat operator/(const unsigned long x, const bigfloat& y){
145  bigfloat a;
146  mpf_ui_div(a.x,x,y.x);
147  return a;
148  }
149 
150  friend bigfloat operator/(const bigfloat& x, const unsigned long y){
151  bigfloat a;
152  mpf_div_ui(a.x,x.x,y);
153  return a;
154  }
155 
156  friend bigfloat sqrt_bf(const bigfloat& x){
157  bigfloat a;
158  mpf_sqrt(a.x,x.x);
159  return a;
160  }
161 
162  friend bigfloat sqrt_bf(const unsigned long x){
163  bigfloat a;
164  mpf_sqrt_ui(a.x,x);
165  return a;
166  }
167 
168  friend bigfloat abs_bf(const bigfloat& x){
169  bigfloat a;
170  mpf_abs(a.x,x.x);
171  return a;
172  }
173 
174  friend bigfloat pow_bf(const bigfloat& a, long power) {
175  bigfloat b;
176  mpf_pow_ui(b.x,a.x,power);
177  return b;
178  }
179 
180  /* Comparison Functions */
181 
182  friend int operator>(const bigfloat& x, const bigfloat& y) {
183  int test;
184  test = mpf_cmp(x.x,y.x);
185  if (test > 0) return 1;
186  else return 0;
187  }
188 
189  friend int operator<(const bigfloat& x, const bigfloat& y) {
190  int test;
191  test = mpf_cmp(x.x,y.x);
192  if (test < 0) return 1;
193  else return 0;
194  }
195 
196  friend int sgn(const bigfloat&);
197 
198  /* Miscellaneous Functions */
199 
200  friend bigfloat& random(void);
201 
202  private:
203 
204  mpf_t x;
205 
206  };
207 
208 } // end namespace Chroma
209 
210 #endif
211 
212 
Primary include file for CHROMA library code.
Bigfloat.
Definition: bigfloat.h:23
friend bigfloat operator/(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:138
friend bigfloat operator-(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:102
friend bigfloat operator/(const unsigned long x, const bigfloat &y)
Definition: bigfloat.h:144
bigfloat(const char *str)
Definition: bigfloat.h:33
bigfloat & operator*=(const bigfloat &y)
Definition: bigfloat.h:87
friend bigfloat operator/(const bigfloat &x, const unsigned long y)
Definition: bigfloat.h:150
size_t read(void)
~bigfloat(void)
Definition: bigfloat.h:34
bigfloat & operator=(const signed long y)
Definition: bigfloat.h:60
bigfloat & operator=(const unsigned long y)
Definition: bigfloat.h:55
friend int operator>(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:182
bigfloat(const double d)
Definition: bigfloat.h:30
bigfloat & operator+=(const bigfloat &y)
Definition: bigfloat.h:85
friend bigfloat operator+(const bigfloat &x, const unsigned long y)
Definition: bigfloat.h:96
friend bigfloat pow_bf(const bigfloat &a, long power)
Definition: bigfloat.h:174
friend bigfloat operator-(const bigfloat &x)
Definition: bigfloat.h:120
friend bigfloat sqrt_bf(const unsigned long x)
Definition: bigfloat.h:162
bigfloat(const bigfloat &y)
Definition: bigfloat.h:26
friend bigfloat operator-(const bigfloat &x, const unsigned long y)
Definition: bigfloat.h:114
bigfloat & operator=(const bigfloat &y)
Definition: bigfloat.h:50
static void setDefaultPrecision(unsigned long dprec)
Definition: bigfloat.h:36
friend bigfloat abs_bf(const bigfloat &x)
Definition: bigfloat.h:168
friend int sgn(const bigfloat &)
bigfloat & operator-=(const bigfloat &y)
Definition: bigfloat.h:86
bigfloat & operator=(const double y)
Definition: bigfloat.h:65
friend bigfloat operator*(const bigfloat &x, const unsigned long y)
Definition: bigfloat.h:132
bigfloat & operator/=(const bigfloat &y)
Definition: bigfloat.h:88
size_t write(void)
friend bigfloat sqrt_bf(const bigfloat &x)
Definition: bigfloat.h:156
bigfloat(const unsigned long u)
Definition: bigfloat.h:27
void setPrecision(unsigned long dprec)
Definition: bigfloat.h:41
friend int operator<(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:189
bigfloat & operator=(const Real64 &y)
Definition: bigfloat.h:75
bigfloat(const Real64 &d)
Definition: bigfloat.h:32
bigfloat & operator=(const Real32 &y)
Definition: bigfloat.h:70
friend bigfloat operator-(const unsigned long x, const bigfloat &y)
Definition: bigfloat.h:108
bigfloat(const long i)
Definition: bigfloat.h:28
unsigned long getPrecision(void) const
Definition: bigfloat.h:46
friend bigfloat & random(void)
bigfloat(const Real32 &d)
Definition: bigfloat.h:31
unsigned long getDefaultPrecision(void) const
Definition: bigfloat.h:48
friend bigfloat operator*(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:126
friend bigfloat operator+(const bigfloat &x, const bigfloat &y)
Definition: bigfloat.h:90
bigfloat(const int i)
Definition: bigfloat.h:29
int y
Definition: meslate.cc:35
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
static multi1d< LatticeColorMatrix > u
int i
Definition: pbg5p_w.cc:55
Complex a
Definition: invbicg.cc:95
DComplex d
Definition: invbicg.cc:99
Complex b
Definition: invbicg.cc:96