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