1 | |
---|
2 | |
---|
3 | /**************************************************************************\ |
---|
4 | |
---|
5 | MODULE: quad_float |
---|
6 | |
---|
7 | SUMMARY: |
---|
8 | |
---|
9 | The class quad_float is used to represent quadruple precision numbers. |
---|
10 | Thus, with standard IEEE floating point, you should get the equivalent |
---|
11 | of about 106 bits of precision (but actually just a bit less). |
---|
12 | |
---|
13 | The interface allows you to treat quad_floats as if they were |
---|
14 | "ordinary" floating point types. |
---|
15 | |
---|
16 | See below for more implementation details. |
---|
17 | |
---|
18 | |
---|
19 | \**************************************************************************/ |
---|
20 | |
---|
21 | #include <NTL/ZZ.h> |
---|
22 | |
---|
23 | |
---|
24 | class quad_float { |
---|
25 | public: |
---|
26 | |
---|
27 | quad_float(); // = 0 |
---|
28 | |
---|
29 | quad_float(const quad_float& a); // copy constructor |
---|
30 | |
---|
31 | quad_float& operator=(const quad_float& a); // assignment operator |
---|
32 | quad_float& operator=(double a); |
---|
33 | |
---|
34 | ~quad_float(); |
---|
35 | |
---|
36 | |
---|
37 | static void SetOutputPrecision(long p); |
---|
38 | // This sets the number of decimal digits to be output. Default is |
---|
39 | // 10. |
---|
40 | |
---|
41 | |
---|
42 | static long OutputPrecision(); |
---|
43 | // returns current output precision. |
---|
44 | |
---|
45 | |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | /**************************************************************************\ |
---|
50 | |
---|
51 | Arithmetic Operations |
---|
52 | |
---|
53 | \**************************************************************************/ |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | quad_float operator +(const quad_float& x, const quad_float& y); |
---|
59 | quad_float operator -(const quad_float& x, const quad_float& y); |
---|
60 | quad_float operator *(const quad_float& x, const quad_float& y); |
---|
61 | quad_float operator /(const quad_float& x, const quad_float& y); |
---|
62 | |
---|
63 | |
---|
64 | // PROMOTIONS: operators +, -, *, / promote double to quad_float |
---|
65 | // on (x, y). |
---|
66 | |
---|
67 | quad_float operator -(const quad_float& x); |
---|
68 | |
---|
69 | quad_float& operator += (quad_float& x, const quad_float& y); |
---|
70 | quad_float& operator += (quad_float& x, double y); |
---|
71 | |
---|
72 | quad_float& operator -= (quad_float& x, const quad_float& y); |
---|
73 | quad_float& operator -= (quad_float& x, double y); |
---|
74 | |
---|
75 | quad_float& operator *= (quad_float& x, const quad_float& y); |
---|
76 | quad_float& operator *= (quad_float& x, double y); |
---|
77 | |
---|
78 | quad_float& operator /= (quad_float& x, const quad_float& y); |
---|
79 | quad_float& operator /= (quad_float& x, double y); |
---|
80 | |
---|
81 | quad_float& operator++(quad_float& a); // prefix |
---|
82 | void operator++(quad_float& a, int); // postfix |
---|
83 | |
---|
84 | quad_float& operator--(quad_float& a); // prefix |
---|
85 | void operator--(quad_float& a, int); // postfix |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | /**************************************************************************\ |
---|
90 | |
---|
91 | Comparison |
---|
92 | |
---|
93 | \**************************************************************************/ |
---|
94 | |
---|
95 | |
---|
96 | long operator> (const quad_float& x, const quad_float& y); |
---|
97 | long operator>=(const quad_float& x, const quad_float& y); |
---|
98 | long operator< (const quad_float& x, const quad_float& y); |
---|
99 | long operator<=(const quad_float& x, const quad_float& y); |
---|
100 | long operator==(const quad_float& x, const quad_float& y); |
---|
101 | long operator!=(const quad_float& x, const quad_float& y); |
---|
102 | |
---|
103 | long sign(const quad_float& x); // sign of x, -1, 0, +1 |
---|
104 | long compare(const quad_float& x, const quad_float& y); // sign of x - y |
---|
105 | |
---|
106 | // PROMOTIONS: operators >, ..., != and function compare |
---|
107 | // promote double to quad_float on (x, y). |
---|
108 | |
---|
109 | |
---|
110 | /**************************************************************************\ |
---|
111 | |
---|
112 | Input/Output |
---|
113 | Input Syntax: |
---|
114 | |
---|
115 | <number>: [ "-" ] <unsigned-number> |
---|
116 | <unsigned-number>: <dotted-number> [ <e-part> ] | <e-part> |
---|
117 | <dotted-number>: <digits> | <digits> "." <digits> | "." <digits> | <digits> "." |
---|
118 | <digits>: <digit> <digits> | <digit> |
---|
119 | <digit>: "0" | ... | "9" |
---|
120 | <e-part>: ( "E" | "e" ) [ "+" | "-" ] <digits> |
---|
121 | |
---|
122 | Examples of valid input: |
---|
123 | |
---|
124 | 17 1.5 0.5 .5 5. -.5 e10 e-10 e+10 1.5e10 .5e10 .5E10 |
---|
125 | |
---|
126 | Note that the number of decimal digits of precision that are used |
---|
127 | for output can be set to any number p >= 1 by calling |
---|
128 | the routine quad_float::SetOutputPrecision(p). |
---|
129 | The default value of p is 10. |
---|
130 | The current value of p is returned by a call to quad_float::OutputPrecision(). |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | \**************************************************************************/ |
---|
135 | |
---|
136 | |
---|
137 | istream& operator >> (istream& s, quad_float& x); |
---|
138 | ostream& operator << (ostream& s, const quad_float& x); |
---|
139 | |
---|
140 | |
---|
141 | /**************************************************************************\ |
---|
142 | |
---|
143 | Miscellaneous |
---|
144 | |
---|
145 | \**************************************************************************/ |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | quad_float sqrt(const quad_float& x); |
---|
150 | quad_float floor(const quad_float& x); |
---|
151 | quad_float ceil(const quad_float& x); |
---|
152 | quad_float trunc(const quad_float& x); |
---|
153 | quad_float fabs(const quad_float& x); |
---|
154 | quad_float exp(const quad_float& x); |
---|
155 | quad_float log(const quad_float& x); |
---|
156 | |
---|
157 | |
---|
158 | void power(quad_float& x, const quad_float& a, long e); // x = a^e |
---|
159 | quad_float power(const quad_float& a, long e); |
---|
160 | |
---|
161 | void power2(quad_float& x, long e); // x = 2^e |
---|
162 | quad_float power2_quad_float(long e); |
---|
163 | |
---|
164 | quad_float ldexp(const quad_float& x, long e); // return x*2^e |
---|
165 | |
---|
166 | long IsFinite(quad_float *x); // checks if x is "finite" |
---|
167 | // pointer is used for compatability with |
---|
168 | // IsFinite(double*) |
---|
169 | |
---|
170 | |
---|
171 | void random(quad_float& x); |
---|
172 | quad_float random_quad_float(); |
---|
173 | // generate a random quad_float x with 0 <= x <= 1 |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | /***********************************************************************\ |
---|
180 | |
---|
181 | IMPLEMENTATION DETAILS |
---|
182 | |
---|
183 | A quad_float x is represented as a pair of doubles, x.hi and x.lo, |
---|
184 | such that the number represented by x is x.hi + x.lo, where |
---|
185 | |
---|
186 | |x.lo| <= 0.5*ulp(x.hi), (*) |
---|
187 | |
---|
188 | and ulp(y) means "unit in the last place of y". |
---|
189 | |
---|
190 | For the software to work correctly, IEEE Standard Arithmetic is sufficient. |
---|
191 | That includes just about every modern computer; the only exception I'm |
---|
192 | aware of is Intel x86 platforms running Linux (but you can still |
---|
193 | use this platform--see below). |
---|
194 | |
---|
195 | Also sufficient is any platform that implements arithmetic with correct |
---|
196 | rounding, i.e., given double floating point numbers a and b, a op b |
---|
197 | is computed exactly and then rounded to the nearest double. |
---|
198 | The tie-breaking rule is not important. |
---|
199 | |
---|
200 | This is a rather wierd representation; although it gives one |
---|
201 | essentially twice the precision of an ordinary double, it is |
---|
202 | not really the equivalent of quadratic precision (despite the name). |
---|
203 | For example, the number 1 + 2^{-200} can be represented exactly as |
---|
204 | a quad_float. Also, there is no real notion of "machine precision". |
---|
205 | |
---|
206 | THE INTEL x86 PROBLEM |
---|
207 | |
---|
208 | Although just about every modern processor implements the IEEE |
---|
209 | floating point standard, there is still can be problems |
---|
210 | on processors that support IEEE extended double precision. |
---|
211 | The only processor I know of that supports this is the x86/Pentium. |
---|
212 | |
---|
213 | While extended double precision may sound like a nice thing, |
---|
214 | it is not. Normal double precision has 53 bits of precision. |
---|
215 | Extended has 64. On x86s, the FP registers have 53 or 64 bits |
---|
216 | of precision---this can be set at run-time by modifying |
---|
217 | the cpu "control word" (something that can be done |
---|
218 | only in assembly code). |
---|
219 | However, doubles stored in memory always have only 53 bits. |
---|
220 | Compilers may move values between memory and registers |
---|
221 | whenever they want, which can effectively change the value |
---|
222 | of a floating point number even though at the C/C++ level, |
---|
223 | nothing has happened that should have changed the value. |
---|
224 | Is that sick, or what? |
---|
225 | |
---|
226 | This is a real headache, and if one is not just a bit careful, |
---|
227 | the quad_float code will break. This breaking is not at all subtle, |
---|
228 | and the program QuadTest will catch the problem if it exists. |
---|
229 | |
---|
230 | You should not need to worry about any of this, because NTL automatically |
---|
231 | detects and works around these problems as best it can, as described below. |
---|
232 | It shouldn't make a mistake, but if it does, you will |
---|
233 | catch it in the QuadTest program. |
---|
234 | If things don't work quite right, you might try |
---|
235 | setting NTL_FIX_X86 or NTL_NO_FIX_X86 flags in ntl_config.h, |
---|
236 | but this should not be necessary. |
---|
237 | |
---|
238 | Here are the details about how NTL fixes the problem. |
---|
239 | |
---|
240 | The first and best way is to have the default setting of the control word |
---|
241 | be 53 bits. However, you are at the mercy of your platform |
---|
242 | (compiler, OS, run-time libraries). Windows does this, |
---|
243 | and so the problem simply does not arise here, and NTL neither |
---|
244 | detects nor fixes the problem. Linux, however, does not do this, |
---|
245 | which really sucks. Can we talk these Linux people into changing this? |
---|
246 | |
---|
247 | The second way to fix the problem is by having NTL |
---|
248 | fiddle with control word itself. If you compile NTL using a GNU compiler |
---|
249 | on an x86, this should happen automatically. |
---|
250 | On the one hand, this is not a general, portable solution, |
---|
251 | since it will only work if you use a GNU compiler, or at least one that |
---|
252 | supports GNU 'asm' syntax. |
---|
253 | On the other hand, almost everybody who compiles C++ on x86/Linux |
---|
254 | platforms uses GNU compilers (although are some commercial |
---|
255 | compilers out there that I don't know too much about). |
---|
256 | |
---|
257 | The third way to fix the problem is to 'force' all intermediate |
---|
258 | floating point results into memory. This is not an 'ideal' fix, |
---|
259 | since it is not fully equivalent to 53-bit precision (because of |
---|
260 | double rounding), but it works (although to be honest, I've never seen |
---|
261 | a full proof of correctness in this case). |
---|
262 | NTL's quad_float code does this by storing intermediate results |
---|
263 | in local variables declared to be 'volatile'. |
---|
264 | This is the solution to the problem that NTL uses if it detects |
---|
265 | the problem and can't fix it using the GNU 'asm' hack mentioned above. |
---|
266 | This solution should work on any platform that faithfully |
---|
267 | implements 'volatile' according to the ANSI C standard. |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | BACKGROUND INFO |
---|
272 | |
---|
273 | The code NTL uses algorithms designed by Knuth, Kahan, Dekker, and |
---|
274 | Linnainmaa. The original transcription to C++ was done by Douglas |
---|
275 | Priest. Enhancements and bug fixes were done by Keith Briggs |
---|
276 | (http://epidem13.plantsci.cam.ac.uk/~kbriggs). The NTL version is a |
---|
277 | stripped down version of Briggs' code, with a couple of bug fixes and |
---|
278 | portability improvements. Briggs has continued to develop his |
---|
279 | library; see his web page above for the latest version and more information. |
---|
280 | |
---|
281 | Here is a brief annotated bibliography (compiled by Priest) of papers |
---|
282 | dealing with DP and similar techniques, arranged chronologically. |
---|
283 | |
---|
284 | Kahan, W., Further Remarks on Reducing Truncation Errors, |
---|
285 | {\it Comm.\ ACM\/} {\bf 8} (1965), 40. |
---|
286 | |
---|
287 | M{\o}ller, O., Quasi Double Precision in Floating-Point Addition, |
---|
288 | {\it BIT\/} {\bf 5} (1965), 37--50. |
---|
289 | |
---|
290 | The two papers that first presented the idea of recovering the |
---|
291 | roundoff of a sum. |
---|
292 | |
---|
293 | Dekker, T., A Floating-Point Technique for Extending the Available |
---|
294 | Precision, {\it Numer.\ Math.} {\bf 18} (1971), 224--242. |
---|
295 | |
---|
296 | The classic reference for DP algorithms for sum, product, quotient, |
---|
297 | and square root. |
---|
298 | |
---|
299 | Pichat, M., Correction d'une Somme en Arithmetique \`a Virgule |
---|
300 | Flottante, {\it Numer.\ Math.} {\bf 19} (1972), 400--406. |
---|
301 | |
---|
302 | An iterative algorithm for computing a protracted sum to working |
---|
303 | precision by repeatedly applying the sum-and-roundoff method. |
---|
304 | |
---|
305 | Linnainmaa, S., Analysis of Some Known Methods of Improving the Accuracy |
---|
306 | of Floating-Point Sums, {\it BIT\/} {\bf 14} (1974), 167--202. |
---|
307 | |
---|
308 | Comparison of Kahan and M{\o}ller algorithms with variations given |
---|
309 | by Knuth. |
---|
310 | |
---|
311 | Bohlender, G., Floating-Point Computation of Functions with Maximum |
---|
312 | Accuracy, {\it IEEE Trans.\ Comput.} {\bf C-26} (1977), 621--632. |
---|
313 | |
---|
314 | Extended the analysis of Pichat's algorithm to compute a multi-word |
---|
315 | representation of the exact sum of n working precision numbers. |
---|
316 | This is the algorithm Kahan has called "distillation". |
---|
317 | |
---|
318 | Linnainmaa, S., Software for Doubled-Precision Floating-Point Computations, |
---|
319 | {\it ACM Trans.\ Math.\ Soft.} {\bf 7} (1981), 272--283. |
---|
320 | |
---|
321 | Generalized the hypotheses of Dekker and showed how to take advantage |
---|
322 | of extended precision where available. |
---|
323 | |
---|
324 | Leuprecht, H., and W.~Oberaigner, Parallel Algorithms for the Rounding-Exact |
---|
325 | Summation of Floating-Point Numbers, {\it Computing} {\bf 28} (1982), 89--104. |
---|
326 | |
---|
327 | Variations of distillation appropriate for parallel and vector |
---|
328 | architectures. |
---|
329 | |
---|
330 | Kahan, W., Paradoxes in Concepts of Accuracy, lecture notes from Joint |
---|
331 | Seminar on Issues and Directions in Scientific Computation, Berkeley, 1989. |
---|
332 | |
---|
333 | Gives the more accurate DP sum I've shown above, discusses some |
---|
334 | examples. |
---|
335 | |
---|
336 | Priest, D., Algorithms for Arbitrary Precision Floating Point Arithmetic, |
---|
337 | in P.~Kornerup and D.~Matula, Eds., {\it Proc.\ 10th Symposium on Com- |
---|
338 | puter Arithmetic}, IEEE Computer Society Press, Los Alamitos, Calif., 1991. |
---|
339 | |
---|
340 | Extends from DP to arbitrary precision; gives portable algorithms and |
---|
341 | general proofs. |
---|
342 | |
---|
343 | Sorensen, D., and P.~Tang, On the Orthogonality of Eigenvectors Computed |
---|
344 | by Divide-and-Conquer Techniques, {\it SIAM J.\ Num.\ Anal.} {\bf 28} |
---|
345 | (1991), 1752--1775. |
---|
346 | |
---|
347 | Uses some DP arithmetic to retain orthogonality of eigenvectors |
---|
348 | computed by a parallel divide-and-conquer scheme. |
---|
349 | |
---|
350 | Priest, D., On Properties of Floating Point Arithmetics: Numerical Stability |
---|
351 | and the Cost of Accurate Computations, Ph.D. dissertation, University |
---|
352 | of California at Berkeley, 1992. |
---|
353 | |
---|
354 | More examples, organizes proofs in terms of common properties of fp |
---|
355 | addition/subtraction, gives other summation algorithms. |
---|
356 | |
---|
357 | \***********************************************************************/ |
---|
358 | |
---|