HiRep 0.1
Loading...
Searching...
No Matches
bigfloat.h
1/*
2
3 Mike Clark - 25th May 2005
4
5 bigfloat.h
6
7 Simple C++ wrapper for multiprecision datatype used by AlgRemez
8 algorithm
9
10*/
11
12#include <gmp.h>
13#include <mpf2mpfr.h>
14#include <mpfr.h>
15
16#ifndef INCLUDED_BIGFLOAT_H
17#define INCLUDED_BIGFLOAT_H
18
19class bigfloat {
20private:
21 mpf_t x;
22
23public:
24 bigfloat() {
25 mpf_init(x);
26 }
27 bigfloat(const bigfloat &y) {
28 mpf_init_set(x, y.x);
29 }
30 bigfloat(const unsigned long u) {
31 mpf_init_set_ui(x, u);
32 }
33 bigfloat(const long i) {
34 mpf_init_set_si(x, i);
35 }
36 bigfloat(const int i) {
37 mpf_init_set_si(x, (long)i);
38 }
39 bigfloat(const float d) {
40 mpf_init_set_d(x, (double)d);
41 }
42 bigfloat(const double d) {
43 mpf_init_set_d(x, d);
44 }
45 bigfloat(const char *str) {
46 mpf_init_set_str(x, (char *)str, 10);
47 }
48 ~bigfloat(void) {
49 mpf_clear(x);
50 }
51 operator const double(void) const {
52 return (double)mpf_get_d(x);
53 }
54 static void setDefaultPrecision(unsigned long dprec) {
55 unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
56 mpf_set_default_prec(bprec);
57 }
58
59 void setPrecision(unsigned long dprec) {
60 unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
61 mpf_set_prec(x, bprec);
62 }
63
64 unsigned long getPrecision(void) const {
65 return mpf_get_prec(x);
66 }
67
68 unsigned long getDefaultPrecision(void) const {
69 return mpf_get_default_prec();
70 }
71
72 bigfloat &operator=(const bigfloat &y) {
73 mpf_set(x, y.x);
74 return *this;
75 }
76
77 bigfloat &operator=(const unsigned long y) {
78 mpf_set_ui(x, y);
79 return *this;
80 }
81
82 bigfloat &operator=(const signed long y) {
83 mpf_set_si(x, y);
84 return *this;
85 }
86
87 bigfloat &operator=(const float y) {
88 mpf_set_d(x, (double)y);
89 return *this;
90 }
91
92 bigfloat &operator=(const double y) {
93 mpf_set_d(x, y);
94 return *this;
95 }
96
97 size_t write(void);
98 size_t read(void);
99
100 /* Arithmetic Functions */
101
102 bigfloat &operator+=(const bigfloat &y) {
103 return *this = *this + y;
104 }
105 bigfloat &operator-=(const bigfloat &y) {
106 return *this = *this - y;
107 }
108 bigfloat &operator*=(const bigfloat &y) {
109 return *this = *this * y;
110 }
111 bigfloat &operator/=(const bigfloat &y) {
112 return *this = *this / y;
113 }
114
115 friend bigfloat operator+(const bigfloat &x, const bigfloat &y) {
116 bigfloat a;
117 mpf_add(a.x, x.x, y.x);
118 return a;
119 }
120
121 friend bigfloat operator+(const bigfloat &x, const unsigned long y) {
122 bigfloat a;
123 mpf_add_ui(a.x, x.x, y);
124 return a;
125 }
126
127 friend bigfloat operator-(const bigfloat &x, const bigfloat &y) {
128 bigfloat a;
129 mpf_sub(a.x, x.x, y.x);
130 return a;
131 }
132
133 friend bigfloat operator-(const unsigned long x, const bigfloat &y) {
134 bigfloat a;
135 mpf_ui_sub(a.x, x, y.x);
136 return a;
137 }
138
139 friend bigfloat operator-(const bigfloat &x, const unsigned long y) {
140 bigfloat a;
141 mpf_sub_ui(a.x, x.x, y);
142 return a;
143 }
144
145 friend bigfloat operator-(const bigfloat &x) {
146 bigfloat a;
147 mpf_neg(a.x, x.x);
148 return a;
149 }
150
151 friend bigfloat operator*(const bigfloat &x, const bigfloat &y) {
152 bigfloat a;
153 mpf_mul(a.x, x.x, y.x);
154 return a;
155 }
156
157 friend bigfloat operator*(const bigfloat &x, const unsigned long y) {
158 bigfloat a;
159 mpf_mul_ui(a.x, x.x, y);
160 return a;
161 }
162
163 friend bigfloat operator/(const bigfloat &x, const bigfloat &y) {
164 bigfloat a;
165 mpf_div(a.x, x.x, y.x);
166 return a;
167 }
168
169 friend bigfloat operator/(const unsigned long x, const bigfloat &y) {
170 bigfloat a;
171 mpf_ui_div(a.x, x, y.x);
172 return a;
173 }
174
175 friend bigfloat operator/(const bigfloat &x, const unsigned long y) {
176 bigfloat a;
177 mpf_div_ui(a.x, x.x, y);
178 return a;
179 }
180
181 friend bigfloat sqrt_bf(const bigfloat &x) {
182 bigfloat a;
183 mpf_sqrt(a.x, x.x);
184 return a;
185 }
186
187 friend bigfloat sqrt_bf(const unsigned long x) {
188 bigfloat a;
189 mpf_sqrt_ui(a.x, x);
190 return a;
191 }
192
193 friend bigfloat abs_bf(const bigfloat &x) {
194 bigfloat a;
195 mpf_abs(a.x, x.x);
196 return a;
197 }
198
199 friend bigfloat pow_bf(const bigfloat &a, long power) {
200 bigfloat b;
201 mpf_pow_ui(b.x, a.x, power);
202 return b;
203 }
204
205 friend bigfloat pow_bf(const bigfloat &a, bigfloat &power) {
206 bigfloat b;
207 mpfr_pow(b.x, a.x, power.x, GMP_RNDN);
208 return b;
209 }
210
211 friend bigfloat exp_bf(const bigfloat &a) {
212 bigfloat b;
213 mpfr_exp(b.x, a.x, GMP_RNDN);
214 return b;
215 }
216
217 friend bigfloat log_bf(const bigfloat &a) {
218 bigfloat b;
219 mpfr_log(b.x, a.x, GMP_RNDN);
220 return b;
221 }
222
223 /* Comparison Functions */
224
225 friend int operator>(const bigfloat &x, const bigfloat &y) {
226 int test;
227 test = mpf_cmp(x.x, y.x);
228 if (test > 0) {
229 return 1;
230 } else {
231 return 0;
232 }
233 }
234
235 friend int operator<(const bigfloat &x, const bigfloat &y) {
236 int test;
237 test = mpf_cmp(x.x, y.x);
238 if (test < 0) {
239 return 1;
240 } else {
241 return 0;
242 }
243 }
244
245 friend int sgn(const bigfloat &);
246
247 /* Miscellaneous Functions */
248
249 // friend bigfloat& random(void);
250};
251
252/*
253class complex_bf {
254
255 private:
256 bigfloat x, y;
257
258 public:
259 complex_bf() {
260 x = 0l;
261 y = 0l;
262 }
263
264 complex_bf(bigfloat a, bigfloat b) {
265 x = a;
266 y = b;
267 }
268
269 complex_bf(double a) {
270 x = a;
271 y = 0l;
272 }
273
274 complex_bf& operator=(const complex_bf& a) {
275 x = a.x;
276 y = a.y;
277 return *this;
278 }
279
280 complex_bf& operator=(const double a) {
281 x = a;
282 y = 0l;
283 return *this;
284 }
285
286 complex_bf& operator=(const long a) {
287 x = a;
288 y = 0l;
289 return *this;
290 }
291
292 complex_bf conj(const complex_bf &a) {
293 complex_bf b(a.x, -a.y);
294 return b;
295 }
296
297 friend complex_bf operator+(const complex_bf& a, const complex_bf& b) {
298 complex_bf c(a.x+b.x, a.y+b.y);
299 return c;
300 }
301
302 friend complex_bf operator-(const complex_bf& a, const complex_bf& b) {
303 complex_bf c(a.x-b.x, a.y-b.y);
304 return c;
305 }
306
307 friend complex_bf operator*(const complex_bf& a, const complex_bf& b) {
308 complex_bf c(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);
309 return c;
310 }
311
312 friend complex_bf operator*(const long a, const complex_bf& b) {
313 complex_bf c(b.x*(bigfloat)a, b.y*(bigfloat)a);
314 return c;
315 }
316
317 friend complex_bf operator*(const bigfloat &a, const complex_bf& b) {
318 complex_bf c(a*b.x, a*b.y);
319 return c;
320 }
321
322 friend complex_bf operator*(const complex_bf &a, const bigfloat& b) {
323 return operator*(b, a);
324 }
325
326 friend complex_bf operator/(const complex_bf& a, const complex_bf& b) {
327 bigfloat denominator = abs_complex(b);
328 complex_bf numerator = a*conj(b);
329 return numerator / denominator;
330 }
331
332 friend complex_bf operator/(const complex_bf& a, const bigfloat& b) {
333 return new complex_bf(a.x/b, a.y/b);
334 }
335
336 complex_bf& operator+=(const complex_bf& y) { return *this = *this + y; }
337 complex_bf& operator-=(const complex_bf& y) { return *this = *this - y; }
338 complex_bf& operator*=(const complex_bf& y) { return *this = *this * y; }
339 complex_bf& operator/=(const complex_bf& y) { return *this = *this / y; }
340
341 friend bigfloat abs_complex(const complex_bf& a){
342 return a.x*a.x+a.y*a.y;
343 }
344
345};
346*/
347#endif
Definition bigfloat.h:19