1 | |
---|
2 | #ifndef NTL_quad_float__H |
---|
3 | #define NTL_quad_float__H |
---|
4 | |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 1997, 1998, 1999, 2000 Victor Shoup |
---|
8 | |
---|
9 | This program is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License |
---|
11 | as published by the Free Software Foundation; either version 2 |
---|
12 | of the License, or (at your option) any later version. |
---|
13 | |
---|
14 | This program is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | |
---|
23 | ***************************************************** |
---|
24 | |
---|
25 | The quad_float package is derived from the doubledouble package of |
---|
26 | Keith Briggs. However, the version employed in NTL has been extensively |
---|
27 | modified. Below, I attach the copyright notice from the original |
---|
28 | doubledouble package, which is currently available at |
---|
29 | |
---|
30 | http://www.labs.bt.com/people/briggsk2 |
---|
31 | |
---|
32 | ***************************************************** |
---|
33 | |
---|
34 | Copyright (C) 1997 Keith Martin Briggs |
---|
35 | |
---|
36 | Except where otherwise indicated, |
---|
37 | this program is free software; you can redistribute it and/or |
---|
38 | modify it under the terms of the GNU General Public License |
---|
39 | as published by the Free Software Foundation; either version 2 |
---|
40 | of the License, or (at your option) any later version. |
---|
41 | |
---|
42 | This program is distributed in the hope that it will be useful, |
---|
43 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
44 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
45 | GNU General Public License for more details. |
---|
46 | |
---|
47 | You should have received a copy of the GNU General Public License |
---|
48 | along with this program; if not, write to the Free Software |
---|
49 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
50 | */ |
---|
51 | |
---|
52 | #include <NTL/ZZ.h> |
---|
53 | |
---|
54 | NTL_OPEN_NNS |
---|
55 | |
---|
56 | |
---|
57 | class quad_float { |
---|
58 | public: |
---|
59 | double hi, lo; |
---|
60 | |
---|
61 | // Constructors |
---|
62 | quad_float() : hi(0), lo(0) {} |
---|
63 | |
---|
64 | inline quad_float& operator=(double x); |
---|
65 | |
---|
66 | static long oprec; |
---|
67 | static void SetOutputPrecision(long p); |
---|
68 | static long OutputPrecision() { return oprec; } |
---|
69 | |
---|
70 | quad_float(double x, double y) : hi(x), lo(y) { } // internal use only |
---|
71 | |
---|
72 | ~quad_float() {} |
---|
73 | |
---|
74 | }; // end class quad_float |
---|
75 | |
---|
76 | #if (NTL_BITS_PER_LONG < NTL_DOUBLE_PRECISION) |
---|
77 | |
---|
78 | inline quad_float to_quad_float(long n) { return quad_float(n, 0); } |
---|
79 | inline quad_float to_quad_float(unsigned long n) { return quad_float(n, 0); } |
---|
80 | |
---|
81 | #else |
---|
82 | |
---|
83 | quad_float to_quad_float(long n); |
---|
84 | quad_float to_quad_float(unsigned long n); |
---|
85 | |
---|
86 | #endif |
---|
87 | |
---|
88 | #if (NTL_BITS_PER_INT < NTL_DOUBLE_PRECISION) |
---|
89 | |
---|
90 | inline quad_float to_quad_float(int n) { return quad_float(n, 0); } |
---|
91 | inline quad_float to_quad_float(unsigned int n) { return quad_float(n, 0); } |
---|
92 | |
---|
93 | #else |
---|
94 | |
---|
95 | inline quad_float to_quad_float(int n) |
---|
96 | { return to_quad_float(long(n)); } |
---|
97 | inline quad_float to_quad_float(unsigned int n) |
---|
98 | { return to_quad_float((unsigned long) n); } |
---|
99 | |
---|
100 | #endif |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | inline quad_float to_quad_float(double x) { return quad_float(x, 0); } |
---|
105 | // On platforms with extended doubles, this may result in an |
---|
106 | // improper quad_float object, but it should be converted to a proper |
---|
107 | // one when passed by reference to any of the arithmetic routines, |
---|
108 | // at which time x will be forced to memory. |
---|
109 | |
---|
110 | inline quad_float to_quad_float(float x) |
---|
111 | { return to_quad_float(double(x)); } |
---|
112 | |
---|
113 | inline quad_float& quad_float::operator=(double x) |
---|
114 | { *this = to_quad_float(x); return *this; } |
---|
115 | |
---|
116 | quad_float operator+(const quad_float&, const quad_float& ); |
---|
117 | |
---|
118 | inline quad_float operator+(const quad_float& x, double y ) |
---|
119 | { return x + to_quad_float(y); } |
---|
120 | |
---|
121 | inline quad_float operator+(double x, const quad_float& y) |
---|
122 | { return to_quad_float(x) + y; } |
---|
123 | |
---|
124 | quad_float operator-(const quad_float&, const quad_float& ); |
---|
125 | |
---|
126 | inline quad_float operator-(const quad_float& x, double y ) |
---|
127 | { return x - to_quad_float(y); } |
---|
128 | |
---|
129 | inline quad_float operator-(double x, const quad_float& y) |
---|
130 | { return to_quad_float(x) - y; } |
---|
131 | |
---|
132 | quad_float operator*(const quad_float&, const quad_float& ); |
---|
133 | |
---|
134 | inline quad_float operator*(const quad_float& x, double y ) |
---|
135 | { return x * to_quad_float(y); } |
---|
136 | |
---|
137 | inline quad_float operator*(double x, const quad_float& y) |
---|
138 | { return to_quad_float(x) * y; } |
---|
139 | |
---|
140 | quad_float operator/(const quad_float&, const quad_float& ); |
---|
141 | |
---|
142 | inline quad_float operator/(const quad_float& x, double y ) |
---|
143 | { return x / to_quad_float(y); } |
---|
144 | |
---|
145 | inline quad_float operator/(double x, const quad_float& y) |
---|
146 | { return to_quad_float(x) / y; } |
---|
147 | |
---|
148 | quad_float operator-(const quad_float& x); |
---|
149 | |
---|
150 | quad_float& operator+= (quad_float& x, const quad_float& y); |
---|
151 | inline quad_float& operator += (quad_float& x, double y) |
---|
152 | { x += to_quad_float(y); return x; } |
---|
153 | |
---|
154 | quad_float& operator-= (quad_float& x, const quad_float& y); |
---|
155 | inline quad_float& operator-= (quad_float& x, double y) |
---|
156 | { x -= to_quad_float(y); return x; } |
---|
157 | |
---|
158 | quad_float& operator*= (quad_float& x, const quad_float& y); |
---|
159 | inline quad_float& operator*= (quad_float& x, double y) |
---|
160 | { x *= to_quad_float(y); return x; } |
---|
161 | |
---|
162 | quad_float& operator/= (quad_float& x, const quad_float& y); |
---|
163 | inline quad_float& operator/= (quad_float& x, double y) |
---|
164 | { x /= to_quad_float(y); return x; } |
---|
165 | |
---|
166 | inline quad_float& operator++(quad_float& a) { a += 1.0; return a; } |
---|
167 | inline void operator++(quad_float& a, int) { a += 1.0; } |
---|
168 | |
---|
169 | inline quad_float& operator--(quad_float& a) { a -= 1.0; return a; } |
---|
170 | inline void operator--(quad_float& a, int) { a -= 1.0; } |
---|
171 | |
---|
172 | |
---|
173 | long operator> (const quad_float& x, const quad_float& y); |
---|
174 | long operator>=(const quad_float& x, const quad_float& y); |
---|
175 | long operator< (const quad_float& x, const quad_float& y); |
---|
176 | long operator<=(const quad_float& x, const quad_float& y); |
---|
177 | long operator==(const quad_float& x, const quad_float& y); |
---|
178 | long operator!=(const quad_float& x, const quad_float& y); |
---|
179 | |
---|
180 | inline long operator> (const quad_float& x, double y) |
---|
181 | { return x > to_quad_float(y); } |
---|
182 | inline long operator> (double x, const quad_float& y) |
---|
183 | { return to_quad_float(x) > y; } |
---|
184 | |
---|
185 | inline long operator>=(const quad_float& x, double y) |
---|
186 | { return x >= to_quad_float(y); } |
---|
187 | inline long operator>=(double x, const quad_float& y) |
---|
188 | { return to_quad_float(x) >= y; } |
---|
189 | |
---|
190 | inline long operator< (const quad_float& x, double y) |
---|
191 | { return x < to_quad_float(y); } |
---|
192 | inline long operator< (double x, const quad_float& y) |
---|
193 | { return to_quad_float(x) < y; } |
---|
194 | |
---|
195 | inline long operator<=(const quad_float& x, double y) |
---|
196 | { return x <= to_quad_float(y); } |
---|
197 | inline long operator<=(double x, const quad_float& y) |
---|
198 | { return to_quad_float(x) <= y; } |
---|
199 | |
---|
200 | inline long operator!=(const quad_float& x, double y) |
---|
201 | { return x != to_quad_float(y); } |
---|
202 | inline long operator!=(double x, const quad_float& y) |
---|
203 | { return to_quad_float(x) != y; } |
---|
204 | |
---|
205 | inline long operator==(const quad_float& x, double y) |
---|
206 | { return x == to_quad_float(y); } |
---|
207 | inline long operator==(double x, const quad_float& y) |
---|
208 | { return to_quad_float(x) == y; } |
---|
209 | |
---|
210 | |
---|
211 | inline long sign(const quad_float& x){ |
---|
212 | if (x.hi>0.0) return 1; else if (x.hi<0.0) return -1; else return 0; |
---|
213 | } |
---|
214 | |
---|
215 | long compare(const quad_float&, const quad_float&); |
---|
216 | |
---|
217 | inline long compare(const quad_float& x, double y) |
---|
218 | { return compare(x, to_quad_float(y)); } |
---|
219 | |
---|
220 | inline long compare(double x, const quad_float& y) |
---|
221 | { return compare(to_quad_float(x), y); } |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | NTL_SNS istream& operator >> (NTL_SNS istream&, quad_float&); |
---|
226 | NTL_SNS ostream& operator << (NTL_SNS ostream&, const quad_float&); |
---|
227 | |
---|
228 | |
---|
229 | quad_float sqrt(const quad_float&); |
---|
230 | quad_float floor(const quad_float&); |
---|
231 | quad_float ceil(const quad_float&); |
---|
232 | quad_float trunc(const quad_float&); |
---|
233 | quad_float fabs(const quad_float&); |
---|
234 | |
---|
235 | void power(quad_float&, const quad_float&, long); |
---|
236 | inline quad_float power(const quad_float& x, long e) |
---|
237 | { quad_float z; power(z, x, e); return z; } |
---|
238 | |
---|
239 | void power2(quad_float&, long); |
---|
240 | inline quad_float power2_quad_float(long e) |
---|
241 | { quad_float z; power2(z, e); return z; } |
---|
242 | |
---|
243 | |
---|
244 | long to_long(const quad_float&); |
---|
245 | inline int to_int(const quad_float& x) { return int(to_long(x)); } |
---|
246 | |
---|
247 | inline double to_double(const quad_float& x) { return x.hi; } |
---|
248 | |
---|
249 | inline float to_float(const quad_float& x) { return float(x.hi); } |
---|
250 | |
---|
251 | |
---|
252 | inline void conv(quad_float& x, int a) { x = to_quad_float(a); } |
---|
253 | inline void conv(quad_float& x, long a) { x = to_quad_float(a); } |
---|
254 | |
---|
255 | inline void conv(quad_float& x, unsigned int a) { x = to_quad_float(a); } |
---|
256 | inline void conv(quad_float& x, unsigned long a) { x = to_quad_float(a); } |
---|
257 | |
---|
258 | inline void conv(quad_float& x, float a) { x = to_quad_float(a); } |
---|
259 | inline void conv(quad_float& x, double a) { x = to_quad_float(a); } |
---|
260 | |
---|
261 | |
---|
262 | inline void conv(long& x, const quad_float& a) { x = to_long(a); } |
---|
263 | inline void conv(int& x, const quad_float& a) { x = to_int(a); } |
---|
264 | inline void conv(double& x, const quad_float& a) { x = to_double(a); } |
---|
265 | inline void conv(float& x, const quad_float& a) { x = to_float(a); } |
---|
266 | |
---|
267 | void conv(quad_float&, const ZZ&); |
---|
268 | inline quad_float to_quad_float(const ZZ& x) |
---|
269 | { quad_float z; conv(z, x); return z; } |
---|
270 | |
---|
271 | void conv(ZZ&, const quad_float&); |
---|
272 | inline ZZ to_ZZ(const quad_float& a) |
---|
273 | { ZZ x; conv(x, a); NTL_OPT_RETURN(ZZ, x); } |
---|
274 | |
---|
275 | inline void conv(quad_float& x, const quad_float& a) |
---|
276 | { x = a; } |
---|
277 | inline quad_float to_quad_float(const quad_float& a) |
---|
278 | { return a; } |
---|
279 | |
---|
280 | quad_float to_quad_float(const char *s); |
---|
281 | inline void conv(quad_float& x, const char *s) |
---|
282 | { x = to_quad_float(s); } |
---|
283 | |
---|
284 | long IsFinite(quad_float *x); |
---|
285 | |
---|
286 | long PrecisionOK(); |
---|
287 | |
---|
288 | quad_float ldexp(const quad_float& x, long exp); |
---|
289 | |
---|
290 | quad_float exp(const quad_float& x); |
---|
291 | quad_float log(const quad_float& x); |
---|
292 | |
---|
293 | void random(quad_float& x); |
---|
294 | quad_float random_quad_float(); |
---|
295 | |
---|
296 | |
---|
297 | NTL_CLOSE_NNS |
---|
298 | |
---|
299 | #endif |
---|