4665
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4380
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2017 by eXept Software AG
|
4395
|
5 |
All Rights Reserved
|
4380
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
LimitedPrecisionReal variableByteSubclass:#QDouble
|
|
19 |
instanceVariableNames:''
|
4454
|
20 |
classVariableNames:'DefaultPrintFormat E Epsilon FMax FMin InvFact Ln10 Ln2 NaN Pi
|
|
21 |
QDoubleOne QDoubleZero'
|
4380
|
22 |
poolDictionaries:''
|
|
23 |
category:'Magnitude-Numbers'
|
|
24 |
!
|
|
25 |
|
|
26 |
!QDouble primitiveDefinitions!
|
4413
|
27 |
%{
|
|
28 |
#include <stdio.h>
|
|
29 |
#include <errno.h>
|
|
30 |
|
|
31 |
#define __USE_ISOC9X 1
|
|
32 |
#define __USE_ISOC99 1
|
|
33 |
#include <math.h>
|
|
34 |
|
|
35 |
/*
|
|
36 |
* on some systems, errno is a macro ... check for it here
|
|
37 |
*/
|
|
38 |
#ifndef errno
|
|
39 |
extern errno;
|
|
40 |
#endif
|
|
41 |
|
|
42 |
#if !defined (__win32__)
|
|
43 |
# include <locale.h>
|
|
44 |
#endif
|
|
45 |
|
|
46 |
#if defined (__aix__)
|
|
47 |
# include <float.h>
|
|
48 |
#endif
|
|
49 |
|
|
50 |
#if defined(__irix__) || defined(__solaris__) || defined(__sunos__)
|
|
51 |
# include <nan.h>
|
|
52 |
#endif
|
|
53 |
|
|
54 |
#if defined(__linux__)
|
|
55 |
# ifndef NAN
|
|
56 |
# include <bits/nan.h>
|
|
57 |
# endif
|
|
58 |
#endif
|
|
59 |
|
|
60 |
#ifdef __win32__
|
|
61 |
# ifndef isinf
|
|
62 |
# define isinf(x) \
|
|
63 |
((((unsigned int *)(&x))[0] == 0x00000000) && \
|
|
64 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000))
|
|
65 |
# endif
|
|
66 |
#endif
|
|
67 |
|
|
68 |
#if defined(__x86__) || defined(__x86_64__)
|
|
69 |
|
|
70 |
# ifndef _FPU_EXTENDED
|
|
71 |
# define _FPU_EXTENDED 0x0300
|
|
72 |
# endif
|
|
73 |
|
|
74 |
# ifndef _FPU_DOUBLE
|
|
75 |
# define _FPU_DOUBLE 0x0200
|
|
76 |
# endif
|
|
77 |
|
|
78 |
# if defined( __win32__ ) && (defined( __BORLANDC__ ) || defined( __VISUALC__ ))
|
|
79 |
|
|
80 |
# define fpu_fix_start(old_cw_ptr)\
|
|
81 |
{\
|
|
82 |
*old_cw_ptr = _control87(0, 0); \
|
|
83 |
_control87(_FPU_DOUBLE, _FPU_EXTENDED);\
|
|
84 |
}
|
|
85 |
|
|
86 |
# define fpu_fix_end(old_cw_ptr)\
|
|
87 |
{\
|
|
88 |
_control87(*old_cw_ptr, _FPU_EXTENDED);\
|
|
89 |
}
|
|
90 |
|
|
91 |
# else // assume MINGW, GCC or CLANG
|
|
92 |
|
|
93 |
# ifndef _FPU_GETCW
|
|
94 |
# define _FPU_GETCW(x) asm volatile ("fnstcw %0":"=m" (x));
|
|
95 |
# endif
|
|
96 |
# ifndef _FPU_SETCW
|
|
97 |
# define _FPU_SETCW(x) asm volatile ("fldcw %0": :"m" (x));
|
|
98 |
# endif
|
|
99 |
|
|
100 |
# define fpu_fix_start(old_cw_ptr)\
|
|
101 |
{\
|
|
102 |
volatile unsigned short cw, new_cw;\
|
|
103 |
_FPU_GETCW(cw);\
|
|
104 |
new_cw = (cw & ~_FPU_EXTENDED) | _FPU_DOUBLE;\
|
|
105 |
_FPU_SETCW(new_cw);\
|
|
106 |
*old_cw_ptr = cw;\
|
|
107 |
}
|
|
108 |
|
|
109 |
# define fpu_fix_end(old_cw_ptr)\
|
|
110 |
{\
|
|
111 |
volatile unsigned short cw = *old_cw_ptr;\
|
|
112 |
_FPU_SETCW(cw);\
|
|
113 |
}
|
|
114 |
|
|
115 |
# endif
|
|
116 |
|
|
117 |
#endif
|
|
118 |
|
|
119 |
|
|
120 |
struct qd_real {
|
|
121 |
double x[4]; /* The Components. */
|
|
122 |
};
|
|
123 |
|
|
124 |
struct __quadDoubleStruct {
|
|
125 |
STX_OBJ_HEADER
|
|
126 |
#ifdef __NEED_DOUBLE_ALIGN
|
|
127 |
__FILLTYPE_DOUBLE f_filler;
|
|
128 |
#endif
|
|
129 |
double d_quadDoubleValue[4];
|
|
130 |
};
|
|
131 |
|
|
132 |
#define __QuadDoubleInstPtr(obj) ((struct __quadDoubleStruct *)(__objPtr(obj)))
|
|
133 |
|
|
134 |
#ifndef __isQuadDouble
|
|
135 |
# define __isQuadDouble(o) \
|
|
136 |
(__Class(o) == @global(QuadDouble))
|
|
137 |
#endif
|
|
138 |
|
|
139 |
#ifndef __qIsQuadDouble
|
|
140 |
# define __qIsQuadDouble(o) \
|
|
141 |
(__qClass(o) == @global(QuadDouble))
|
|
142 |
#endif
|
|
143 |
|
|
144 |
#define __qNew_qdReal(newQD, d0,d1,d2,d3) { \
|
|
145 |
__qNew(newQD, sizeof(struct __quadDoubleStruct)); \
|
|
146 |
__stx_setClass(newQD, QDouble); \
|
|
147 |
__QuadDoubleInstPtr(newQD)->d_quadDoubleValue[0] = d0; \
|
|
148 |
__QuadDoubleInstPtr(newQD)->d_quadDoubleValue[1] = d1; \
|
|
149 |
__QuadDoubleInstPtr(newQD)->d_quadDoubleValue[2] = d2; \
|
|
150 |
__QuadDoubleInstPtr(newQD)->d_quadDoubleValue[3] = d3; \
|
|
151 |
}
|
|
152 |
|
4420
|
153 |
// sigh: not all compilers (borland) support inline functions;
|
|
154 |
// therefore we have to use macros...
|
|
155 |
// sigh2: c-macros are unhygienic - to avoid catching/hiding variable bindings,
|
|
156 |
// use different names in each macros (i.e. a_xxx)
|
|
157 |
|
4413
|
158 |
// qd_real(c0, c1, c2, c3);
|
|
159 |
|
|
160 |
#define _QD_SPLITTER 134217729.0 // = 2^27 + 1
|
|
161 |
#define _QD_SPLIT_THRESH 6.69692879491417e+299 // = 2^996
|
|
162 |
|
4420
|
163 |
#define m_quick_two_sum(rslt_1, a_1, b_1, err_1)\
|
|
164 |
{\
|
|
165 |
double s_1 = (a_1) + (b_1);\
|
|
166 |
(err_1) = (b_1) - (s_1 - (a_1));\
|
|
167 |
(rslt_1) = s_1; \
|
4413
|
168 |
}
|
|
169 |
|
4420
|
170 |
#define m_quick_two_diff(rslt_2, a_2, b_2, err_2)\
|
|
171 |
{\
|
|
172 |
double s_2 = (a_2) - (b_2);\
|
|
173 |
(err_2) = ((a_2) - s_2) - (b_2);\
|
|
174 |
(rslt_2) = s_2;\
|
4413
|
175 |
}
|
|
176 |
|
4420
|
177 |
#define m_two_sum(rslt_3, a_3, b_3, err_3)\
|
|
178 |
{\
|
4981
|
179 |
double s_3 = (a_3) + (b_3);\
|
|
180 |
double v_3 = s_3 - (a_3);\
|
|
181 |
(err_3) = ((a_3) - (s_3 - v_3)) + ((b_3) - v_3);\
|
|
182 |
(rslt_3) = s_3;\
|
4413
|
183 |
}
|
|
184 |
|
|
185 |
/* Computes fl(a-b) and err(a-b). */
|
4420
|
186 |
#define m_two_diff(rslt_4, a_4, b_4, err_4)\
|
|
187 |
{\
|
4981
|
188 |
double s_4 = (a_4) - (b_4);\
|
|
189 |
double bb_4 = s_4 - (a_4);\
|
|
190 |
(err_4) = ((a_4) - (s_4 - bb_4)) - ((b_4) + bb_4);\
|
|
191 |
(rslt_4) = s_4;\
|
4413
|
192 |
}
|
|
193 |
|
4420
|
194 |
#define m_three_sum(a_5, b_5, c_5)\
|
|
195 |
{ \
|
|
196 |
double t1_5, t2_5, t3_5; \
|
4981
|
197 |
m_two_sum(t1_5, (a_5), (b_5), t2_5); \
|
|
198 |
m_two_sum((a_5), (c_5), t1_5, t3_5); \
|
|
199 |
m_two_sum((b_5), t2_5, t3_5, (c_5)); \
|
4413
|
200 |
}
|
|
201 |
|
4420
|
202 |
#define m_three_sum2(a_6, b_6, c_6)\
|
|
203 |
{\
|
|
204 |
double t1_6, t2_6, t3_6;\
|
4981
|
205 |
m_two_sum(t1_6, (a_6), (b_6), t2_6);\
|
|
206 |
m_two_sum((a_6), (c_6), t1_6, t3_6);\
|
|
207 |
(b_6) = t2_6 + t3_6;\
|
4413
|
208 |
}
|
|
209 |
|
|
210 |
#ifndef QD_FMS
|
|
211 |
|
|
212 |
/* Computes high word and lo word of a */
|
4420
|
213 |
#define m_split(a_7, hi_7, lo_7)\
|
|
214 |
{\
|
|
215 |
double temp_7;\
|
4981
|
216 |
double thi_7, tlo_7;\
|
|
217 |
if ((a_7) > _QD_SPLIT_THRESH || (a_7) < -_QD_SPLIT_THRESH) {\
|
|
218 |
(a_7) *= 3.7252902984619140625e-09;\
|
|
219 |
temp_7 = _QD_SPLITTER * (a_7);\
|
|
220 |
thi_7 = temp_7 - (temp_7 - (a_7));\
|
|
221 |
tlo_7 = (a_7) - thi_7;\
|
|
222 |
thi_7 *= 268435456.0;\
|
|
223 |
tlo_7 *= 268435456.0;\
|
4420
|
224 |
} else {\
|
4981
|
225 |
temp_7 = _QD_SPLITTER * (a_7);\
|
|
226 |
thi_7 = temp_7 - (temp_7 - (a_7));\
|
|
227 |
tlo_7 = (a_7) - thi_7;\
|
4420
|
228 |
}\
|
4981
|
229 |
(hi_7) = thi_7; \
|
|
230 |
(lo_7) = tlo_7; \
|
4413
|
231 |
}
|
|
232 |
|
|
233 |
#endif
|
|
234 |
|
|
235 |
|
|
236 |
#ifdef QD_FMS
|
|
237 |
|
|
238 |
/* Computes fl(a*b) and err(a*b). */
|
|
239 |
|
4420
|
240 |
#define m_two_prod(rslt_8, a_8, b_8, err_8)\
|
|
241 |
{\
|
4981
|
242 |
double p_8 = (a_8) * (b_8);\
|
|
243 |
err_8 = QD_FMS((a_8), (b_8), p_8);\
|
4420
|
244 |
rslt_8 = p_8; \
|
4413
|
245 |
}
|
|
246 |
|
|
247 |
#else
|
|
248 |
|
4420
|
249 |
#define m_two_prod(rslt_8, a_8, b_8, err_8)\
|
|
250 |
{\
|
|
251 |
double a_hi_8, a_lo_8, b_hi_8, b_lo_8;\
|
4981
|
252 |
double p_8 = (a_8) * (b_8);\
|
4420
|
253 |
m_split(a_8, a_hi_8, a_lo_8);\
|
|
254 |
m_split(b_8, b_hi_8, b_lo_8);\
|
|
255 |
err_8 = ((a_hi_8 * b_hi_8 - p_8) + a_hi_8 * b_lo_8 + a_lo_8 * b_hi_8) + a_lo_8 * b_lo_8;\
|
|
256 |
rslt_8 = p_8; \
|
4413
|
257 |
}
|
|
258 |
|
|
259 |
#endif
|
|
260 |
|
|
261 |
#ifdef QD_FMS
|
|
262 |
|
4420
|
263 |
#define m_two_sqr(rslt_9, a_9, err_9)\
|
|
264 |
{\
|
4981
|
265 |
double p_9 = (a_9) * (a_9);\
|
|
266 |
err_9 = QD_FMS((a_9), (a_9), p_9);\
|
4420
|
267 |
rslt_9 = p_9;\
|
4413
|
268 |
}
|
|
269 |
|
|
270 |
#else
|
|
271 |
|
4420
|
272 |
#define m_two_sqr(rslt_9, a_9, err_9)\
|
|
273 |
{\
|
|
274 |
double hi_9, lo_9;\
|
4981
|
275 |
double q_9 = (a_9) * (a_9);\
|
4420
|
276 |
m_split(a_9, hi_9, lo_9);\
|
|
277 |
err_9 = ((hi_9 * hi_9 - q_9) + 2.0 * hi_9 * lo_9) + lo_9 * lo_9;\
|
|
278 |
rslt_9 = q_9;\
|
4413
|
279 |
}
|
|
280 |
|
|
281 |
#endif
|
|
282 |
|
4420
|
283 |
#define m_renorm4(c0_10, c1_10, c2_10, c3_10)\
|
|
284 |
{\
|
|
285 |
double s0_10, s1_10, s2_10 = 0.0, s3_10 = 0.0;\
|
4413
|
286 |
\
|
4420
|
287 |
if (! isinf(c0_10)) { \
|
4413
|
288 |
\
|
4420
|
289 |
m_quick_two_sum(s0_10, c2_10, c3_10, c3_10);\
|
|
290 |
m_quick_two_sum(s0_10, c1_10, s0_10, c2_10);\
|
|
291 |
m_quick_two_sum(c0_10, c0_10, s0_10, c1_10);\
|
|
292 |
\
|
|
293 |
s0_10 = c0_10;\
|
|
294 |
s1_10 = c1_10;\
|
|
295 |
if (s1_10 != 0.0) {\
|
|
296 |
m_quick_two_sum(s1_10, s1_10, c2_10, s2_10);\
|
|
297 |
if (s2_10 != 0.0) {\
|
|
298 |
m_quick_two_sum(s2_10, s2_10, c3_10, s3_10);\
|
4413
|
299 |
} else {\
|
4420
|
300 |
m_quick_two_sum(s1_10, s1_10, c3_10, s2_10);\
|
4413
|
301 |
}\
|
|
302 |
} else {\
|
4420
|
303 |
m_quick_two_sum(s0_10, s0_10, c2_10, s1_10);\
|
|
304 |
if (s1_10 != 0.0) {\
|
|
305 |
m_quick_two_sum(s1_10, s1_10, c3_10, s2_10);\
|
4413
|
306 |
} else {\
|
4420
|
307 |
m_quick_two_sum(s0_10, s0_10, c3_10, s1_10);\
|
4413
|
308 |
}\
|
|
309 |
}\
|
|
310 |
\
|
4420
|
311 |
c0_10 = s0_10;\
|
|
312 |
c1_10 = s1_10;\
|
|
313 |
c2_10 = s2_10;\
|
|
314 |
c3_10 = s3_10;\
|
4413
|
315 |
}\
|
|
316 |
}
|
|
317 |
|
4420
|
318 |
#define m_renorm5(c0_11, c1_11, c2_11, c3_11, c4_11)\
|
|
319 |
{\
|
|
320 |
double s0_11, s1_11, s2_11 = 0.0, s3_11 = 0.0; \
|
4413
|
321 |
\
|
4420
|
322 |
if (! isinf(c0_11)) { \
|
|
323 |
m_quick_two_sum(s0_11, c3_11, c4_11, c4_11); \
|
|
324 |
m_quick_two_sum(s0_11, c2_11, s0_11, c3_11); \
|
|
325 |
m_quick_two_sum(s0_11, c1_11, s0_11, c2_11); \
|
|
326 |
m_quick_two_sum(c0_11, c0_11, s0_11, c1_11); \
|
4413
|
327 |
\
|
4420
|
328 |
s0_11 = c0_11; \
|
|
329 |
s1_11 = c1_11; \
|
4413
|
330 |
\
|
4420
|
331 |
m_quick_two_sum(s0_11, c0_11, c1_11, s1_11); \
|
|
332 |
if (s1_11 != 0.0) { \
|
|
333 |
m_quick_two_sum(s1_11, s1_11, c2_11, s2_11); \
|
|
334 |
if (s2_11 != 0.0) { \
|
|
335 |
m_quick_two_sum(s2_11 ,s2_11, c3_11, s3_11); \
|
|
336 |
if (s3_11 != 0.0) {\
|
|
337 |
s3_11 += c4_11; \
|
4413
|
338 |
} else {\
|
4420
|
339 |
s2_11 += c4_11;\
|
4413
|
340 |
}\
|
|
341 |
} else { \
|
4420
|
342 |
m_quick_two_sum(s1_11, s1_11, c3_11, s2_11); \
|
|
343 |
if (s2_11 != 0.0) {\
|
|
344 |
m_quick_two_sum(s2_11, s2_11, c4_11, s3_11); \
|
4413
|
345 |
} else { \
|
4420
|
346 |
m_quick_two_sum(s1_11, s1_11, c4_11, s2_11); \
|
4413
|
347 |
} \
|
|
348 |
} \
|
|
349 |
} else { \
|
4420
|
350 |
m_quick_two_sum(s0_11,s0_11, c2_11, s1_11); \
|
|
351 |
if (s1_11 != 0.0) { \
|
|
352 |
m_quick_two_sum(s1_11,s1_11, c3_11, s2_11); \
|
|
353 |
if (s2_11 != 0.0) {\
|
|
354 |
m_quick_two_sum(s2_11,s2_11, c4_11, s3_11); \
|
4413
|
355 |
} else { \
|
4420
|
356 |
m_quick_two_sum(s1_11 ,s1_11, c4_11, s2_11); \
|
4413
|
357 |
} \
|
|
358 |
} else { \
|
4420
|
359 |
m_quick_two_sum(s0_11,s0_11, c3_11, s1_11); \
|
|
360 |
if (s1_11 != 0.0) { \
|
|
361 |
m_quick_two_sum(s1_11,s1_11, c4_11, s2_11); \
|
4413
|
362 |
} else { \
|
4420
|
363 |
m_quick_two_sum(s0_11,s0_11, c4_11, s1_11); \
|
4413
|
364 |
} \
|
|
365 |
} \
|
|
366 |
} \
|
|
367 |
\
|
4420
|
368 |
c0_11 = s0_11; \
|
|
369 |
c1_11 = s1_11; \
|
|
370 |
c2_11 = s2_11; \
|
|
371 |
c3_11 = s3_11; \
|
4413
|
372 |
} \
|
|
373 |
}
|
|
374 |
|
|
375 |
%}
|
4380
|
376 |
! !
|
|
377 |
|
|
378 |
!QDouble primitiveFunctions!
|
4413
|
379 |
%{
|
|
380 |
|
|
381 |
#if 0
|
|
382 |
|
|
383 |
/*********** Basic Functions ************/
|
|
384 |
/* Computes fl(a+b) and err(a+b). Assumes |a| >= |b|. */
|
|
385 |
inline double
|
|
386 |
quick_two_sum(double a, double b, double *errPtr) {
|
|
387 |
double s = a + b;
|
|
388 |
*errPtr = b - (s - a);
|
|
389 |
return s;
|
|
390 |
}
|
|
391 |
|
|
392 |
/* Computes fl(a-b) and err(a-b). Assumes |a| >= |b| */
|
|
393 |
inline double
|
|
394 |
quick_two_diff(double a, double b, double *errPtr) {
|
|
395 |
double s = a - b;
|
|
396 |
*errPtr = (a - s) - b;
|
|
397 |
return s;
|
|
398 |
}
|
|
399 |
|
|
400 |
/* Computes fl(a+b) and err(a+b). */
|
|
401 |
inline double
|
|
402 |
two_sum(double a, double b, double *errPtr) {
|
|
403 |
double s = a + b;
|
|
404 |
double bb = s - a;
|
|
405 |
*errPtr = (a - (s - bb)) + (b - bb);
|
|
406 |
return s;
|
|
407 |
}
|
|
408 |
|
|
409 |
/* Computes fl(a-b) and err(a-b). */
|
|
410 |
inline double
|
|
411 |
two_diff(double a, double b, double *errPtr) {
|
|
412 |
double s = a - b;
|
|
413 |
double bb = s - a;
|
|
414 |
*errPtr = (a - (s - bb)) - (b + bb);
|
|
415 |
return s;
|
|
416 |
}
|
|
417 |
|
|
418 |
#ifndef QD_FMS
|
|
419 |
/* Computes high word and lo word of a */
|
|
420 |
inline void
|
|
421 |
split(double a, double *hiPtr, double *loPtr) {
|
|
422 |
double temp;
|
|
423 |
if (a > _QD_SPLIT_THRESH || a < -_QD_SPLIT_THRESH) {
|
|
424 |
a *= 3.7252902984619140625e-09; // 2^-28
|
|
425 |
temp = _QD_SPLITTER * a;
|
|
426 |
*hiPtr = temp - (temp - a);
|
|
427 |
*loPtr = a - *hiPtr;
|
|
428 |
*hiPtr *= 268435456.0; // 2^28
|
|
429 |
*loPtr *= 268435456.0; // 2^28
|
|
430 |
} else {
|
|
431 |
temp = _QD_SPLITTER * a;
|
|
432 |
*hiPtr = temp - (temp - a);
|
|
433 |
*loPtr = a - *hiPtr;
|
|
434 |
}
|
|
435 |
}
|
|
436 |
#endif
|
|
437 |
|
|
438 |
/* Computes fl(a*b) and err(a*b). */
|
|
439 |
inline double
|
|
440 |
two_prod(double a, double b, double *errPtr) {
|
|
441 |
#ifdef QD_FMS
|
|
442 |
double p = a * b;
|
|
443 |
*errPtr = QD_FMS(a, b, p);
|
|
444 |
return p;
|
|
445 |
#else
|
|
446 |
double a_hi, a_lo, b_hi, b_lo;
|
|
447 |
double p = a * b;
|
|
448 |
split(a, &a_hi, &a_lo);
|
|
449 |
split(b, &b_hi, &b_lo);
|
|
450 |
*errPtr = ((a_hi * b_hi - p) + a_hi * b_lo + a_lo * b_hi) + a_lo * b_lo;
|
|
451 |
return p;
|
|
452 |
#endif
|
|
453 |
}
|
|
454 |
|
|
455 |
/* Computes fl(a*a) and err(a*a). Faster than the above method. */
|
|
456 |
inline double
|
|
457 |
two_sqr(double a, double *errPtr) {
|
|
458 |
#ifdef QD_FMS
|
|
459 |
double p = a * a;
|
|
460 |
*errPtr = QD_FMS(a, a, p);
|
|
461 |
return p;
|
|
462 |
#else
|
|
463 |
double hi, lo;
|
|
464 |
double q = a * a;
|
|
465 |
split(a, &hi, &lo);
|
|
466 |
*errPtr = ((hi * hi - q) + 2.0 * hi * lo) + lo * lo;
|
|
467 |
return q;
|
|
468 |
#endif
|
|
469 |
}
|
|
470 |
|
|
471 |
/* Computes the nearest integer to d. */
|
|
472 |
inline double
|
|
473 |
nint(double d) {
|
|
474 |
if (d == floor(d))
|
|
475 |
return d;
|
|
476 |
return floor(d + 0.5);
|
|
477 |
}
|
|
478 |
|
|
479 |
/* Computes the truncated integer. */
|
|
480 |
inline double
|
|
481 |
aint(double d) {
|
|
482 |
return (d >= 0.0) ? floor(d) : ceil(d);
|
|
483 |
}
|
|
484 |
|
|
485 |
inline void
|
|
486 |
renorm4(double *c0Ptr, double *c1Ptr, double *c2Ptr, double *c3Ptr) {
|
|
487 |
double s0, s1, s2 = 0.0, s3 = 0.0;
|
|
488 |
double c0 = *c0Ptr;
|
|
489 |
|
|
490 |
if (isinf(c0)) return;
|
|
491 |
|
|
492 |
s0 = quick_two_sum(*c2Ptr, *c3Ptr, c3Ptr);
|
|
493 |
s0 = quick_two_sum(*c1Ptr, s0, c2Ptr);
|
|
494 |
c0 = quick_two_sum(c0, s0, c1Ptr);
|
|
495 |
|
|
496 |
s0 = c0;
|
|
497 |
s1 = *c1Ptr;
|
|
498 |
if (s1 != 0.0) {
|
|
499 |
s1 = quick_two_sum(s1, *c2Ptr, &s2);
|
|
500 |
if (s2 != 0.0)
|
|
501 |
s2 = quick_two_sum(s2, *c3Ptr, &s3);
|
|
502 |
else
|
|
503 |
s1 = quick_two_sum(s1, *c3Ptr, &s2);
|
4380
|
504 |
} else {
|
|
505 |
s0 = quick_two_sum(s0, *c2Ptr, &s1);
|
4413
|
506 |
if (s1 != 0.0)
|
|
507 |
s1 = quick_two_sum(s1, *c3Ptr, &s2);
|
|
508 |
else
|
|
509 |
s0 = quick_two_sum(s0, *c3Ptr, &s1);
|
|
510 |
}
|
|
511 |
|
|
512 |
*c0Ptr = s0;
|
|
513 |
*c1Ptr = s1;
|
|
514 |
*c2Ptr = s2;
|
|
515 |
*c3Ptr = s3;
|
|
516 |
}
|
|
517 |
|
|
518 |
inline void
|
|
519 |
renorm5(double *c0Ptr, double *c1Ptr, double *c2Ptr, double *c3Ptr, double *c4Ptr) {
|
|
520 |
double s0, s1, s2 = 0.0, s3 = 0.0;
|
|
521 |
|
|
522 |
if (isinf(*c0Ptr)) return;
|
|
523 |
|
|
524 |
s0 = quick_two_sum(*c3Ptr, *c4Ptr, c4Ptr);
|
|
525 |
s0 = quick_two_sum(*c2Ptr, s0, c3Ptr);
|
|
526 |
s0 = quick_two_sum(*c1Ptr, s0, c2Ptr);
|
|
527 |
*c0Ptr = quick_two_sum(*c0Ptr, s0, c1Ptr);
|
|
528 |
|
|
529 |
s0 = *c0Ptr;
|
|
530 |
s1 = *c1Ptr;
|
|
531 |
|
|
532 |
s0 = quick_two_sum(*c0Ptr, *c1Ptr, &s1);
|
|
533 |
if (s1 != 0.0) {
|
|
534 |
s1 = quick_two_sum(s1, *c2Ptr, &s2);
|
|
535 |
if (s2 != 0.0) {
|
|
536 |
s2 =quick_two_sum(s2, *c3Ptr, &s3);
|
|
537 |
if (s3 != 0.0)
|
|
538 |
s3 += *c4Ptr;
|
|
539 |
else
|
|
540 |
s2 += *c4Ptr;
|
|
541 |
} else {
|
|
542 |
s1 = quick_two_sum(s1, *c3Ptr, &s2);
|
|
543 |
if (s2 != 0.0)
|
|
544 |
s2 = quick_two_sum(s2, *c4Ptr, &s3);
|
|
545 |
else
|
|
546 |
s1 = quick_two_sum(s1, *c4Ptr, &s2);
|
|
547 |
}
|
|
548 |
} else {
|
|
549 |
s0 = quick_two_sum(s0, *c2Ptr, &s1);
|
|
550 |
if (s1 != 0.0) {
|
|
551 |
s1 = quick_two_sum(s1, *c3Ptr, &s2);
|
|
552 |
if (s2 != 0.0)
|
|
553 |
s2 = quick_two_sum(s2, *c4Ptr, &s3);
|
|
554 |
else
|
|
555 |
s1 = quick_two_sum(s1, *c4Ptr, &s2);
|
|
556 |
} else {
|
|
557 |
s0 = quick_two_sum(s0, *c3Ptr, &s1);
|
|
558 |
if (s1 != 0.0)
|
|
559 |
s1 = quick_two_sum(s1, *c4Ptr, &s2);
|
|
560 |
else
|
|
561 |
s0 = quick_two_sum(s0, *c4Ptr, &s1);
|
|
562 |
}
|
|
563 |
}
|
|
564 |
|
|
565 |
*c0Ptr = s0;
|
|
566 |
*c1Ptr = s1;
|
|
567 |
*c2Ptr = s2;
|
|
568 |
*c3Ptr = s3;
|
|
569 |
}
|
|
570 |
|
|
571 |
inline void
|
|
572 |
three_sum(double *aPtr, double *bPtr, double *cPtr) {
|
|
573 |
double t1, t2, t3;
|
|
574 |
t1 = two_sum(*aPtr, *bPtr, &t2);
|
|
575 |
*aPtr = two_sum(*cPtr, t1, &t3);
|
|
576 |
*bPtr = two_sum(t2, t3, cPtr);
|
|
577 |
}
|
|
578 |
|
|
579 |
inline void three_sum2(double *aPtr, double *bPtr, double *cPtr) {
|
|
580 |
double t1, t2, t3;
|
|
581 |
t1 = two_sum(*aPtr, *bPtr, &t2);
|
|
582 |
*aPtr = two_sum(*cPtr, t1, &t3);
|
|
583 |
*bPtr = t2 + t3;
|
|
584 |
}
|
|
585 |
#endif
|
|
586 |
|
|
587 |
#if 0
|
|
588 |
/* These are provided to give consistent
|
|
589 |
interface for double with double-double and quad-double. */
|
|
590 |
inline void
|
|
591 |
sincosh(double t, double &sinh_t, double &cosh_t) {
|
|
592 |
sinh_t = sinh(t);
|
|
593 |
cosh_t = cosh(t);
|
|
594 |
}
|
|
595 |
|
|
596 |
inline double
|
|
597 |
sqr(double t) {
|
|
598 |
return t * t;
|
|
599 |
}
|
|
600 |
|
|
601 |
inline double
|
|
602 |
to_double(double a) {
|
|
603 |
return a;
|
|
604 |
}
|
|
605 |
|
|
606 |
inline int
|
|
607 |
to_int(double a) {
|
|
608 |
return static_cast<int>(a);
|
|
609 |
}
|
|
610 |
#endif
|
|
611 |
|
|
612 |
%}
|
|
613 |
! !
|
4380
|
614 |
|
|
615 |
!QDouble class methodsFor:'documentation'!
|
|
616 |
|
|
617 |
copyright
|
|
618 |
"
|
|
619 |
COPYRIGHT (c) 2017 by eXept Software AG
|
4395
|
620 |
All Rights Reserved
|
4380
|
621 |
|
|
622 |
This software is furnished under a license and may be used
|
|
623 |
only in accordance with the terms of that license and with the
|
|
624 |
inclusion of the above copyright notice. This software may not
|
|
625 |
be provided or otherwise made available to, or used by, any
|
|
626 |
other person. No title to or ownership of the software is
|
|
627 |
hereby transferred.
|
|
628 |
"
|
|
629 |
!
|
|
630 |
|
|
631 |
documentation
|
|
632 |
"
|
4391
|
633 |
ATTENTION: ongoing, unfinished work.
|
4450
|
634 |
No warranty that this works correctly...
|
4391
|
635 |
|
4380
|
636 |
QDoubles represent rational numbers with extended, but still limited precision.
|
|
637 |
|
4451
|
638 |
In contrast to Floats (which use the C-compiler's native 64bit 'double' format),
|
4430
|
639 |
QDoubles give you roughly 200 bit or approx. 60 decimal digits of precision.
|
4380
|
640 |
|
|
641 |
Representation:
|
4981
|
642 |
QDoubles use 4 IEEE doubles, each keeping 53 bits of precision.
|
|
643 |
A qDouble's value is the sum of those 4 doubles,
|
|
644 |
and a qDouble keeps this unevaluated sum as its state.
|
|
645 |
(due to overlap, the final precision is less than 53*4)
|
4380
|
646 |
|
|
647 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation
|
4519
|
648 |
the number of decmal digits:
|
4981
|
649 |
QDouble decimalPrecision
|
|
650 |
LongFloat decimalPrecision
|
|
651 |
Float decimalPrecision
|
|
652 |
ShortFloat decimalPrecision
|
|
653 |
|
4519
|
654 |
the number of bits:
|
4981
|
655 |
QDouble precision
|
|
656 |
LongFloat precision
|
|
657 |
Float precision
|
|
658 |
ShortFloat precision
|
|
659 |
|
|
660 |
Notice:
|
|
661 |
when assigning a converted double precision number as in:
|
|
662 |
qd := 1.0 asQDouble.
|
|
663 |
you still get only a regular double precision approximation to 0.1
|
|
664 |
because the error is already inherit in the double.
|
|
665 |
|
|
666 |
For a full precision constant, you (currently) need to convert from a string
|
|
667 |
(because the compilers do not know about then, yet):
|
|
668 |
qd := QDouble readFrom:'0.1'.
|
|
669 |
|
|
670 |
To see the error of the double precision version, compute:
|
|
671 |
(0.1 asQDouble) - (QDouble readFrom:'0.1')
|
|
672 |
|
4380
|
673 |
[author:]
|
4981
|
674 |
Claus Gittinger
|
4380
|
675 |
|
|
676 |
[see also:]
|
4981
|
677 |
Number
|
|
678 |
Float ShortFloat LongFloat
|
|
679 |
Fraction FixedPoint Integer Complex
|
|
680 |
FloatArray DoubleArray
|
4380
|
681 |
"
|
4454
|
682 |
!
|
|
683 |
|
|
684 |
examples
|
|
685 |
"
|
|
686 |
Floats, LongFloats suffer from loosing bits:
|
4981
|
687 |
|
4454
|
688 |
(Float readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
689 |
-(Float readFrom:'0.333333333333333333333333333333333333333333333333333333333')
|
4981
|
690 |
-> 0.0
|
|
691 |
|
4454
|
692 |
(Float readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
693 |
= (Float readFrom:'0.333333333333333333333333333333333333333333333333333333333')
|
4981
|
694 |
-> true
|
4454
|
695 |
|
|
696 |
(Float readFrom:'0.33333333333333333333333333333333333333333333333333333333333333333333')
|
|
697 |
= (Float readFrom:'0.3333333333333333333333333333333333333333333333333333333333333333333')
|
4981
|
698 |
-> true
|
4454
|
699 |
1000 0110 1000 0101 1000 0101 1000 0101 1000 0101 1000 0101 1101 0101 0011 1111
|
|
700 |
(Float readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
701 |
= (Float readFrom:'0.3333333333333333333333333333333333333333333333333333333333333333333')
|
|
702 |
|
|
703 |
(LongFloat readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
704 |
-(LongFloat readFrom:'0.333333333333333333333333333333333333333333333333333333333')
|
4981
|
705 |
-> 0.0
|
4454
|
706 |
|
|
707 |
(LongFloat readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
708 |
= (LongFloat readFrom:'0.333333333333333333333333333333333333333333333333333333333')
|
4981
|
709 |
-> 0.0
|
4454
|
710 |
|
|
711 |
(QDouble readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
712 |
-(QDouble readFrom:'0.333333333333333333333333333333333333333333333333333333333')
|
|
713 |
|
|
714 |
(QDouble readFrom:'0.33333333333333333333333333333333333333333333333333333333333')
|
|
715 |
-(QDouble readFrom:'0.3333333333333333333333333333333333333333333333333333333333')
|
|
716 |
|
|
717 |
|
|
718 |
(QDouble readFrom:'0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333')
|
|
719 |
-(QDouble readFrom:'0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333')
|
|
720 |
"
|
4380
|
721 |
! !
|
|
722 |
|
|
723 |
!QDouble class methodsFor:'instance creation'!
|
|
724 |
|
|
725 |
basicNew
|
|
726 |
"return a new quad-precision double - here we return 0.0
|
|
727 |
Notice that numbers are usually NOT created this way ...
|
|
728 |
It's implemented here to allow things like binary store & load
|
|
729 |
of floats. (but even this support will go away eventually, it's not
|
|
730 |
a good idea to store the bits of a float - the reader might have a
|
|
731 |
totally different representation - so floats should be
|
|
732 |
binary stored in a device independent format."
|
|
733 |
|
|
734 |
%{ /* NOCONTEXT */
|
|
735 |
#ifdef __SCHTEAM__
|
|
736 |
ERROR("trying to instantiate a quad double");
|
|
737 |
#else
|
|
738 |
OBJ newFloat;
|
4404
|
739 |
|
4395
|
740 |
__qNew(newFloat, sizeof(struct __quadDoubleStruct));
|
|
741 |
__stx_setClass(newFloat, QDouble);
|
|
742 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[0] = 0.0;
|
|
743 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[1] = 0.0;
|
|
744 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[2] = 0.0;
|
|
745 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[3] = 0.0;
|
4380
|
746 |
RETURN (newFloat);
|
|
747 |
#endif /* not SCHTEAM */
|
|
748 |
%}
|
|
749 |
|
|
750 |
"
|
|
751 |
self basicNew
|
|
752 |
"
|
|
753 |
|
|
754 |
"Created: / 12-06-2017 / 16:00:38 / cg"
|
|
755 |
!
|
|
756 |
|
|
757 |
d0:d0 d1:d1 d2:d2 d3:d3
|
|
758 |
"return a new quad-precision double from individual double components"
|
|
759 |
|
|
760 |
%{ /* NOCONTEXT */
|
|
761 |
#ifdef __SCHTEAM__
|
|
762 |
ERROR("trying to instantiate a quad double");
|
|
763 |
#else
|
|
764 |
OBJ newQD;
|
|
765 |
|
4395
|
766 |
if (__isFloatLike(d0)
|
4380
|
767 |
&& __isFloatLike(d1)
|
|
768 |
&& __isFloatLike(d2)
|
4395
|
769 |
&& __isFloatLike(d3)) {
|
|
770 |
__qNew_qdReal(newQD, __floatVal(d0), __floatVal(d1),
|
|
771 |
__floatVal(d2), __floatVal(d3));
|
|
772 |
RETURN (newQD);
|
4380
|
773 |
}
|
|
774 |
#endif
|
|
775 |
%}.
|
|
776 |
self error:'invalid argument'
|
|
777 |
|
|
778 |
"
|
|
779 |
self d0: 3.141592653589793116e+00
|
4395
|
780 |
d1: 1.224646799147353207e-16
|
|
781 |
d2: -2.994769809718339666e-33
|
|
782 |
d3: 1.112454220863365282e-49
|
4380
|
783 |
"
|
|
784 |
|
|
785 |
"Created: / 12-06-2017 / 20:17:14 / cg"
|
|
786 |
!
|
|
787 |
|
|
788 |
fromDoubleArray:aDoubleArray
|
|
789 |
"return a new quad-precision double from coercing a double array"
|
|
790 |
|
|
791 |
%{ /* NOCONTEXT */
|
|
792 |
#ifdef __SCHTEAM__
|
|
793 |
ERROR("trying to instantiate a quad double");
|
|
794 |
#else
|
|
795 |
OBJ newQD;
|
|
796 |
|
4395
|
797 |
if (__isDoubleArray(aDoubleArray)) {
|
|
798 |
__qNew_qdReal(newQD,
|
|
799 |
__DoubleArrayInstPtr(aDoubleArray)->d_element[0],
|
|
800 |
__DoubleArrayInstPtr(aDoubleArray)->d_element[1],
|
|
801 |
__DoubleArrayInstPtr(aDoubleArray)->d_element[2],
|
|
802 |
__DoubleArrayInstPtr(aDoubleArray)->d_element[3]);
|
|
803 |
RETURN (newQD);
|
4380
|
804 |
}
|
|
805 |
#endif
|
|
806 |
%}.
|
|
807 |
self error:'invalid argument'
|
|
808 |
|
|
809 |
"
|
4395
|
810 |
self fromDoubleArray(DoubleArray
|
|
811 |
with: 3.141592653589793116e+00
|
|
812 |
with: 1.224646799147353207e-16
|
|
813 |
with: -2.994769809718339666e-33
|
|
814 |
with: 1.112454220863365282e-49)
|
4380
|
815 |
"
|
|
816 |
|
|
817 |
"Created: / 12-06-2017 / 18:25:32 / cg"
|
|
818 |
!
|
|
819 |
|
|
820 |
fromFloat:aFloat
|
|
821 |
"return a new quad-precision double from coercing aFloat"
|
|
822 |
|
|
823 |
%{ /* NOCONTEXT */
|
|
824 |
#ifdef __SCHTEAM__
|
|
825 |
ERROR("trying to instantiate a quad double");
|
|
826 |
#else
|
|
827 |
double dVal;
|
|
828 |
OBJ newFloat;
|
|
829 |
|
|
830 |
if (__isFloatLike(aFloat)) {
|
4395
|
831 |
dVal = __floatVal(aFloat);
|
|
832 |
} else if (__isShortFloat(aFloat)) {
|
|
833 |
dVal = __shortFloatVal(aFloat);
|
4380
|
834 |
} else {
|
4395
|
835 |
goto badArg;
|
|
836 |
}
|
|
837 |
|
|
838 |
__qNew(newFloat, sizeof(struct __quadDoubleStruct));
|
|
839 |
__stx_setClass(newFloat, QDouble);
|
|
840 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[0] = dVal;
|
|
841 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[1] = 0.0;
|
|
842 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[2] = 0.0;
|
|
843 |
__QuadDoubleInstPtr(newFloat)->d_quadDoubleValue[3] = 0.0;
|
4380
|
844 |
RETURN (newFloat);
|
|
845 |
|
|
846 |
badArg: ;
|
|
847 |
|
|
848 |
#endif
|
|
849 |
%}.
|
|
850 |
self error:'invalid argument'
|
|
851 |
|
|
852 |
"
|
|
853 |
self fromFloat:1.0
|
|
854 |
"
|
|
855 |
|
|
856 |
"Created: / 12-06-2017 / 16:06:54 / cg"
|
|
857 |
!
|
|
858 |
|
|
859 |
fromInteger:anInteger
|
|
860 |
"return a new quad-precision double from coercing anInteger"
|
|
861 |
|
|
862 |
%{ /* NOCONTEXT */
|
|
863 |
#ifndef __SCHTEAM__
|
|
864 |
OBJ newFloat;
|
|
865 |
|
|
866 |
if (__isSmallInteger(anInteger)) {
|
4981
|
867 |
INT iVal = __smallIntegerVal(anInteger);
|
|
868 |
double *a;
|
|
869 |
|
|
870 |
__qNew(newFloat, sizeof(struct __quadDoubleStruct));
|
|
871 |
__stx_setClass(newFloat, QDouble);
|
|
872 |
|
|
873 |
a = __QuadDoubleInstPtr(newFloat)->d_quadDoubleValue;
|
|
874 |
a[1] = 0.0;
|
|
875 |
a[2] = 0.0;
|
|
876 |
a[3] = 0.0;
|
|
877 |
|
|
878 |
// need more than 52bits?
|
|
879 |
if ((sizeof(INT) > 52)
|
|
880 |
&& ((iVal > 0xFFFFFFFFFFFFF)
|
|
881 |
|| (iVal < -0xFFFFFFFFFFFFF))) {
|
|
882 |
a[0] = (double)(iVal & ~0xFFFFFFFF);
|
|
883 |
a[1] = (double)(iVal & 0xFFFFFFFF);
|
|
884 |
// m_renorm4(a[0], a[1], a[2], a[3]);
|
|
885 |
} else {
|
|
886 |
a[0] = (double)iVal;
|
|
887 |
}
|
|
888 |
RETURN (newFloat);
|
4380
|
889 |
}
|
|
890 |
#endif
|
|
891 |
%}.
|
|
892 |
^ super fromInteger:anInteger
|
|
893 |
|
|
894 |
"
|
|
895 |
self fromInteger:2
|
4454
|
896 |
self fromInteger:16rFFFFFFFF -- 32bit 4294967295.0
|
|
897 |
self fromInteger:16rFFFFFFFFFFFF -- 48bit 281474976710655.0
|
|
898 |
self fromInteger:16rFFFFFFFFFFFFF -- 52bit 4503599627370495.0
|
|
899 |
self fromInteger:16rFFFFFFFFFFFFFF -- 56bit 72057594037927935.0
|
|
900 |
self fromInteger:16rFFFFFFFFFFFFFFF -- 60bit 1152921504606846975.0
|
|
901 |
self fromInteger:16r1FFFFFFFFFFFFFFF -- 61bit 2305843009213693951.0
|
|
902 |
self fromInteger:16r3FFFFFFFFFFFFFFF -- 62bit 4611686018427387903.0
|
|
903 |
self fromInteger:16r7FFFFFFFFFFFFFFF -- 63bit 9223372036854775807.0
|
|
904 |
self fromInteger:16rFFFFFFFFFFFFFFFF -- 64bit 18446744073709551615.0
|
4380
|
905 |
"
|
|
906 |
|
|
907 |
"Created: / 12-06-2017 / 16:10:10 / cg"
|
4454
|
908 |
"Modified: / 04-07-2017 / 12:51:52 / cg"
|
4380
|
909 |
! !
|
|
910 |
|
4395
|
911 |
!QDouble class methodsFor:'coercing & converting'!
|
|
912 |
|
|
913 |
coerce:aNumber
|
|
914 |
"convert the argument aNumber into an instance of the receiver's class and return it."
|
|
915 |
|
|
916 |
^ aNumber asQDouble
|
|
917 |
|
|
918 |
"Created: / 12-06-2017 / 17:13:47 / cg"
|
|
919 |
"Modified: / 12-06-2017 / 21:09:06 / cg"
|
|
920 |
! !
|
|
921 |
|
4380
|
922 |
!QDouble class methodsFor:'constants'!
|
|
923 |
|
4440
|
924 |
NaN
|
|
925 |
"return a QDouble which represents not-a-Number (i.e. an invalid number)"
|
|
926 |
|
|
927 |
NaN isNil ifTrue:[
|
4981
|
928 |
NaN := self d0:(Float NaN) d1:(Float NaN) d2:(Float NaN) d3:(Float NaN)
|
4440
|
929 |
].
|
|
930 |
^ NaN
|
|
931 |
|
|
932 |
"Created: / 21-06-2017 / 20:44:57 / cg"
|
|
933 |
!
|
|
934 |
|
4380
|
935 |
e
|
|
936 |
"return the constant e as quad precision double.
|
4433
|
937 |
(returns approx. 200 bits of precision)"
|
4380
|
938 |
|
|
939 |
E isNil ifTrue:[
|
4388
|
940 |
E := self fromDoubleArray:(DoubleArray
|
|
941 |
with: 2.718281828459045091e+00
|
|
942 |
with: 1.445646891729250158e-16
|
|
943 |
with: -2.127717108038176765e-33
|
|
944 |
with: 1.515630159841218954e-49)
|
|
945 |
].
|
4380
|
946 |
^ E
|
|
947 |
|
|
948 |
"
|
|
949 |
self e
|
|
950 |
"
|
|
951 |
|
|
952 |
"Created: / 12-06-2017 / 18:29:36 / cg"
|
|
953 |
!
|
|
954 |
|
4392
|
955 |
fmax
|
|
956 |
"return the constant e as quad precision double.
|
4433
|
957 |
(returns approx. 200 bits of precision)"
|
4392
|
958 |
|
|
959 |
FMax isNil ifTrue:[
|
4395
|
960 |
FMax := self fromDoubleArray:(DoubleArray
|
|
961 |
with: 1.797693134862314E+308
|
|
962 |
with: 9.97920154767359795037e+291
|
|
963 |
with: 5.53956966280111259858e+275
|
|
964 |
with: 3.07507889307840487279e+259)
|
4392
|
965 |
].
|
|
966 |
^ FMax
|
|
967 |
|
|
968 |
"
|
|
969 |
Float fmax
|
|
970 |
self fmax
|
|
971 |
"
|
|
972 |
|
|
973 |
"Created: / 14-06-2017 / 19:14:18 / cg"
|
|
974 |
!
|
|
975 |
|
|
976 |
fmin
|
|
977 |
"return the smallest representable instance of this class"
|
|
978 |
|
|
979 |
FMin isNil ifTrue:[
|
4395
|
980 |
FMin := 1.6259745436952323e-260 asQDouble
|
4392
|
981 |
].
|
|
982 |
^ FMin
|
|
983 |
|
|
984 |
"
|
|
985 |
QDouble fmin
|
|
986 |
Float fmin
|
|
987 |
"
|
|
988 |
|
|
989 |
"Created: / 14-06-2017 / 19:14:49 / cg"
|
|
990 |
!
|
|
991 |
|
4411
|
992 |
infinity
|
|
993 |
^ Infinity positive
|
|
994 |
|
|
995 |
"Created: / 18-06-2017 / 23:41:07 / cg"
|
|
996 |
!
|
|
997 |
|
4412
|
998 |
invFact
|
4436
|
999 |
"table returning 1/n!!
|
|
1000 |
(for taylor series)"
|
4981
|
1001 |
|
4412
|
1002 |
InvFact isNil ifTrue:[
|
4981
|
1003 |
InvFact := Array new:15.
|
|
1004 |
InvFact at:1 put:(self d0:1.66666666666666657e-01 d1:9.25185853854297066e-18 d2:5.13581318503262866e-34 d3:2.85094902409834186e-50).
|
|
1005 |
InvFact at:2 put:(self d0:4.16666666666666644e-02 d1:2.31296463463574266e-18 d2:1.28395329625815716e-34 d3:7.12737256024585466e-51).
|
|
1006 |
InvFact at:3 put:(self d0:8.33333333333333322e-03 d1:1.15648231731787138e-19 d2:1.60494162032269652e-36 d3:2.22730392507682967e-53).
|
|
1007 |
InvFact at:4 put:(self d0:1.38888888888888894e-03 d1:-5.30054395437357706e-20 d2:-1.73868675534958776e-36 d3:-1.63335621172300840e-52).
|
|
1008 |
InvFact at:5 put:(self d0:1.98412698412698413e-04 d1:1.72095582934207053e-22 d2:1.49269123913941271e-40 d3:1.29470326746002471e-58).
|
|
1009 |
InvFact at:6 put:(self d0:2.48015873015873016e-05 d1:2.15119478667758816e-23 d2:1.86586404892426588e-41 d3:1.61837908432503088e-59).
|
|
1010 |
InvFact at:7 put:(self d0:2.75573192239858925e-06 d1:-1.85839327404647208e-22 d2:8.49175460488199287e-39 d3:-5.72661640789429621e-55).
|
|
1011 |
InvFact at:8 put:(self d0:2.75573192239858883e-07 d1:2.37677146222502973e-23 d2:-3.26318890334088294e-40 d3:1.61435111860404415e-56).
|
|
1012 |
InvFact at:9 put:(self d0:2.50521083854417202e-08 d1:-1.44881407093591197e-24 d2:2.04267351467144546e-41 d3:-8.49632672007163175e-58).
|
|
1013 |
InvFact at:10 put:(self d0:2.08767569878681002e-09 d1:-1.20734505911325997e-25 d2:1.70222792889287100e-42 d3:1.41609532150396700e-58).
|
|
1014 |
InvFact at:11 put:(self d0:1.60590438368216133e-10 d1:1.25852945887520981e-26 d2:-5.31334602762985031e-43 d3:3.54021472597605528e-59).
|
|
1015 |
InvFact at:12 put:(self d0:1.14707455977297245e-11 d1:2.06555127528307454e-28 d2:6.88907923246664603e-45 d3:5.72920002655109095e-61).
|
|
1016 |
InvFact at:13 put:(self d0:7.64716373181981641e-13 d1:7.03872877733453001e-30 d2:-7.82753927716258345e-48 d3:1.92138649443790242e-64).
|
|
1017 |
InvFact at:14 put:(self d0:4.77947733238738525e-14 d1:4.39920548583408126e-31 d2:-4.89221204822661465e-49 d3:1.20086655902368901e-65).
|
|
1018 |
InvFact at:15 put:(self d0:2.81145725434552060e-15 d1:1.65088427308614326e-31 d2:-2.87777179307447918e-50 d3:4.27110689256293549e-67).
|
4412
|
1019 |
].
|
|
1020 |
^ InvFact
|
|
1021 |
|
4436
|
1022 |
"
|
4981
|
1023 |
1.0 / (3 factorial) 0.166666666666667
|
|
1024 |
|
|
1025 |
1 asQDouble / (3 factorial) - (self invFact at:1)
|
|
1026 |
1 asQDouble / (4 factorial) - (self invFact at:2)
|
|
1027 |
1 asQDouble / (5 factorial) - (self invFact at:3)
|
|
1028 |
1 asQDouble / (6 factorial) - (self invFact at:4)
|
|
1029 |
1 asQDouble / (7 factorial) - (self invFact at:5)
|
|
1030 |
1 asQDouble / (8 factorial) - (self invFact at:6)
|
|
1031 |
1 asQDouble / (9 factorial) - (self invFact at:7)
|
|
1032 |
1 asQDouble / (10 factorial) - (self invFact at:8)
|
|
1033 |
1 asQDouble / (11 factorial) - (self invFact at:9)
|
|
1034 |
1 asQDouble / (12 factorial) - (self invFact at:10)
|
|
1035 |
1 asQDouble / (13 factorial) - (self invFact at:11)
|
|
1036 |
1 asQDouble / (14 factorial) - (self invFact at:12)
|
|
1037 |
1 asQDouble / (15 factorial) - (self invFact at:13)
|
|
1038 |
1 asQDouble / (16 factorial) - (self invFact at:14)
|
|
1039 |
1 asQDouble / (17 factorial) - (self invFact at:15)
|
4436
|
1040 |
"
|
|
1041 |
|
4412
|
1042 |
"Created: / 19-06-2017 / 02:22:23 / cg"
|
4436
|
1043 |
"Modified (comment): / 20-06-2017 / 13:04:54 / cg"
|
4412
|
1044 |
!
|
|
1045 |
|
4380
|
1046 |
ln10
|
|
1047 |
"return the constant e as quad precision double.
|
4433
|
1048 |
(returns approx. 200 bits of precision)"
|
4380
|
1049 |
|
|
1050 |
Ln10 isNil ifTrue:[
|
4388
|
1051 |
Ln10 := self fromDoubleArray:(DoubleArray
|
|
1052 |
with: 2.302585092994045901e+00
|
|
1053 |
with: -2.170756223382249351e-16
|
|
1054 |
with: -9.984262454465776570e-33
|
|
1055 |
with: -4.023357454450206379e-49)
|
|
1056 |
].
|
4380
|
1057 |
^ Ln10
|
|
1058 |
|
|
1059 |
"
|
|
1060 |
self ln10
|
|
1061 |
"
|
|
1062 |
|
|
1063 |
"Created: / 12-06-2017 / 18:32:29 / cg"
|
|
1064 |
!
|
|
1065 |
|
|
1066 |
ln2
|
|
1067 |
"return the constant e as quad precision double.
|
4433
|
1068 |
(returns approx. 200 bits of precision)"
|
4380
|
1069 |
|
|
1070 |
Ln2 isNil ifTrue:[
|
4388
|
1071 |
Ln2 := self fromDoubleArray:(DoubleArray
|
|
1072 |
with: 6.931471805599452862e-01
|
|
1073 |
with: 2.319046813846299558e-17
|
|
1074 |
with: 5.707708438416212066e-34
|
|
1075 |
with: -3.582432210601811423e-50)
|
|
1076 |
].
|
4380
|
1077 |
^ Ln2
|
|
1078 |
|
|
1079 |
"
|
|
1080 |
self ln2
|
|
1081 |
"
|
|
1082 |
|
|
1083 |
"Created: / 12-06-2017 / 18:31:34 / cg"
|
|
1084 |
!
|
|
1085 |
|
4411
|
1086 |
negativeInfinity
|
|
1087 |
^ Infinity negative
|
|
1088 |
|
|
1089 |
"Created: / 18-06-2017 / 23:40:47 / cg"
|
|
1090 |
!
|
|
1091 |
|
4380
|
1092 |
pi
|
|
1093 |
"return the constant pi as quad precision double.
|
4433
|
1094 |
(returns approx. 200 bits of precision)"
|
4380
|
1095 |
|
|
1096 |
Pi isNil ifTrue:[
|
4388
|
1097 |
Pi := self fromDoubleArray:(DoubleArray
|
|
1098 |
with: 3.141592653589793116e+00
|
|
1099 |
with: 1.224646799147353207e-16
|
|
1100 |
with: -2.994769809718339666e-33
|
|
1101 |
with: 1.112454220863365282e-49)
|
|
1102 |
].
|
4380
|
1103 |
^ Pi
|
|
1104 |
|
|
1105 |
"
|
|
1106 |
self pi
|
|
1107 |
"
|
|
1108 |
|
|
1109 |
"Created: / 12-06-2017 / 18:27:13 / cg"
|
4395
|
1110 |
!
|
|
1111 |
|
|
1112 |
unity
|
|
1113 |
"return the neutral element for multiplication (1.0) as QDouble"
|
|
1114 |
|
|
1115 |
QDoubleOne isNil ifTrue:[
|
|
1116 |
QDoubleOne := 1.0 asQDouble.
|
|
1117 |
].
|
|
1118 |
^ QDoubleOne
|
|
1119 |
|
|
1120 |
"
|
|
1121 |
self unity
|
|
1122 |
"
|
|
1123 |
|
|
1124 |
"Created: / 15-06-2017 / 11:45:22 / cg"
|
|
1125 |
!
|
|
1126 |
|
|
1127 |
zero
|
|
1128 |
"return the neutral element for addition (0.0) as QDouble"
|
|
1129 |
|
|
1130 |
QDoubleZero isNil ifTrue:[
|
|
1131 |
QDoubleZero := 0.0 asQDouble
|
|
1132 |
].
|
|
1133 |
^ QDoubleZero
|
|
1134 |
|
|
1135 |
"
|
|
1136 |
self zero
|
|
1137 |
"
|
|
1138 |
|
|
1139 |
"Created: / 15-06-2017 / 11:44:13 / cg"
|
4380
|
1140 |
! !
|
|
1141 |
|
|
1142 |
!QDouble class methodsFor:'queries'!
|
|
1143 |
|
4395
|
1144 |
defaultPrintPrecision
|
|
1145 |
"return the number of decimal digits printed by default"
|
|
1146 |
|
4440
|
1147 |
^ 20
|
4395
|
1148 |
|
|
1149 |
"
|
|
1150 |
ShortFloat defaultPrintPrecision
|
|
1151 |
Float defaultPrintPrecision
|
|
1152 |
LongFloat defaultPrintPrecision
|
|
1153 |
QDouble defaultPrintPrecision
|
|
1154 |
"
|
|
1155 |
|
|
1156 |
"Created: / 17-06-2017 / 02:58:51 / cg"
|
4440
|
1157 |
"Modified: / 21-06-2017 / 13:39:08 / cg"
|
4395
|
1158 |
!
|
|
1159 |
|
4380
|
1160 |
epsilon
|
|
1161 |
"return the maximum relative spacing of instances of mySelf
|
|
1162 |
(i.e. the value-delta of the least significant bit)"
|
|
1163 |
|
4443
|
1164 |
^ 1.2154326714572500565324311366323150942261000827598106963711353e-64
|
4380
|
1165 |
|
|
1166 |
"
|
4440
|
1167 |
Float epsilon -> 2.22044604925031E-16
|
|
1168 |
ShortFloat epsilon -> 1.19209289550781E-07
|
|
1169 |
LongFloat epsilon -> 1.0842021724855E-19
|
|
1170 |
QDouble epsilon -> 1.21543267145725E-63
|
4380
|
1171 |
"
|
|
1172 |
|
|
1173 |
"Created: / 12-06-2017 / 18:52:44 / cg"
|
4443
|
1174 |
"Modified: / 22-06-2017 / 15:34:56 / cg"
|
4380
|
1175 |
!
|
|
1176 |
|
|
1177 |
numBitsInExponent
|
|
1178 |
"answer the number of bits in the exponent"
|
|
1179 |
|
|
1180 |
^ Float numBitsInExponent
|
|
1181 |
|
|
1182 |
"
|
4963
|
1183 |
1.0 asQDouble numBitsInExponent
|
4380
|
1184 |
"
|
|
1185 |
|
|
1186 |
"Created: / 12-06-2017 / 11:11:04 / cg"
|
4963
|
1187 |
"Modified (comment): / 28-05-2019 / 08:55:04 / Claus Gittinger"
|
4380
|
1188 |
!
|
|
1189 |
|
|
1190 |
numBitsInMantissa
|
4430
|
1191 |
"answer the number of bits in the mantissa.
|
|
1192 |
Here, a fake number is returned"
|
|
1193 |
|
|
1194 |
^ (Float numBitsInMantissa - 1) * 4
|
4395
|
1195 |
|
4380
|
1196 |
"
|
4963
|
1197 |
1.0 asFloat numBitsInMantissa
|
|
1198 |
1.0 asShortFloat numBitsInMantissa
|
|
1199 |
1.0 asLongFloat numBitsInMantissa
|
|
1200 |
1.0 asQDouble numBitsInMantissa
|
4380
|
1201 |
1.0 asQDouble class numBitsInMantissa
|
|
1202 |
|
|
1203 |
Float numBitsInMantissa
|
|
1204 |
ShortFloat numBitsInMantissa
|
|
1205 |
QDouble numBitsInMantissa
|
|
1206 |
"
|
|
1207 |
|
|
1208 |
"Created: / 12-06-2017 / 11:13:44 / cg"
|
4430
|
1209 |
"Modified (comment): / 20-06-2017 / 11:05:26 / cg"
|
4963
|
1210 |
"Modified (comment): / 28-05-2019 / 09:07:07 / Claus Gittinger"
|
4380
|
1211 |
!
|
|
1212 |
|
|
1213 |
precision
|
|
1214 |
"answer the number of bits in the mantissa"
|
|
1215 |
|
4431
|
1216 |
"/ subtract some due to overlap in the component numbers
|
|
1217 |
^ (Float precision - 2) * 4
|
4395
|
1218 |
|
4380
|
1219 |
"
|
|
1220 |
1.0 class numBitsInMantissa
|
|
1221 |
1.0 asShortFloat class numBitsInMantissa
|
|
1222 |
1.0 asLongFloat class numBitsInMantissa
|
|
1223 |
1.0 asQDouble class numBitsInMantissa
|
|
1224 |
|
|
1225 |
Float numBitsInMantissa
|
|
1226 |
ShortFloat numBitsInMantissa
|
|
1227 |
QDouble numBitsInMantissa
|
|
1228 |
QDouble precision
|
|
1229 |
"
|
|
1230 |
|
|
1231 |
"Created: / 12-06-2017 / 18:49:11 / cg"
|
4431
|
1232 |
"Modified (comment): / 20-06-2017 / 12:59:00 / cg"
|
4380
|
1233 |
!
|
|
1234 |
|
|
1235 |
radix
|
|
1236 |
"answer the radix of a Floats exponent
|
|
1237 |
This is an IEEE float, which is represented as binary"
|
|
1238 |
|
|
1239 |
^ Float radix
|
|
1240 |
|
|
1241 |
"Created: / 12-06-2017 / 18:50:04 / cg"
|
|
1242 |
! !
|
|
1243 |
|
|
1244 |
!QDouble methodsFor:'arithmetic'!
|
|
1245 |
|
|
1246 |
* aNumber
|
|
1247 |
"return the product of the receiver and the argument, aNumber"
|
|
1248 |
|
|
1249 |
^ aNumber productFromQDouble:self
|
|
1250 |
|
|
1251 |
"
|
4392
|
1252 |
(((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) * (QDouble fromFloat:2.0)) asDoubleArray
|
4380
|
1253 |
"
|
|
1254 |
|
|
1255 |
"Created: / 13-06-2017 / 01:00:47 / cg"
|
4392
|
1256 |
"Modified (comment): / 14-06-2017 / 12:08:50 / cg"
|
4380
|
1257 |
!
|
|
1258 |
|
|
1259 |
+ aNumber
|
|
1260 |
"return the sum of the receiver and the argument, aNumber"
|
|
1261 |
|
|
1262 |
^ aNumber sumFromQDouble:self
|
|
1263 |
|
|
1264 |
"
|
|
1265 |
((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) asDoubleArray
|
|
1266 |
"
|
|
1267 |
|
|
1268 |
"Created: / 12-06-2017 / 16:17:46 / cg"
|
|
1269 |
"Modified: / 12-06-2017 / 23:06:22 / cg"
|
|
1270 |
!
|
|
1271 |
|
|
1272 |
- aNumber
|
|
1273 |
"return the sum of the receiver and the argument, aNumber"
|
|
1274 |
|
4393
|
1275 |
^ self + (aNumber negated)
|
4380
|
1276 |
|
|
1277 |
"
|
|
1278 |
(QDouble fromFloat:1e20) asDoubleArray
|
|
1279 |
((QDouble fromFloat:1e20) - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1280 |
(QDouble fromFloat:1e-20) asDoubleArray
|
|
1281 |
((QDouble fromFloat:1e-20) - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1282 |
((QDouble fromFloat:2.0) - (QDouble fromFloat:1.0)) asDoubleArray
|
4395
|
1283 |
|
4393
|
1284 |
((QDouble fromFloat:2.0) - (QDouble fromFloat:1.0) + (QDouble fromFloat:1.0)) asDoubleArray
|
|
1285 |
((QDouble fromFloat:1e-20) - (QDouble fromFloat:1.0) + (QDouble fromFloat:1.0)) asDoubleArray
|
4380
|
1286 |
"
|
|
1287 |
|
|
1288 |
"Created: / 12-06-2017 / 23:41:39 / cg"
|
4393
|
1289 |
"Modified (comment): / 15-06-2017 / 00:34:41 / cg"
|
4386
|
1290 |
!
|
|
1291 |
|
|
1292 |
/ aNumber
|
|
1293 |
"return the quotient of the receiver and the argument, aNumber"
|
|
1294 |
|
|
1295 |
^ aNumber quotientFromQDouble:self
|
|
1296 |
|
|
1297 |
"
|
|
1298 |
((QDouble fromFloat:1e20) / (QDouble fromFloat:2.0)) asDoubleArray
|
4395
|
1299 |
|
4393
|
1300 |
((QDouble fromFloat:1.2345) / (QDouble fromFloat:10.0)) asDoubleArray
|
|
1301 |
((QDouble fromFloat:1.2345) / 10.0) asDoubleArray
|
|
1302 |
|
4386
|
1303 |
"
|
|
1304 |
|
|
1305 |
"Created: / 13-06-2017 / 17:59:09 / cg"
|
4393
|
1306 |
"Modified (comment): / 15-06-2017 / 00:14:26 / cg"
|
4380
|
1307 |
! !
|
|
1308 |
|
|
1309 |
!QDouble methodsFor:'coercing & converting'!
|
|
1310 |
|
|
1311 |
asDoubleArray
|
4395
|
1312 |
^ DoubleArray
|
|
1313 |
with:self d0
|
|
1314 |
with:self d1
|
|
1315 |
with:self d2
|
|
1316 |
with:self d3.
|
4380
|
1317 |
|
|
1318 |
"
|
4386
|
1319 |
(QDouble fromFloat:1.0) asDoubleArray
|
|
1320 |
(QDouble fromFloat:2.0) asDoubleArray
|
4380
|
1321 |
"
|
|
1322 |
|
|
1323 |
"Created: / 12-06-2017 / 18:19:19 / cg"
|
4386
|
1324 |
"Modified (comment): / 13-06-2017 / 17:58:09 / cg"
|
4380
|
1325 |
!
|
|
1326 |
|
|
1327 |
asFloat
|
4386
|
1328 |
^ self d0
|
4380
|
1329 |
|
|
1330 |
"
|
|
1331 |
(QDouble fromFloat:1.0) asFloat
|
|
1332 |
(QDouble fromFloat:2.0) asFloat
|
|
1333 |
"
|
|
1334 |
|
|
1335 |
"Created: / 12-06-2017 / 18:15:27 / cg"
|
4386
|
1336 |
"Modified: / 13-06-2017 / 17:56:50 / cg"
|
4380
|
1337 |
!
|
|
1338 |
|
4421
|
1339 |
asInteger
|
|
1340 |
^ self d0 asInteger
|
4981
|
1341 |
+ self d1 asInteger
|
|
1342 |
+ self d2 asInteger
|
4421
|
1343 |
+ self d3 asInteger
|
|
1344 |
|
|
1345 |
"Created: / 19-06-2017 / 18:07:17 / cg"
|
|
1346 |
!
|
|
1347 |
|
4395
|
1348 |
asQDouble
|
|
1349 |
"return a QDouble with same value as myself."
|
|
1350 |
|
|
1351 |
^ self
|
|
1352 |
|
|
1353 |
"Created: / 15-06-2017 / 12:08:02 / cg"
|
|
1354 |
!
|
|
1355 |
|
4430
|
1356 |
asTrueFraction
|
|
1357 |
self halt.
|
|
1358 |
^ self d0 asTrueFraction
|
|
1359 |
+ self d1 asTrueFraction
|
|
1360 |
+ self d2 asTrueFraction
|
|
1361 |
+ self d3 asTrueFraction
|
|
1362 |
|
|
1363 |
"
|
|
1364 |
1e10 asTrueFraction -> 10000000000
|
|
1365 |
1e20 asTrueFraction -> 100000000000000000000
|
|
1366 |
(1e20 + 1) asTrueFraction -> 100000000000000000000 ouch!!
|
|
1367 |
|
|
1368 |
1e10 asQDouble asTrueFraction -> 10000000000
|
|
1369 |
1e20 asQDouble asTrueFraction -> 100000000000000000000
|
|
1370 |
(1e20 asQDouble + 1) asTrueFraction -> 100000000000000000001
|
|
1371 |
|
4981
|
1372 |
(1e40 asQDouble + 1e20 + 1) asTrueFraction -> 10000000000000000303886028427003666890753
|
|
1373 |
(1e40 asQDouble + 1e20) asTrueFraction
|
4430
|
1374 |
"
|
|
1375 |
|
|
1376 |
"Created: / 20-06-2017 / 11:09:03 / cg"
|
|
1377 |
!
|
|
1378 |
|
4380
|
1379 |
coerce:aNumber
|
|
1380 |
"convert the argument aNumber into an instance of the receiver's class and return it."
|
|
1381 |
|
|
1382 |
^ aNumber asQDouble
|
|
1383 |
|
|
1384 |
"Created: / 12-06-2017 / 17:13:47 / cg"
|
|
1385 |
"Modified: / 12-06-2017 / 21:09:06 / cg"
|
|
1386 |
!
|
|
1387 |
|
4430
|
1388 |
exponent
|
|
1389 |
^ self d0 exponent
|
|
1390 |
|
|
1391 |
"Created: / 20-06-2017 / 11:06:02 / cg"
|
|
1392 |
!
|
|
1393 |
|
4380
|
1394 |
generality
|
|
1395 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
1396 |
|
|
1397 |
^ 95
|
|
1398 |
|
|
1399 |
"Created: / 12-06-2017 / 17:13:14 / cg"
|
|
1400 |
!
|
|
1401 |
|
|
1402 |
negative
|
4386
|
1403 |
^ self d0 negative
|
4380
|
1404 |
|
|
1405 |
"
|
|
1406 |
(QDouble fromFloat:0.0) negative
|
|
1407 |
(QDouble fromFloat:1.0) negative
|
|
1408 |
(QDouble fromFloat:-1.0) negative
|
|
1409 |
"
|
|
1410 |
|
|
1411 |
"Created: / 13-06-2017 / 01:57:39 / cg"
|
4386
|
1412 |
"Modified: / 13-06-2017 / 17:58:26 / cg"
|
4380
|
1413 |
!
|
|
1414 |
|
|
1415 |
positive
|
4962
|
1416 |
"return true, if the receiver is greater or equal to zero (not negative)"
|
|
1417 |
|
4386
|
1418 |
^ self d0 positive
|
4380
|
1419 |
|
|
1420 |
"
|
|
1421 |
(QDouble fromFloat:1.0) positive
|
|
1422 |
(QDouble fromFloat:-1.0) positive
|
|
1423 |
"
|
|
1424 |
|
|
1425 |
"Created: / 13-06-2017 / 01:56:53 / cg"
|
4386
|
1426 |
"Modified: / 13-06-2017 / 17:58:41 / cg"
|
4962
|
1427 |
"Modified (comment): / 28-05-2019 / 05:55:55 / Claus Gittinger"
|
4380
|
1428 |
! !
|
|
1429 |
|
4385
|
1430 |
!QDouble methodsFor:'comparing'!
|
|
1431 |
|
|
1432 |
< aNumber
|
|
1433 |
"return true, if the argument, aNumber is greater than the receiver"
|
|
1434 |
|
|
1435 |
^ aNumber lessFromQDouble:self
|
|
1436 |
|
|
1437 |
"Created: / 13-06-2017 / 16:58:53 / cg"
|
|
1438 |
!
|
|
1439 |
|
|
1440 |
= aNumber
|
|
1441 |
"return true, if the argument, aNumber has the same value as than the receiver"
|
|
1442 |
|
|
1443 |
^ aNumber equalFromQDouble:self
|
|
1444 |
|
|
1445 |
"Created: / 13-06-2017 / 17:12:09 / cg"
|
|
1446 |
! !
|
|
1447 |
|
4380
|
1448 |
!QDouble methodsFor:'double dispatching'!
|
|
1449 |
|
|
1450 |
differenceFromFloat:aFloat
|
|
1451 |
"aFloat - self"
|
4395
|
1452 |
|
4380
|
1453 |
^ aFloat + (self negated)
|
|
1454 |
|
|
1455 |
"
|
|
1456 |
1.0 - (QDouble fromFloat:1.0)
|
|
1457 |
1e20 - (QDouble fromFloat:1.0)
|
|
1458 |
(1.0 - (QDouble fromFloat:1.0)) asFloat
|
|
1459 |
(1e20 - (QDouble fromFloat:1.0)) asFloat
|
|
1460 |
|
|
1461 |
(1.0 - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1462 |
(1e20 - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1463 |
(1e20 - (QDouble fromFloat:1.0) + 1e-20) asDoubleArray
|
|
1464 |
"
|
|
1465 |
|
|
1466 |
"Created: / 12-06-2017 / 23:38:05 / cg"
|
|
1467 |
!
|
|
1468 |
|
|
1469 |
differenceFromQDouble:aQDouble
|
|
1470 |
"aQDouble - self"
|
4395
|
1471 |
|
4380
|
1472 |
^ aQDouble + (self negated)
|
|
1473 |
|
|
1474 |
"
|
|
1475 |
1.0 - (QDouble fromFloat:1.0)
|
|
1476 |
1e20 - (QDouble fromFloat:1.0)
|
|
1477 |
(1.0 - (QDouble fromFloat:1.0)) asFloat
|
|
1478 |
(1e20 - (QDouble fromFloat:1.0)) asFloat
|
|
1479 |
|
|
1480 |
(1.0 - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1481 |
(1e20 - (QDouble fromFloat:1.0)) asDoubleArray
|
|
1482 |
(1e20 - (QDouble fromFloat:1.0) + 1e-20) asDoubleArray
|
|
1483 |
"
|
|
1484 |
|
|
1485 |
"Created: / 12-06-2017 / 23:38:19 / cg"
|
|
1486 |
!
|
|
1487 |
|
|
1488 |
equalFromQDouble:aQDouble
|
|
1489 |
%{
|
|
1490 |
if (__Class(aQDouble) == QDouble) {
|
4395
|
1491 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1492 |
double *b = __QuadDoubleInstPtr(aQDouble)->d_quadDoubleValue;
|
|
1493 |
|
|
1494 |
RETURN ((a[0] == b[0]
|
|
1495 |
&& a[1] == b[1]
|
|
1496 |
&& a[2] == b[2]
|
|
1497 |
&& a[3] == b[3]) ? true : false);
|
4380
|
1498 |
}
|
|
1499 |
%}.
|
4386
|
1500 |
^ (aQDouble d0 = self d0)
|
|
1501 |
and:[ (aQDouble d1 = self d1)
|
|
1502 |
and:[ (aQDouble d2 = self d2)
|
|
1503 |
and:[ (aQDouble d3 = self d3) ]]]
|
4395
|
1504 |
|
4380
|
1505 |
"
|
|
1506 |
(QDouble fromFloat:1.0) = (QDouble fromFloat:1.0)
|
|
1507 |
(QDouble fromFloat:1.0) = 1.0
|
|
1508 |
1.0 = (QDouble fromFloat:1.0)
|
|
1509 |
"
|
|
1510 |
|
|
1511 |
"Created: / 13-06-2017 / 03:01:19 / cg"
|
4386
|
1512 |
"Modified: / 13-06-2017 / 18:01:52 / cg"
|
4380
|
1513 |
!
|
|
1514 |
|
4385
|
1515 |
lessFromQDouble:aQDouble
|
|
1516 |
"sent when aQDouble does not know how to compare to the receiver..
|
|
1517 |
Return true if aQDouble < self"
|
4380
|
1518 |
|
4385
|
1519 |
%{
|
|
1520 |
if (__Class(aQDouble) == QDouble) {
|
4395
|
1521 |
double *a = __QuadDoubleInstPtr(aQDouble)->d_quadDoubleValue;
|
|
1522 |
double *b = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1523 |
|
|
1524 |
// now compare if a < b!
|
|
1525 |
RETURN
|
|
1526 |
((a[0] < b[0] ||
|
|
1527 |
(a[0] == b[0] && (a[1] < b[1] ||
|
|
1528 |
(a[1] == b[1] && (a[2] < b[2] ||
|
|
1529 |
(a[2] == b[2] && a[3] < b[3])))))) ? true : false);
|
4380
|
1530 |
}
|
|
1531 |
%}.
|
4385
|
1532 |
^ super lessFromQDouble:aQDouble
|
4380
|
1533 |
|
|
1534 |
"
|
4385
|
1535 |
(1.0 + 1e-40) > 1.0
|
|
1536 |
((QDouble fromFloat:1.0) + (QDouble fromFloat:1e-40)) > (QDouble fromFloat:1.0)
|
4380
|
1537 |
|
4385
|
1538 |
(QDouble fromFloat:1.0) > (QDouble fromFloat:1.0)
|
|
1539 |
(QDouble fromFloat:1.1) > (QDouble fromFloat:1.0)
|
|
1540 |
(QDouble fromFloat:1.0) > 1.0
|
|
1541 |
(QDouble fromFloat:1.1) > 1.0
|
|
1542 |
1.0 > (QDouble fromFloat:1.0)
|
|
1543 |
"
|
|
1544 |
|
|
1545 |
"Created: / 13-06-2017 / 17:07:47 / cg"
|
4380
|
1546 |
!
|
|
1547 |
|
|
1548 |
productFromFloat:aFloat
|
|
1549 |
%{
|
|
1550 |
if (__isFloatLike(aFloat)) {
|
4981
|
1551 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1552 |
double b = __floatVal(aFloat);
|
|
1553 |
double p0, p1, p2, p3;
|
|
1554 |
double q0, q1, q2;
|
|
1555 |
double s0, s1, s2, s3, s4;
|
|
1556 |
OBJ newQD;
|
|
1557 |
int savedCV;
|
|
1558 |
|
|
1559 |
fpu_fix_start(&savedCV);
|
|
1560 |
|
|
1561 |
m_two_prod(p0, a[0], b, q0);
|
|
1562 |
m_two_prod(p1, a[1], b, q1);
|
|
1563 |
m_two_prod(p2, a[2], b, q2);
|
|
1564 |
p3 = a[3] * b;
|
|
1565 |
|
|
1566 |
s0 = p0;
|
|
1567 |
|
|
1568 |
m_two_sum(s1, q0, p1, s2);
|
|
1569 |
|
|
1570 |
m_three_sum(s2, q1, p2);
|
|
1571 |
|
|
1572 |
m_three_sum2(q1, q2, p3);
|
|
1573 |
s3 = q1;
|
|
1574 |
|
|
1575 |
s4 = q2 + p2;
|
|
1576 |
|
|
1577 |
m_renorm5(s0, s1, s2, s3, s4);
|
|
1578 |
fpu_fix_end(&savedCV);
|
|
1579 |
|
|
1580 |
__qNew_qdReal(newQD, s0, s1, s2, s3);
|
|
1581 |
RETURN( newQD );
|
4380
|
1582 |
}
|
|
1583 |
%}.
|
|
1584 |
^ super productFromFloat:aFloat.
|
|
1585 |
|
|
1586 |
"
|
4421
|
1587 |
loosing bits here:
|
4981
|
1588 |
|
4421
|
1589 |
(1e20+1.0)*2.0
|
|
1590 |
(1e20+1.0)*1e20
|
|
1591 |
(1e40+1.0)*2.0
|
|
1592 |
|
|
1593 |
but not here:
|
|
1594 |
|
|
1595 |
(1.0 asQDouble) * 2.0
|
4981
|
1596 |
((1e20 asQDouble) + (1.0)) * 2.0
|
4421
|
1597 |
((1e20 asQDouble) + (1.0)) * 100.0 10000000000000000000100.0
|
4981
|
1598 |
((1e20 asQDouble) + (1.0)) * 1000.0
|
|
1599 |
((1e40 asQDouble) + (1.0)) * 2.0
|
4387
|
1600 |
|
4380
|
1601 |
2.0 * (QDouble fromFloat:1.0)
|
4387
|
1602 |
2.0 * (QDouble fromFloat:3.0)
|
4421
|
1603 |
(QDouble fromFloat:2.0) * (QDouble fromFloat:3.0)
|
4981
|
1604 |
|
4421
|
1605 |
QDouble ln2 DoubleArray(0.693147180559945 2.3190468138463E-17 5.70770843841621E-34 -3.58243221060181E-50)
|
|
1606 |
2.0 * QDouble ln2 DoubleArray(1.38629436111989 4.6380936276926E-17 1.14154168768324E-33 -7.16486442120362E-50)
|
4981
|
1607 |
QDouble ln2 * 2.0
|
|
1608 |
|
4421
|
1609 |
2.0 * ((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) DoubleArray(2E+20 2.0 0.0 0.0)
|
|
1610 |
((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) * 2.0 DoubleArray(2E+20 4E+20 0.0 0.0)
|
|
1611 |
((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) * (QDouble fromFloat:2.0) DoubleArray(2E+20 4E+20 0.0 0.0)
|
|
1612 |
(QDouble fromFloat:2.0) * ((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) DoubleArray(2E+20 4E+20 0.0 0.0)
|
4981
|
1613 |
|
4387
|
1614 |
(2.0 * ((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0))) - (QDouble fromFloat:1e20) - (QDouble fromFloat:1e20)
|
|
1615 |
|
4380
|
1616 |
(2.0 * (QDouble fromFloat:1.0)) asFloat
|
|
1617 |
(1e20 * (QDouble fromFloat:1.0)) asFloat
|
|
1618 |
|
|
1619 |
(1e20 * (QDouble fromFloat:1.0) * 1e-20) asDoubleArray
|
|
1620 |
"
|
|
1621 |
|
|
1622 |
"Created: / 13-06-2017 / 00:58:56 / cg"
|
4421
|
1623 |
"Modified: / 19-06-2017 / 16:48:18 / cg"
|
|
1624 |
"Modified (comment): / 19-06-2017 / 18:11:43 / cg"
|
4380
|
1625 |
!
|
|
1626 |
|
|
1627 |
productFromQDouble:aQDouble
|
|
1628 |
%{
|
|
1629 |
if (__Class(aQDouble) == QDouble) {
|
4981
|
1630 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1631 |
double *b = __QuadDoubleInstPtr(aQDouble)->d_quadDoubleValue;
|
|
1632 |
OBJ newQD;
|
|
1633 |
int savedCV;
|
4454
|
1634 |
|
|
1635 |
#define QD_IEEE_MUL
|
|
1636 |
|
|
1637 |
#ifndef QD_IEEE_MUL
|
4981
|
1638 |
// sloppy
|
|
1639 |
double p0, p1, p2, p3, p4, p5;
|
|
1640 |
double q0, q1, q2, q3, q4, q5;
|
|
1641 |
double t0, t1;
|
|
1642 |
double s0, s1, s2;
|
|
1643 |
|
|
1644 |
fpu_fix_start(&savedCV);
|
|
1645 |
|
|
1646 |
m_two_prod(p0, a[0], b[0], q0);
|
|
1647 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[0], b[0], p0, q0);
|
|
1648 |
|
|
1649 |
m_two_prod(p1, a[0], b[1], q1);
|
|
1650 |
m_two_prod(p2, a[1], b[0], q2);
|
|
1651 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[0], b[1], p1, q1);
|
|
1652 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[1], b[0], p2, q2);
|
|
1653 |
|
|
1654 |
m_two_prod(p3, a[0], b[2], q3);
|
|
1655 |
m_two_prod(p4, a[1], b[1], q4);
|
|
1656 |
m_two_prod(p5, a[2], b[0], q5);
|
|
1657 |
|
|
1658 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[0], b[2], p3, q3);
|
|
1659 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[1], b[1], p4, q4);
|
|
1660 |
// fprintf(stderr, "%f * %f -> %f, %f\n", a[2], b[0], p5, q5);
|
|
1661 |
|
|
1662 |
/* Start Accumulation */
|
|
1663 |
m_three_sum(p1, p2, q0);
|
|
1664 |
// fprintf(stderr, "mul7: after three_sum: p1:%e p2:%e q0:%e\n", p1, p2, q0);
|
|
1665 |
|
|
1666 |
/* Six-Three Sum of p2, q1, q2, p3, p4, p5. */
|
|
1667 |
m_three_sum(p2, q1, q2);
|
|
1668 |
// fprintf(stderr, "mul8: after three_sum: p2:%e q1:%e q2:%e\n", p2, q1, q2);
|
|
1669 |
m_three_sum(p3, p4, p5);
|
|
1670 |
// fprintf(stderr, "mul9: after three_sum: p3:%e p4:%e p5:%e\n", p3, p4, p5);
|
|
1671 |
|
|
1672 |
/* compute (s0, s1, s2) = (p2, q1, q2) + (p3, p4, p5). */
|
|
1673 |
m_two_sum(s0, p2, p3, t0);
|
|
1674 |
// fprintf(stderr, "mul10: after two_sum: s0:%e p2:%e p3:%e t0:%e\n", s0, p2, p3, t0);
|
|
1675 |
m_two_sum(s1, q1, p4, t1);
|
|
1676 |
// fprintf(stderr, "mul11: after two_sum: s1:%e q1:%e p4:%e t1:%e\n", s1, q1, p4, t1);
|
|
1677 |
s2 = q2 + p5;
|
|
1678 |
m_two_sum(s1, s1, t0, t0);
|
|
1679 |
// fprintf(stderr, "mul12: after two_sum: s1:%e s1:%e t0:%e t0:%e\n", s1, s1, t0, t0);
|
|
1680 |
s2 += (t0 + t1);
|
|
1681 |
|
|
1682 |
/* O(eps^3) order terms */
|
|
1683 |
s1 += a[0]*b[3] + a[1]*b[2] + a[2]*b[1] + a[3]*b[0] + q0 + q3 + q4 + q5;
|
|
1684 |
|
|
1685 |
// fprintf(stderr, "before renorm5: p0:%e p2:%e s0:%e s1:%e s2:%e\n", p0, p1, s0, s1, s2);
|
|
1686 |
m_renorm5(p0, p1, s0, s1, s2);
|
|
1687 |
// fprintf(stderr, "after renorm5: p0:%e p2:%e s0:%e s1:%e s2:%e\n", p0, p1, s0, s1, s2);
|
|
1688 |
fpu_fix_end(&savedCV);
|
|
1689 |
|
|
1690 |
__qNew_qdReal(newQD, p0, p1, s0, s1);
|
4454
|
1691 |
#else
|
4981
|
1692 |
// accurate
|
|
1693 |
double p0, p1, p2, p3, p4, p5;
|
|
1694 |
double q0, q1, q2, q3, q4, q5;
|
|
1695 |
double p6, p7, p8, p9;
|
|
1696 |
double q6, q7, q8, q9;
|
|
1697 |
double r0, r1;
|
|
1698 |
double t0, t1;
|
|
1699 |
double s0, s1, s2;
|
|
1700 |
|
|
1701 |
fpu_fix_start(&savedCV);
|
|
1702 |
|
|
1703 |
m_two_prod(p0, a[0], b[0], q0);
|
|
1704 |
|
|
1705 |
m_two_prod(p1, a[0], b[1], q1);
|
|
1706 |
m_two_prod(p2, a[1], b[0], q2);
|
|
1707 |
|
|
1708 |
m_two_prod(p3, a[0], b[2], q3);
|
|
1709 |
m_two_prod(p4, a[1], b[1], q4);
|
|
1710 |
m_two_prod(p5, a[2], b[0], q5);
|
|
1711 |
|
|
1712 |
/* Start Accumulation */
|
|
1713 |
m_three_sum(p1, p2, q0);
|
|
1714 |
|
|
1715 |
/* Six-Three Sum of p2, q1, q2, p3, p4, p5. */
|
|
1716 |
m_three_sum(p2, q1, q2);
|
|
1717 |
m_three_sum(p3, p4, p5);
|
|
1718 |
/* compute (s0, s1, s2) = (p2, q1, q2) + (p3, p4, p5). */
|
|
1719 |
m_two_sum(s0, p2, p3, t0);
|
|
1720 |
m_two_sum(s1, q1, p4, t1);
|
|
1721 |
s2 = q2 + p5;
|
|
1722 |
m_two_sum(s1, s1, t0, t0);
|
|
1723 |
s2 += (t0 + t1);
|
|
1724 |
|
|
1725 |
/* O(eps^3) order terms */
|
|
1726 |
m_two_prod(p6, a[0], b[3], q6);
|
|
1727 |
m_two_prod(p7, a[1], b[2], q7);
|
|
1728 |
m_two_prod(p8, a[2], b[1], q8);
|
|
1729 |
m_two_prod(p9, a[3], b[0], q9);
|
|
1730 |
|
|
1731 |
/* Nine-Two-Sum of q0, s1, q3, q4, q5, p6, p7, p8, p9. */
|
|
1732 |
m_two_sum(q0, q0, q3, q3);
|
|
1733 |
m_two_sum(q4, q4, q5, q5);
|
|
1734 |
m_two_sum(p6, p6, p7, p7);
|
|
1735 |
m_two_sum(p8, p8, p9, p9);
|
|
1736 |
/* Compute (t0, t1) = (q0, q3) + (q4, q5). */
|
|
1737 |
m_two_sum(t0, q0, q4, t1);
|
|
1738 |
t1 += (q3 + q5);
|
|
1739 |
/* Compute (r0, r1) = (p6, p7) + (p8, p9). */
|
|
1740 |
m_two_sum(r0, p6, p8, r1);
|
|
1741 |
r1 += (p7 + p9);
|
|
1742 |
/* Compute (q3, q4) = (t0, t1) + (r0, r1). */
|
|
1743 |
m_two_sum(q3, t0, r0, q4);
|
|
1744 |
q4 += (t1 + r1);
|
|
1745 |
/* Compute (t0, t1) = (q3, q4) + s1. */
|
|
1746 |
m_two_sum(t0, q3, s1, t1);
|
|
1747 |
t1 += q4;
|
|
1748 |
|
|
1749 |
/* O(eps^4) terms -- Nine-One-Sum */
|
|
1750 |
t1 += a[1] * b[3] + a[2] * b[2] + a[3] * b[1] + q6 + q7 + q8 + q9 + s2;
|
|
1751 |
|
|
1752 |
// fprintf(stderr, "before accur-renorm5: p0:%e p1:%e s0:%e t0:%e t1:%e\n", p0, p1, s0, t0, t1);
|
|
1753 |
m_renorm5(p0, p1, s0, t0, t1);
|
|
1754 |
// fprintf(stderr, "after accur-renorm5: p0:%e p1:%e s0:%e t0:%e t1:%e\n", p0, p1, s0, t0, t1);
|
|
1755 |
fpu_fix_end(&savedCV);
|
|
1756 |
|
|
1757 |
__qNew_qdReal(newQD, p0, p1, s0, t0);
|
|
1758 |
#endif
|
|
1759 |
RETURN( newQD );
|
4380
|
1760 |
}
|
|
1761 |
%}.
|
|
1762 |
^ super productFromQDouble:aQDouble.
|
|
1763 |
|
|
1764 |
"
|
4387
|
1765 |
(QDouble fromFloat:1.0) * 2.0
|
4380
|
1766 |
2.0 * (QDouble fromFloat:1.0)
|
4387
|
1767 |
(QDouble fromFloat:1.0) * (QDouble fromFloat:2.0)
|
|
1768 |
|
4380
|
1769 |
1e20 * (QDouble fromFloat:1.0)
|
|
1770 |
(2.0 * (QDouble fromFloat:1.0)) asFloat
|
|
1771 |
(1e20 * (QDouble fromFloat:1.0)) asFloat
|
|
1772 |
|
|
1773 |
(1e20 * (QDouble fromFloat:1.0) * 1e-20) asDoubleArray
|
4392
|
1774 |
|
|
1775 |
( ((QDouble fromFloat:1.0) + (QDouble fromFloat:1e20)) * (QDouble fromFloat:2.0)) asDoubleArray
|
4380
|
1776 |
"
|
|
1777 |
|
|
1778 |
"Created: / 13-06-2017 / 01:06:22 / cg"
|
4454
|
1779 |
"Modified: / 05-07-2017 / 11:07:16 / cg"
|
4380
|
1780 |
!
|
|
1781 |
|
4385
|
1782 |
quotientFromQDouble:aQDouble
|
4386
|
1783 |
"sloppy"
|
4395
|
1784 |
|
4385
|
1785 |
|q0 q1 q2 q3 r|
|
|
1786 |
|
4393
|
1787 |
q0 := aQDouble d0 / self d0.
|
|
1788 |
"/ Stdout showCR:('q0: %1 (a[0]=%2; b[0]=%3)\n' bindWith:q0 with:self d0 with:aQDouble d0).
|
|
1789 |
r := aQDouble - (self * q0).
|
|
1790 |
"/ Stdout showCR:('r: %1\n' bindWith:r asDoubleArray).
|
4385
|
1791 |
|
4393
|
1792 |
q1 := r d0 / self d0.
|
|
1793 |
"/ Stdout showCR:('q1: %1 (r[0]=%2; b[0]=%3)\n' bindWith:q1 with:r d0 with:aQDouble d0).
|
|
1794 |
r := r - (self * q1).
|
|
1795 |
"/ Stdout showCR:('r: %1\n' bindWith:r asDoubleArray).
|
4395
|
1796 |
|
4393
|
1797 |
q2 := r d0 / self d0.
|
|
1798 |
"/ Stdout showCR:('q2: %1 (r[0]=%2; b[0]=%3)\n' bindWith:q2 with:r d0 with:aQDouble d0).
|
|
1799 |
r := r - (self * q2).
|
|
1800 |
"/ Stdout showCR:('r: %1\n' bindWith:r asDoubleArray).
|
4380
|
1801 |
|
4393
|
1802 |
q3 := r d0 / self d0.
|
|
1803 |
"/ Stdout showCR:('q3: %1 (r[0]=%2; b[0]=%3)\n' bindWith:q3 with:r d0 with:aQDouble d0).
|
4386
|
1804 |
|
|
1805 |
r := self class d0:q0 d1:q1 d2:q2 d3:q3.
|
4393
|
1806 |
"/ Stdout showCR:('before renorm: %1\n' bindWith:r asDoubleArray).
|
4386
|
1807 |
r renorm.
|
4393
|
1808 |
"/ Stdout showCR:('after renorm: %1\n' bindWith:r asDoubleArray).
|
4386
|
1809 |
^ r
|
4380
|
1810 |
|
|
1811 |
"
|
4385
|
1812 |
2.0 / (QDouble fromFloat:2.0)
|
4387
|
1813 |
2.0 / (QDouble fromFloat:1.0)
|
4385
|
1814 |
1e20 / (QDouble fromFloat:1.0)
|
|
1815 |
(2.0 / (QDouble fromFloat:1.0)) asFloat
|
|
1816 |
(1e20 / (QDouble fromFloat:1.0)) asFloat
|
|
1817 |
|
|
1818 |
(QDouble fromFloat:2.0) / 2.0
|
|
1819 |
(QDouble fromFloat:1e20) / 2.0
|
|
1820 |
((QDouble fromFloat:1.0) / 2.0) asFloat
|
|
1821 |
((QDouble fromFloat:1e20 / 2.0)) asFloat
|
4395
|
1822 |
|
4385
|
1823 |
((1e20 + (QDouble fromFloat:1.0) + 1e-20) / 2.0) asDoubleArray
|
4393
|
1824 |
|
|
1825 |
((QDouble fromFloat:10.0) quotientFromQDouble: (QDouble fromFloat:1.234)) asDoubleArray
|
|
1826 |
((QDouble fromFloat:1.234) / (QDouble fromFloat:10.0)) asDoubleArray
|
4395
|
1827 |
|
4393
|
1828 |
q0: 1.234000e-01 (a[0]=1.234000e+00; b[0]=1.000000e+01)
|
|
1829 |
a: 1.234000e+00/0.000000e+00/0.000000e+00/0.000000e+00
|
|
1830 |
b: 1.000000e+01/0.000000e+00/0.000000e+00/0.000000e+00
|
|
1831 |
(b * q0): 1.234000e+00/-2.775558e-17/0.000000e+00/0.000000e+00
|
|
1832 |
r: 2.775558e-17/0.000000e+00/0.000000e+00/0.000000e+00
|
|
1833 |
|
|
1834 |
q1: 2.775558e-18 (r[0]=2.775558e-17; b[0]=1.000000e+01)
|
|
1835 |
r: -1.540744e-33/0.000000e+00/0.000000e+00/0.000000e+00
|
|
1836 |
|
|
1837 |
q2: -1.540744e-34 (r[0]=-1.540744e-33; b[0]=1.000000e+01)
|
|
1838 |
r: 8.552847e-50/0.000000e+00/0.000000e+00/0.000000e+00
|
|
1839 |
|
|
1840 |
q3: 8.552847e-51 (r[0]=8.552847e-50; b[0]=1.000000e+01)
|
|
1841 |
|
|
1842 |
before renorm: 1.234000e-01/2.775558e-18/-1.540744e-34/8.552847e-51
|
|
1843 |
after renorm: 1.234000e-01/2.775558e-18/-1.540744e-34/8.552847e-51
|
|
1844 |
1.234/10.0 is: 0.123400 / 0.000000 / -0.000000 / 0.000000
|
|
1845 |
|
|
1846 |
|
4380
|
1847 |
"
|
|
1848 |
|
4385
|
1849 |
"Created: / 13-06-2017 / 17:50:35 / cg"
|
4393
|
1850 |
"Modified (comment): / 15-06-2017 / 01:02:05 / cg"
|
4380
|
1851 |
!
|
|
1852 |
|
|
1853 |
sumFromFloat:aFloat
|
|
1854 |
%{
|
|
1855 |
if (__isFloatLike(aFloat)) {
|
4395
|
1856 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1857 |
double b = __floatVal(aFloat);
|
|
1858 |
double c0, c1, c2, c3;
|
|
1859 |
double e;
|
|
1860 |
OBJ newQD;
|
|
1861 |
int savedCV;
|
|
1862 |
|
|
1863 |
fpu_fix_start(&savedCV);
|
|
1864 |
|
|
1865 |
m_two_sum(c0, a[0], b, e);
|
|
1866 |
m_two_sum(c1 ,a[1], e, e);
|
|
1867 |
m_two_sum(c2, a[2], e, e);
|
|
1868 |
m_two_sum(c3, a[3], e, e);
|
|
1869 |
|
|
1870 |
m_renorm5(c0, c1, c2, c3, e);
|
|
1871 |
fpu_fix_end(&savedCV);
|
|
1872 |
__qNew_qdReal(newQD, c0, c1, c2, c3);
|
|
1873 |
RETURN( newQD );
|
4380
|
1874 |
}
|
|
1875 |
%}.
|
|
1876 |
^ super sumFromFloat:aFloat.
|
|
1877 |
|
|
1878 |
"
|
|
1879 |
1.0 + (QDouble fromFloat:1.0)
|
|
1880 |
1e20 + (QDouble fromFloat:1.0)
|
|
1881 |
(1.0 + (QDouble fromFloat:1.0)) asFloat
|
|
1882 |
(1e20 + (QDouble fromFloat:1.0)) asFloat
|
|
1883 |
|
|
1884 |
(1.0 + (QDouble fromFloat:1.0)) asDoubleArray
|
|
1885 |
(1e20 + (QDouble fromFloat:1.0)) asDoubleArray
|
|
1886 |
(1e20 + (QDouble fromFloat:1.0) + 1e-20) asDoubleArray
|
|
1887 |
"
|
|
1888 |
|
|
1889 |
"Created: / 12-06-2017 / 17:16:41 / cg"
|
4387
|
1890 |
"Modified: / 14-06-2017 / 11:43:47 / cg"
|
4380
|
1891 |
!
|
|
1892 |
|
4454
|
1893 |
sumFromInteger:anInteger
|
|
1894 |
^ self sumFromFloat:(anInteger asFloat)
|
|
1895 |
|
|
1896 |
"
|
|
1897 |
1 + (QDouble fromFloat:1.0)
|
|
1898 |
1e20 asInteger + (QDouble fromFloat:1.0)
|
|
1899 |
(1 + (QDouble fromFloat:1.0)) asFloat
|
|
1900 |
(1e20 asInteger + (QDouble fromFloat:1.0)) asFloat
|
|
1901 |
"
|
|
1902 |
|
|
1903 |
"Created: / 03-07-2017 / 10:35:46 / cg"
|
|
1904 |
!
|
|
1905 |
|
4380
|
1906 |
sumFromQDouble:aQDouble
|
|
1907 |
%{
|
4454
|
1908 |
|
|
1909 |
/* s = quick_three_accum(a, b, c) adds c to the dd-pair (a, b).
|
|
1910 |
* If the result does not fit in two doubles, then the sum is
|
|
1911 |
* output into s and (a,b) contains the remainder. Otherwise
|
|
1912 |
* s is zero and (a,b) contains the sum. */
|
|
1913 |
# define m_quick_three_accum(q3_outRef, q3_a, q3_b, q3_c) \
|
|
1914 |
{ \
|
|
1915 |
double s;\
|
|
1916 |
int za, zb;\
|
|
1917 |
\
|
|
1918 |
m_two_sum(s, q3_b, q3_c, q3_b); \
|
|
1919 |
m_two_sum(s, q3_a, s, q3_a); \
|
|
1920 |
\
|
|
1921 |
za = (q3_a != 0.0);\
|
|
1922 |
zb = (q3_b != 0.0);\
|
|
1923 |
\
|
|
1924 |
if (za && zb) {\
|
|
1925 |
q3_outRef = s;\
|
|
1926 |
} else {\
|
|
1927 |
if (!zb) {\
|
4981
|
1928 |
q3_b = q3_a;\
|
|
1929 |
q3_a = s;\
|
4454
|
1930 |
} else {\
|
4981
|
1931 |
q3_a = s;\
|
4454
|
1932 |
}\
|
|
1933 |
q3_outRef = 0.0;\
|
|
1934 |
}\
|
|
1935 |
}
|
|
1936 |
|
4380
|
1937 |
if (__Class(aQDouble) == QDouble) {
|
4981
|
1938 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
1939 |
double *b = __QuadDoubleInstPtr(aQDouble)->d_quadDoubleValue;
|
|
1940 |
OBJ newQD;
|
|
1941 |
int savedCV;
|
4454
|
1942 |
|
|
1943 |
#define QD_IEEE_ADD
|
|
1944 |
|
|
1945 |
#ifndef xQD_IEEE_ADD
|
4981
|
1946 |
// sloppy_add...
|
4380
|
1947 |
|
4393
|
1948 |
# if 0
|
4981
|
1949 |
double s0, s1, s2, s3;
|
|
1950 |
double t0, t1, t2, t3;
|
|
1951 |
|
|
1952 |
fpu_fix_start(&savedCV);
|
|
1953 |
m_two_sum(s0, a[0], b[0], t0);
|
|
1954 |
m_two_sum(s1, a[1], b[1], t1);
|
|
1955 |
m_two_sum(s2, a[2], b[2], t2);
|
|
1956 |
m_two_sum(s3, a[3], b[3], t3);
|
|
1957 |
|
|
1958 |
m_two_sum(s1, s1, t0, t0);
|
|
1959 |
m_three_sum(s2, t0, t1);
|
|
1960 |
m_three_sum2(s3, t0, t2);
|
|
1961 |
t0 = t0 + t1 + t3;
|
|
1962 |
|
|
1963 |
m_renorm5(s0, s1, s2, s3, t0);
|
|
1964 |
fpu_fix_end(&savedCV);
|
|
1965 |
|
|
1966 |
__qNew_qdReal(newQD, s0, s1, s2, s3);
|
4393
|
1967 |
# else
|
4380
|
1968 |
|
4981
|
1969 |
/* Same as above, but addition re-organized to minimize
|
|
1970 |
data dependency ... unfortunately some compilers are
|
|
1971 |
not very smart to do this automatically */
|
|
1972 |
double s0, s1, s2, s3;
|
|
1973 |
double t0, t1, t2, t3;
|
|
1974 |
double v0, v1, v2, v3;
|
|
1975 |
double u0, u1, u2, u3;
|
|
1976 |
double w0, w1, w2, w3;
|
|
1977 |
|
|
1978 |
fpu_fix_start(&savedCV);
|
|
1979 |
s0 = a[0] + b[0];
|
|
1980 |
s1 = a[1] + b[1];
|
|
1981 |
s2 = a[2] + b[2];
|
|
1982 |
s3 = a[3] + b[3];
|
|
1983 |
|
|
1984 |
v0 = s0 - a[0];
|
|
1985 |
v1 = s1 - a[1];
|
|
1986 |
v2 = s2 - a[2];
|
|
1987 |
v3 = s3 - a[3];
|
|
1988 |
|
|
1989 |
u0 = s0 - v0;
|
|
1990 |
u1 = s1 - v1;
|
|
1991 |
u2 = s2 - v2;
|
|
1992 |
u3 = s3 - v3;
|
|
1993 |
|
|
1994 |
w0 = a[0] - u0;
|
|
1995 |
w1 = a[1] - u1;
|
|
1996 |
w2 = a[2] - u2;
|
|
1997 |
w3 = a[3] - u3;
|
|
1998 |
|
|
1999 |
u0 = b[0] - v0;
|
|
2000 |
u1 = b[1] - v1;
|
|
2001 |
u2 = b[2] - v2;
|
|
2002 |
u3 = b[3] - v3;
|
|
2003 |
|
|
2004 |
t0 = w0 + u0;
|
|
2005 |
t1 = w1 + u1;
|
|
2006 |
t2 = w2 + u2;
|
|
2007 |
t3 = w3 + u3;
|
|
2008 |
|
|
2009 |
m_two_sum(s1, s1, t0, t0);
|
|
2010 |
m_three_sum(s2, t0, t1);
|
|
2011 |
m_three_sum2(s3, t0, t2);
|
|
2012 |
t0 = t0 + t1 + t3;
|
|
2013 |
|
|
2014 |
/* renormalize */
|
|
2015 |
m_renorm5(s0, s1, s2, s3, t0);
|
|
2016 |
fpu_fix_end(&savedCV);
|
|
2017 |
__qNew_qdReal(newQD, s0, s1, s2, s3);
|
4393
|
2018 |
# endif
|
4380
|
2019 |
#else
|
4981
|
2020 |
// ieee_add...
|
|
2021 |
int i, j, k;
|
|
2022 |
double s, t;
|
|
2023 |
double u, v; /* double-length accumulator */
|
|
2024 |
double x[4] = {0.0, 0.0, 0.0, 0.0};
|
|
2025 |
|
|
2026 |
fpu_fix_start(&savedCV);
|
|
2027 |
|
|
2028 |
i = j = k = 0;
|
|
2029 |
if (abs(a[i]) > abs(b[j]))
|
|
2030 |
u = a[i++];
|
|
2031 |
else
|
|
2032 |
u = b[j++];
|
|
2033 |
if (abs(a[i]) > abs(b[j]))
|
|
2034 |
v = a[i++];
|
|
2035 |
else
|
|
2036 |
v = b[j++];
|
|
2037 |
|
|
2038 |
m_quick_two_sum(u, u, v, v);
|
|
2039 |
|
|
2040 |
while (k < 4) {
|
|
2041 |
if (i >= 4 && j >= 4) {
|
|
2042 |
x[k] = u;
|
|
2043 |
if (k < 3)
|
|
2044 |
x[++k] = v;
|
|
2045 |
break;
|
|
2046 |
}
|
|
2047 |
|
|
2048 |
if (i >= 4)
|
|
2049 |
t = b[j++];
|
|
2050 |
else if (j >= 4)
|
|
2051 |
t = a[i++];
|
|
2052 |
else if (abs(a[i]) > abs(b[j])) {
|
|
2053 |
t = a[i++];
|
|
2054 |
} else
|
|
2055 |
t = b[j++];
|
|
2056 |
|
|
2057 |
m_quick_three_accum(s, u, v, t);
|
|
2058 |
|
|
2059 |
if (s != 0.0) {
|
|
2060 |
x[k++] = s;
|
|
2061 |
}
|
|
2062 |
}
|
|
2063 |
|
|
2064 |
/* add the rest. */
|
|
2065 |
for (k = i; k < 4; k++)
|
|
2066 |
x[3] += a[k];
|
|
2067 |
for (k = j; k < 4; k++)
|
|
2068 |
x[3] += b[k];
|
|
2069 |
|
|
2070 |
m_renorm4(x[0], x[1], x[2], x[3]);
|
|
2071 |
fpu_fix_end(&savedCV);
|
|
2072 |
__qNew_qdReal(newQD, x[0], x[1], x[2], x[3]);
|
4380
|
2073 |
#endif
|
4981
|
2074 |
RETURN(newQD);
|
4380
|
2075 |
}
|
|
2076 |
%}.
|
|
2077 |
^ super sumFromQDouble:aQDouble
|
4395
|
2078 |
|
4380
|
2079 |
"
|
|
2080 |
(QDouble fromFloat:1.0) + (QDouble fromFloat:1.0)
|
|
2081 |
(QDouble fromFloat:1.0) + 1.0
|
|
2082 |
1.0 + (QDouble fromFloat:1.0)
|
|
2083 |
|
|
2084 |
((QDouble fromFloat:1.0) + (QDouble fromFloat:1.0)) asDoubleArray
|
|
2085 |
((QDouble fromFloat:1.0) + 1.0) asDoubleArray
|
|
2086 |
(1.0 + (QDouble fromFloat:1.0)) asDoubleArray
|
|
2087 |
(1e-20 + (QDouble fromFloat:1.0)) asDoubleArray
|
|
2088 |
(1e20 + (QDouble fromFloat:1.0)) asDoubleArray
|
|
2089 |
"
|
|
2090 |
|
|
2091 |
"Created: / 12-06-2017 / 21:15:43 / cg"
|
4454
|
2092 |
"Modified: / 03-07-2017 / 23:09:11 / cg"
|
4380
|
2093 |
! !
|
|
2094 |
|
|
2095 |
!QDouble methodsFor:'inspecting'!
|
|
2096 |
|
|
2097 |
inspectorExtraAttributes
|
|
2098 |
"extra (pseudo instvar) entries to be shown in an inspector."
|
|
2099 |
|
|
2100 |
^ super inspectorExtraAttributes
|
4981
|
2101 |
add:'-{doubles}' -> [ self asDoubleArray printString ];
|
|
2102 |
yourself
|
4380
|
2103 |
|
|
2104 |
"Created: / 12-06-2017 / 23:43:05 / cg"
|
4478
|
2105 |
"Modified (format): / 18-07-2017 / 19:54:48 / cg"
|
4380
|
2106 |
! !
|
|
2107 |
|
4385
|
2108 |
!QDouble methodsFor:'mathematical functions'!
|
|
2109 |
|
4412
|
2110 |
exp
|
4440
|
2111 |
"/ the exp_sloppy code is the algorithm from the original C++ qd package;
|
|
2112 |
"/ however, it is inexact in the 37th digit
|
|
2113 |
"/ Therefore, use the inherited code, which is slow, but more precise.
|
|
2114 |
|
|
2115 |
^ super exp
|
|
2116 |
|
|
2117 |
"
|
4981
|
2118 |
2.0 exp -> 7.38905609893065
|
4440
|
2119 |
2.0 asQDouble exp -> 7.38905609893065022723
|
4442
|
2120 |
|
4981
|
2121 |
2 asQDouble exp printfPrintString:'%70.68f'
|
|
2122 |
-> 7.389056098930650227230427460575007813180315570551847324087127822432669
|
4442
|
2123 |
|
|
2124 |
actual result:
|
4981
|
2125 |
-> 7.3890560989306502272304274605750078131803155705518473240871278225225737960790577633843124850791217947737531612654...
|
|
2126 |
|
4440
|
2127 |
"
|
|
2128 |
|
|
2129 |
"Created: / 19-06-2017 / 01:49:32 / cg"
|
4442
|
2130 |
"Modified (comment): / 22-06-2017 / 14:32:53 / cg"
|
4440
|
2131 |
!
|
|
2132 |
|
|
2133 |
exp_sloppy
|
|
2134 |
"/ this is the algorithm from the qd C++ package;
|
|
2135 |
"/ however, it is inexact in the 37th digit
|
|
2136 |
"/
|
|
2137 |
"/ the inherited exp code is much slower, but more precise.
|
4981
|
2138 |
|
4412
|
2139 |
"/ Strategy: We first reduce the size of x by noting that
|
|
2140 |
"/
|
|
2141 |
"/ exp(kr + m * log(2)) = 2^m * exp(r)^k
|
|
2142 |
"/
|
|
2143 |
"/ where m and k are integers. By choosing m appropriately
|
|
2144 |
"/ we can make |kr| <= log(2) / 2 = 0.347. Then exp(r) is
|
|
2145 |
"/ evaluated using the familiar Taylor series. Reducing the
|
|
2146 |
"/ argument substantially speeds up the convergence. */
|
|
2147 |
|
4427
|
2148 |
|k inv_k d0 m mul_pwr2 mul2 r
|
4413
|
2149 |
s p t thresh eps i|
|
|
2150 |
|
|
2151 |
eps := 1.21543267145725e-63. "/ = 2^-209.
|
|
2152 |
|
4412
|
2153 |
d0 := self d0.
|
|
2154 |
(d0 <= -709.0) ifTrue:[
|
4981
|
2155 |
^ 0.0 asQDouble.
|
4413
|
2156 |
].
|
|
2157 |
(d0 >= 709.0) ifTrue:[
|
4981
|
2158 |
^ Infinity positive
|
4427
|
2159 |
].
|
|
2160 |
|
|
2161 |
(d0 = 0.0) ifTrue:[
|
4981
|
2162 |
^ 1.0 asQDouble.
|
4413
|
2163 |
].
|
4427
|
2164 |
(d0 = 1.0) ifTrue:[
|
4981
|
2165 |
self isOne ifTrue:[
|
|
2166 |
^ self class e
|
|
2167 |
].
|
4413
|
2168 |
].
|
4412
|
2169 |
|
4427
|
2170 |
k := 1.0 ldexp:16.
|
|
2171 |
inv_k := 1.0 / k.
|
4412
|
2172 |
|
|
2173 |
m := (d0 / Float ln2 + 0.5) floor.
|
|
2174 |
|
|
2175 |
mul_pwr2 := [:a :b | QDouble d0:(a d0 * b) d1:(a d1 * b) d2:(a d2 * b) d3:(a d3 * b) ].
|
4427
|
2176 |
mul2 := [:a | QDouble d0:(a d0 * 2.0) d1:(a d1 * 2.0) d2:(a d2 * 2.0) d3:(a d3 * 2.0) ].
|
4412
|
2177 |
|
|
2178 |
r := mul_pwr2 value:(self - (self class ln2 * m)) value:inv_k.
|
4413
|
2179 |
thresh := inv_k * eps.
|
4412
|
2180 |
|
|
2181 |
p := r squared.
|
|
2182 |
s := r + (mul_pwr2 value:p value:0.5).
|
|
2183 |
i := 1.
|
|
2184 |
[
|
4981
|
2185 |
p := p * r.
|
|
2186 |
t := p * (self class invFact at:i).
|
|
2187 |
i := i+1.
|
|
2188 |
s := s + t.
|
|
2189 |
] doWhile:[ (t asFloat abs > thresh) and:[i < 10] ].
|
4412
|
2190 |
|
4427
|
2191 |
s := (mul2 value:s) + s squared.
|
|
2192 |
s := (mul2 value:s) + s squared.
|
|
2193 |
s := (mul2 value:s) + s squared.
|
|
2194 |
s := (mul2 value:s) + s squared.
|
|
2195 |
s := (mul2 value:s) + s squared.
|
|
2196 |
s := (mul2 value:s) + s squared.
|
|
2197 |
s := (mul2 value:s) + s squared.
|
|
2198 |
s := (mul2 value:s) + s squared.
|
|
2199 |
s := (mul2 value:s) + s squared.
|
|
2200 |
s := (mul2 value:s) + s squared.
|
|
2201 |
s := (mul2 value:s) + s squared.
|
|
2202 |
s := (mul2 value:s) + s squared.
|
|
2203 |
s := (mul2 value:s) + s squared.
|
|
2204 |
s := (mul2 value:s) + s squared.
|
|
2205 |
s := (mul2 value:s) + s squared.
|
|
2206 |
s := (mul2 value:s) + s squared.
|
|
2207 |
|
4412
|
2208 |
s := s + 1.0.
|
|
2209 |
^ s ldexp:m asInteger.
|
|
2210 |
|
|
2211 |
"
|
|
2212 |
1.0 exp -> 2.71828182845905
|
|
2213 |
1.0 asQDouble exp -> 2.71828182845905
|
|
2214 |
"
|
|
2215 |
|
4440
|
2216 |
"Created: / 21-06-2017 / 13:48:27 / cg"
|
4412
|
2217 |
!
|
|
2218 |
|
4454
|
2219 |
exp_withAccuracy:epsilon
|
|
2220 |
"compute e^x of the receiver using a taylor series approximation.
|
|
2221 |
This method is only invoked for limitedPrecisionReal classes, which do not compute
|
|
2222 |
exp themself (i.e. QDouble)"
|
|
2223 |
|
|
2224 |
"/ uses taylor series:
|
|
2225 |
"/ x x^2 x^3
|
|
2226 |
"/ e^x = 1 + --- + --- + --- ...
|
|
2227 |
"/ 1!! 2!! 3!!
|
|
2228 |
|
4522
|
2229 |
|x2 facN num den approx delta|
|
4454
|
2230 |
|
|
2231 |
x2 := self asFloat squared.
|
|
2232 |
|
|
2233 |
num := x2.
|
|
2234 |
den := 2.
|
|
2235 |
facN := 2.
|
|
2236 |
approx := self + 1 + (num / den).
|
|
2237 |
|
|
2238 |
[
|
4981
|
2239 |
facN := facN + 1.
|
|
2240 |
den := den * facN.
|
|
2241 |
num := num * self.
|
|
2242 |
|
|
2243 |
delta := num / den.
|
|
2244 |
"/ Transcript showCR:delta.
|
|
2245 |
delta isNaN ifTrue:[self halt:'nan when dividing for delta'. num / den].
|
|
2246 |
delta = 0 ifTrue:[self halt:'zero delta'].
|
|
2247 |
approx := approx + delta.
|
4454
|
2248 |
] doUntil:[delta abs <= epsilon].
|
|
2249 |
|
|
2250 |
^ approx
|
|
2251 |
|
|
2252 |
"
|
|
2253 |
wolfram:
|
4981
|
2254 |
7.389056098930650227230427460575007813180315570551847324087
|
|
2255 |
|
|
2256 |
2 asQDouble exp_withAccuracy:QDouble epsilon 7.38905609893065022723
|
|
2257 |
2 asQDouble exp_withAccuracy:LongFloat epsilon 7.38905609893065022723
|
4454
|
2258 |
2 asQDouble exp_withAccuracy:Float epsilon 7.38905609893065022489
|
|
2259 |
|
4981
|
2260 |
2.718281828459045235360287471352662497757247093699959574966...
|
|
2261 |
|
|
2262 |
1 asQDouble exp_withAccuracy:QDouble epsilon 2.71828182845904523536
|
|
2263 |
1 asQDouble exp_withAccuracy:LongFloat epsilon 2.71828182845904523536
|
4454
|
2264 |
1 asQDouble exp_withAccuracy:Float epsilon 2.71828182845904522671
|
|
2265 |
|
4981
|
2266 |
1.7392749415205010473946813036112352261479840577250084... × 10^18
|
|
2267 |
|
|
2268 |
42 asQDouble exp_withAccuracy:QDouble epsilon NAN
|
4454
|
2269 |
42 asQDouble exp_withAccuracy:LongFloat epsilon 1.73927494152050104739e18
|
|
2270 |
42 asQDouble exp_withAccuracy:Float epsilon 1.73927494152050104739e18
|
|
2271 |
|
|
2272 |
"
|
|
2273 |
|
|
2274 |
"Created: / 04-07-2017 / 11:58:26 / cg"
|
4522
|
2275 |
"Modified: / 10-10-2017 / 16:03:34 / cg"
|
4454
|
2276 |
!
|
|
2277 |
|
4385
|
2278 |
floor
|
|
2279 |
"return the receiver truncated towards negative infinity"
|
|
2280 |
|
|
2281 |
%{
|
|
2282 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
2283 |
OBJ newQD;
|
|
2284 |
|
|
2285 |
double x0, x1, x2, x3;
|
|
2286 |
x1 = x2 = x3 = 0.0;
|
|
2287 |
x0 =floor(a[0]);
|
|
2288 |
|
|
2289 |
if (x0 == a[0]) {
|
4395
|
2290 |
x1 = floor(a[1]);
|
|
2291 |
if (x1 == a[1]) {
|
|
2292 |
x2 = floor(a[2]);
|
|
2293 |
if (x2 == a[2]) {
|
|
2294 |
x3 = floor(a[3]);
|
|
2295 |
}
|
|
2296 |
}
|
|
2297 |
|
|
2298 |
m_renorm4(x0, x1, x2, x3);
|
4385
|
2299 |
}
|
|
2300 |
__qNew_qdReal(newQD, x0, x1, x2, x3);
|
|
2301 |
RETURN( newQD );
|
|
2302 |
%}.
|
|
2303 |
|
|
2304 |
"
|
|
2305 |
(QDouble fromFloat:4.0) floor
|
|
2306 |
(QDouble fromFloat:4.1) floor
|
|
2307 |
(QDouble fromFloat:0.1) floor
|
|
2308 |
(0.1 + (QDouble fromFloat:1.0)) floor
|
4395
|
2309 |
(1e20 + (QDouble fromFloat:1.0)) floor
|
4385
|
2310 |
|
|
2311 |
(QDouble fromFloat:1.5) floor
|
|
2312 |
(QDouble fromFloat:0.5) floor
|
|
2313 |
(QDouble fromFloat:-0.5) floor
|
|
2314 |
(QDouble fromFloat:-1.5) floor
|
|
2315 |
"
|
|
2316 |
|
|
2317 |
"Created: / 13-06-2017 / 01:52:44 / cg"
|
|
2318 |
"Modified (comment): / 13-06-2017 / 17:33:19 / cg"
|
|
2319 |
!
|
|
2320 |
|
4415
|
2321 |
floor2
|
|
2322 |
"return the receiver truncated towards negative infinity"
|
|
2323 |
|
|
2324 |
|f0 f1 f2 f3|
|
|
2325 |
%{
|
|
2326 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
2327 |
OBJ newQD;
|
|
2328 |
|
|
2329 |
double x0, x1, x2, x3;
|
|
2330 |
x1 = x2 = x3 = 0.0;
|
|
2331 |
x0 =floor(a[0]);
|
|
2332 |
|
|
2333 |
if (x0 == a[0]) {
|
|
2334 |
x1 = floor(a[1]);
|
|
2335 |
if (x1 == a[1]) {
|
|
2336 |
x2 = floor(a[2]);
|
|
2337 |
if (x2 == a[2]) {
|
|
2338 |
x3 = floor(a[3]);
|
|
2339 |
}
|
|
2340 |
}
|
|
2341 |
|
|
2342 |
m_renorm4(x0, x1, x2, x3);
|
|
2343 |
}
|
|
2344 |
__qMKFLOAT(f0, x0);
|
|
2345 |
__qMKFLOAT(f1, x1);
|
|
2346 |
__qMKFLOAT(f2, x2);
|
|
2347 |
__qMKFLOAT(f3, x3);
|
|
2348 |
%}.
|
|
2349 |
^ (f0 + f1 + f2 + f3) asInteger
|
|
2350 |
|
|
2351 |
"
|
|
2352 |
(QDouble fromFloat:4.0) floor
|
|
2353 |
(QDouble fromFloat:4.1) floor
|
|
2354 |
(QDouble fromFloat:0.1) floor
|
|
2355 |
(0.1 + (QDouble fromFloat:1.0)) floor
|
|
2356 |
(1e20 + (QDouble fromFloat:1.0)) floor
|
|
2357 |
|
|
2358 |
(QDouble fromFloat:1.5) floor
|
|
2359 |
(QDouble fromFloat:0.5) floor
|
|
2360 |
(QDouble fromFloat:-0.5) floor
|
|
2361 |
(QDouble fromFloat:-1.5) floor
|
|
2362 |
"
|
|
2363 |
|
|
2364 |
"Created: / 13-06-2017 / 01:52:44 / cg"
|
|
2365 |
"Modified (comment): / 13-06-2017 / 17:33:19 / cg"
|
|
2366 |
!
|
|
2367 |
|
4411
|
2368 |
ln
|
4442
|
2369 |
"return the natural logarithm of myself.
|
4445
|
2370 |
Raises an exception, if the receiver is less or equal to zero.
|
|
2371 |
|
|
2372 |
Not sure if this is really faster than using a taylor right away:
|
|
2373 |
the three exp-computations at the end are done in qDouble and are tailors themself..."
|
4442
|
2374 |
|
4412
|
2375 |
|d0 x|
|
|
2376 |
|
4454
|
2377 |
^ super ln.
|
4981
|
2378 |
|
4412
|
2379 |
d0 := self d0.
|
|
2380 |
d0 = 1.0 ifTrue:[
|
4981
|
2381 |
self isOne ifTrue:[ ^ self class zero ].
|
4412
|
2382 |
].
|
4454
|
2383 |
d0 > 0.0 ifTrue:[
|
4981
|
2384 |
"/ initial approx.
|
|
2385 |
x := d0 ln asQDouble.
|
|
2386 |
"/ three more iterations of newton...
|
|
2387 |
x := x + (self / (x exp)) - 1.0.
|
|
2388 |
x := x + (self / (x exp)) - 1.0.
|
|
2389 |
x := x + (self / (x exp)) - 1.0.
|
|
2390 |
|
|
2391 |
^ x
|
4412
|
2392 |
].
|
4981
|
2393 |
|
4454
|
2394 |
"/ now done via trapInfinity; was:
|
|
2395 |
"/ d0 = 0.0 ifTrue:[
|
|
2396 |
"/ ^ Infinity negative
|
|
2397 |
"/ ].
|
|
2398 |
|
|
2399 |
"/ if you need -INF for a zero receiver, try Number trapInfinity:[...]
|
|
2400 |
^ self class
|
4981
|
2401 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal])
|
|
2402 |
receiver:self
|
|
2403 |
selector:#ln
|
|
2404 |
arguments:#()
|
|
2405 |
errorString:'bad receiver in ln (not strictly positive)'
|
4411
|
2406 |
|
|
2407 |
"
|
4412
|
2408 |
-1 ln
|
4442
|
2409 |
|
4454
|
2410 |
-1.0 asQDouble ln
|
|
2411 |
0.0 asQDouble ln
|
4411
|
2412 |
1.0 asQDouble ln
|
4442
|
2413 |
|
4981
|
2414 |
3.0 ln printfPrintString:'%60.58lf'
|
|
2415 |
-> 1.0986122886681097821082175869378261268138885498046875000000'
|
|
2416 |
^
|
|
2417 |
|
|
2418 |
3.0 asQDouble ln printfPrintString:'%60.58f'
|
|
2419 |
-> 1.0986122886681096913952452369225257046474905578227494517347
|
|
2420 |
|
|
2421 |
3.0 asQDouble ln printfPrintString:'%70.68f'
|
|
2422 |
-> 1.09861228866810969139524523692252570464749055782274945173469433364779
|
4443
|
2423 |
|
|
2424 |
(3.0 asQDouble ln_withAccuracy:1e-64) printfPrintString:'%70.68f'
|
4981
|
2425 |
1.09861228866810969139524523692252570464749055782274945173469433364475
|
4443
|
2426 |
(3.0 asQDouble ln_withAccuracy:1e-100) printfPrintString:'%70.68f'
|
4981
|
2427 |
'1.098612288668109691395245236922525704647490557822749451734694333656909'
|
4442
|
2428 |
|
|
2429 |
actual result:
|
4981
|
2430 |
-> 1.0986122886681096913952452369225257046474905578227494517346943336374942932186089668736157548137320887879700290659...
|
4411
|
2431 |
"
|
|
2432 |
|
|
2433 |
"Created: / 18-06-2017 / 23:32:54 / cg"
|
4454
|
2434 |
"Modified: / 04-07-2017 / 11:46:27 / cg"
|
4411
|
2435 |
!
|
|
2436 |
|
4385
|
2437 |
negated
|
4395
|
2438 |
^ self class
|
|
2439 |
d0:(self d0) negated
|
|
2440 |
d1:(self d1) negated
|
|
2441 |
d2:(self d2) negated
|
|
2442 |
d3:(self d3) negated
|
|
2443 |
|
|
2444 |
"
|
4385
|
2445 |
(QDouble fromFloat:1.0) negated
|
|
2446 |
((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0)) negated asDoubleArray
|
|
2447 |
|
|
2448 |
(((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0))
|
|
2449 |
+ ((QDouble fromFloat:1e20) + (QDouble fromFloat:1.0))) asDoubleArray
|
|
2450 |
"
|
|
2451 |
|
|
2452 |
"Created: / 12-06-2017 / 20:14:55 / cg"
|
|
2453 |
"Modified (comment): / 12-06-2017 / 23:46:57 / cg"
|
|
2454 |
!
|
|
2455 |
|
4442
|
2456 |
sqrt
|
|
2457 |
"Return the square root of the receiver"
|
|
2458 |
|
|
2459 |
"this computes a roughly 65 digits precision result,
|
|
2460 |
using sqrt from the double as an initial guess"
|
|
2461 |
|
|
2462 |
|guess|
|
4981
|
2463 |
|
4442
|
2464 |
guess := self asFloat sqrt asQDouble.
|
4454
|
2465 |
^ self sqrt_withAccuracy:(self epsilon) fromInitialGuess:guess
|
4442
|
2466 |
|
4981
|
2467 |
"FloatD is only correct in roughly 17 digits
|
|
2468 |
|
|
2469 |
2 sqrt printfPrintString:'%50.48f'
|
|
2470 |
-> 1.414213562373095145474621858738828450441360473633
|
|
2471 |
^
|
4442
|
2472 |
QDouble gives you much more:
|
|
2473 |
|
4981
|
2474 |
2 asQDouble sqrt printfPrintString:'%50.48f'
|
|
2475 |
-> 1.414213562373095048801688724209698078569671875377
|
|
2476 |
|
|
2477 |
2 asQDouble sqrt printfPrintString:'%60.58f'
|
|
2478 |
-> 1.4142135623730950488016887242096980785696718753769480731767'
|
|
2479 |
|
|
2480 |
2 asQDouble sqrt printfPrintString:'%70.68f'
|
|
2481 |
-> 1.41421356237309504880168872420969807856967187537694807317667973799602
|
4442
|
2482 |
|
|
2483 |
actual digits:
|
4981
|
2484 |
-> 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309...
|
4442
|
2485 |
"
|
|
2486 |
|
|
2487 |
"Created: / 22-06-2017 / 13:53:47 / cg"
|
4447
|
2488 |
"Modified: / 22-06-2017 / 17:08:06 / mawalch"
|
4454
|
2489 |
"Modified (format): / 03-07-2017 / 12:09:04 / cg"
|
4442
|
2490 |
!
|
|
2491 |
|
4385
|
2492 |
squared
|
4442
|
2493 |
"return receiver * receiver"
|
|
2494 |
|
4385
|
2495 |
%{
|
|
2496 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
|
2497 |
double p0, p1, p2, p3, p4, p5;
|
|
2498 |
double q0, q1, q2, q3;
|
|
2499 |
double s0, s1;
|
|
2500 |
double t0, t1;
|
|
2501 |
double t;
|
|
2502 |
OBJ newQD;
|
4395
|
2503 |
|
4442
|
2504 |
// code makes use of some symetries to avoid a few multiplications...
|
4981
|
2505 |
|
4385
|
2506 |
m_two_sqr(p0, a[0], q0);
|
|
2507 |
t = 2.0 * a[0];
|
|
2508 |
|
|
2509 |
m_two_prod(p1, t, a[1], q1);
|
|
2510 |
m_two_prod(p2, t, a[2], q2);
|
|
2511 |
m_two_sqr(p3, a[1], q3);
|
|
2512 |
|
|
2513 |
m_two_sum(p1, q0, p1, q0);
|
|
2514 |
|
|
2515 |
m_two_sum(q0, q0, q1, q1);
|
|
2516 |
m_two_sum(p2, p2, p3, p3);
|
|
2517 |
|
|
2518 |
m_two_sum(s0, q0, p2, t0);
|
|
2519 |
m_two_sum(s1, q1, p3, t1);
|
|
2520 |
|
|
2521 |
m_two_sum(s1, s1, t0, t0);
|
|
2522 |
t0 += t1;
|
|
2523 |
|
|
2524 |
m_quick_two_sum(s1, s1, t0, t0);
|
|
2525 |
m_quick_two_sum(p2, s0, s1, t1);
|
|
2526 |
m_quick_two_sum(p3, t1, t0, q0);
|
|
2527 |
|
|
2528 |
p4 = 2.0 * a[0] * a[3];
|
|
2529 |
p5 = 2.0 * a[1] * a[2];
|
|
2530 |
|
|
2531 |
m_two_sum(p4, p4, p5, p5);
|
|
2532 |
m_two_sum(q2, q2, q3, q3);
|
|
2533 |
|
|
2534 |
m_two_sum(t0, p4, q2, t1);
|
|
2535 |
t1 = t1 + p5 + q3;
|
|
2536 |
|
|
2537 |
m_two_sum(p3, p3, t0, p4);
|
|
2538 |
p4 = p4 + q0 + t1;
|
|
2539 |
|
|
2540 |
m_renorm5(p0, p1, p2, p3, p4);
|
|
2541 |
|
|
2542 |
__qNew_qdReal(newQD, p0, p1, s0, s1);
|
|
2543 |
RETURN( newQD );
|
|
2544 |
%}.
|
|
2545 |
|
|
2546 |
"
|
|
2547 |
(QDouble fromFloat:4.0) squared
|
4395
|
2548 |
(1e20 + (QDouble fromFloat:1.0)) squared
|
4385
|
2549 |
"
|
|
2550 |
|
|
2551 |
"Created: / 13-06-2017 / 01:27:58 / cg"
|
4442
|
2552 |
"Modified: / 22-06-2017 / 14:08:31 / cg"
|
4385
|
2553 |
! !
|
|
2554 |
|
4380
|
2555 |
!QDouble methodsFor:'printing & storing'!
|
|
2556 |
|
4393
|
2557 |
digitsWithPrecision:precision
|
4454
|
2558 |
<resource: #obsolete>
|
4395
|
2559 |
"generate digits and exponent.
|
|
2560 |
if precision is >0, that many digits are generated.
|
|
2561 |
If it is 0 the required number of digits is generated
|
|
2562 |
(but never more than the decimalPrecision, which is 65)"
|
|
2563 |
|
4393
|
2564 |
|numDigits r exp i d out str|
|
|
2565 |
|
|
2566 |
numDigits := precision+1. "/ number of digits
|
|
2567 |
r := self abs.
|
|
2568 |
self d0 = 0.0 ifTrue:[
|
4395
|
2569 |
^ { String new:(precision max:1) withAll:$0 . 0 }
|
4393
|
2570 |
].
|
|
2571 |
|
|
2572 |
out := WriteStream on:(String new:precision+5).
|
|
2573 |
|
|
2574 |
"/ determine approx. exponent
|
|
2575 |
exp := self d0 abs log10 floor.
|
|
2576 |
exp < -300 ifTrue:[
|
4395
|
2577 |
"/ 1e-305 asQDouble
|
|
2578 |
r := r * (10.0 raisedToInteger:300).
|
|
2579 |
r := r / (10.0 raisedToInteger:(exp+300)).
|
4393
|
2580 |
] ifFalse:[
|
4395
|
2581 |
exp > 300 ifTrue:[
|
|
2582 |
"/ 1e305 asQDouble
|
|
2583 |
"/ lexpr(x,exp) = x * 2 ^ exp
|
4393
|
2584 |
self halt.
|
4395
|
2585 |
r := r * (2 raisedTo:-53).
|
|
2586 |
r := r / (10.0 asQDouble raisedTo: exp).
|
|
2587 |
r := r * (2 raisedTo:53).
|
|
2588 |
] ifFalse:[
|
|
2589 |
r := r / (10.0 asQDouble raisedTo:exp).
|
|
2590 |
]
|
4393
|
2591 |
].
|
|
2592 |
|
|
2593 |
"/ Fix exponent if we are off by one
|
|
2594 |
(r >= 10.0) ifTrue:[
|
4395
|
2595 |
r := r / 10.0.
|
|
2596 |
exp := exp + 1.
|
4393
|
2597 |
] ifFalse:[
|
4395
|
2598 |
(r < 1.0) ifTrue:[
|
|
2599 |
r := r * 10.0.
|
|
2600 |
exp := exp - 1.
|
|
2601 |
]
|
4393
|
2602 |
].
|
|
2603 |
|
|
2604 |
((r >= 10.0) or:[ r < 1.0 ]) ifTrue:[
|
4395
|
2605 |
self error:'can''t compute exponent.'.
|
4393
|
2606 |
].
|
|
2607 |
|
4395
|
2608 |
"/
|
4393
|
2609 |
"/ Extract the digits
|
4395
|
2610 |
"/ notice, that the d1,d2 and d3 components might
|
|
2611 |
"/ be negative; therefore characters out of the 0..9 range
|
|
2612 |
"/ might be produced here
|
|
2613 |
"/
|
|
2614 |
i := 1.
|
|
2615 |
[ (precision ~~ 0 and:[ i <= numDigits ])
|
|
2616 |
or:[ (precision == 0 and:[r d0 ~= 0.0]) ]] whileTrue:[
|
|
2617 |
d := r d0 truncated.
|
|
2618 |
r := r - d.
|
|
2619 |
r := r * 10.0.
|
|
2620 |
|
|
2621 |
out nextPut:($0 + d).
|
|
2622 |
i := i + 1.
|
4393
|
2623 |
].
|
4395
|
2624 |
numDigits := i-1.
|
4393
|
2625 |
|
|
2626 |
str := out contents.
|
4395
|
2627 |
|
4393
|
2628 |
"/ Fix out-of-range digits.
|
4395
|
2629 |
numDigits to:2 by:-1 do:[:i |
|
|
2630 |
(str at:i) < $0 ifTrue:[
|
|
2631 |
str at:i-1 put:(str at:i-1) - 1.
|
|
2632 |
str at:i put:(str at:i) + 10.
|
|
2633 |
] ifFalse:[
|
|
2634 |
(str at:i) > $9 ifTrue:[
|
|
2635 |
str at:i-1 put:(str at:i-1) + 1.
|
|
2636 |
str at:i put:(str at:i) - 10.
|
|
2637 |
].
|
|
2638 |
].
|
4393
|
2639 |
].
|
|
2640 |
|
4395
|
2641 |
str first <= $0 ifTrue:[
|
|
2642 |
self error:'non-positive leading digit'
|
|
2643 |
].
|
|
2644 |
|
|
2645 |
"/ Round, handle carry
|
4393
|
2646 |
(str at:numDigits) >= $5 ifTrue:[
|
4395
|
2647 |
str at:numDigits-1 put:(str at:numDigits-1) + 1.
|
|
2648 |
i := numDigits-1.
|
|
2649 |
[i > 1 and:[(str at:i) > $9]] whileTrue:[
|
|
2650 |
str at:i put:(str at:i) - 10.
|
|
2651 |
i := i - 1.
|
|
2652 |
str at:i put:(str at:i) + 1.
|
|
2653 |
]
|
|
2654 |
].
|
4393
|
2655 |
|
|
2656 |
"/ If first digit is 10, shift everything.
|
4395
|
2657 |
str first > $9 ifTrue:[
|
|
2658 |
exp := exp + 1.
|
|
2659 |
str at:1 put:$0.
|
|
2660 |
str := '1',str
|
|
2661 |
].
|
|
2662 |
^ { (str copyTo:numDigits-1) . exp }
|
|
2663 |
|
4393
|
2664 |
"
|
4395
|
2665 |
0 asQDouble digitsWithPrecision:1 -> #('0' 0)
|
|
2666 |
0 asQDouble digitsWithPrecision:0 -> #('0' 0)
|
|
2667 |
|
|
2668 |
|
|
2669 |
1.2345 printfPrintString:'%.4f'
|
|
2670 |
1.2345 asQDouble digitsWithPrecision:5 -> #('12345' 0)
|
|
2671 |
|
|
2672 |
--- but 1.2345 is not really what you think:
|
|
2673 |
1.2345 printfPrintString:'%.20f'
|
|
2674 |
1.2345 asQDouble digitsWithPrecision:20 -> #('12344999999999999307' 0)
|
|
2675 |
|
|
2676 |
12.345 asQDouble digitsWithPrecision:5 -> #('12345' 1)
|
|
2677 |
12345 asQDouble digitsWithPrecision:5 -> #('12345' 4)
|
|
2678 |
12345.1 asQDouble digitsWithPrecision:5 -> #('12345' 4)
|
|
2679 |
12345.9 asQDouble digitsWithPrecision:5 -> #('12346' 4)
|
|
2680 |
|
4393
|
2681 |
1.2345 asQDouble / 10.0 asQDouble
|
4395
|
2682 |
1.2345 asQDouble / 10.0
|
4393
|
2683 |
"
|
|
2684 |
|
4395
|
2685 |
"Created: / 15-06-2017 / 09:10:01 / cg"
|
|
2686 |
"Modified: / 16-06-2017 / 10:01:03 / cg"
|
4393
|
2687 |
!
|
|
2688 |
|
|
2689 |
printOn:aStream
|
4395
|
2690 |
"return a printed representation of the receiver.
|
|
2691 |
|
|
2692 |
Notice:
|
4981
|
2693 |
this code was adapted from an ugly piece of c++ code,
|
|
2694 |
which was obviously hacked.
|
|
2695 |
It does need a rework.
|
|
2696 |
As an alternative, use the printf functions, which should also deal wth QDoubles"
|
|
2697 |
|
|
2698 |
thisContext isRecursive ifTrue:[
|
|
2699 |
aStream nextPutAll:'aQDouble (error while printing)'.
|
|
2700 |
^ self.
|
4978
|
2701 |
].
|
4981
|
2702 |
|
4438
|
2703 |
PrintfScanf printf:'%g' on:aStream argument:self.
|
4395
|
2704 |
|
|
2705 |
"/ self
|
|
2706 |
"/ printOn:aStream precision:40 width:0
|
|
2707 |
"/ fixed:true showPositive:false uppercase:false fillChar:(Character space)
|
4393
|
2708 |
|
|
2709 |
"
|
4404
|
2710 |
(1.2345 asQDouble) printOn:Transcript . Transcript cr.
|
|
2711 |
(1.2345 asFloat) printOn:Transcript . Transcript cr.
|
|
2712 |
(1.2345 asLongFloat) printOn:Transcript . Transcript cr.
|
|
2713 |
(1.2345 asShortFloat) printOn:Transcript . Transcript cr.
|
|
2714 |
|
4393
|
2715 |
((QDouble fromFloat:1.2345) / 10.0) printOn:Transcript
|
4439
|
2716 |
((QDouble fromFloat:1.2345) / 10000.0) printOn:Transcript
|
|
2717 |
((QDouble fromFloat:1.2345) / 1000000000.0) printOn:Transcript
|
4393
|
2718 |
"
|
|
2719 |
|
|
2720 |
"Created: / 15-06-2017 / 01:51:36 / cg"
|
4439
|
2721 |
"Modified (comment): / 21-06-2017 / 09:55:10 / cg"
|
4978
|
2722 |
"Modified: / 05-06-2019 / 20:38:58 / Claus Gittinger"
|
4393
|
2723 |
!
|
|
2724 |
|
4395
|
2725 |
printOn:aStream precision:precisionIn width:width
|
4393
|
2726 |
fixed:fixed showPositive:showPositive uppercase:uppercase fillChar:fillChar
|
4454
|
2727 |
<resource: #obsolete>
|
4393
|
2728 |
|
|
2729 |
"return a printed representation of the receiver.
|
4395
|
2730 |
This is a parametrized entry, which can be used by printf-like functions.
|
|
2731 |
Notice:
|
|
2732 |
this code was adapted from an ugly piece of c++ code,
|
|
2733 |
which was obviously hacked.
|
|
2734 |
It does need a rework.
|
|
2735 |
As an alternative, use the printf functions, which should also deal wth QDoubles
|
|
2736 |
"
|
|
2737 |
|
|
2738 |
"
|
|
2739 |
1.2345 asQDouble printString
|
|
2740 |
12.345 asQDouble printString
|
|
2741 |
12345 asQDouble printString
|
|
2742 |
"
|
|
2743 |
|
|
2744 |
|sgn count delta exp precision|
|
|
2745 |
|
|
2746 |
"/ self d1 = 0.0 ifTrue:[
|
|
2747 |
"/ self d0 printOn:aStream.
|
|
2748 |
"/ ^ self.
|
|
2749 |
"/ ].
|
4385
|
2750 |
|
4393
|
2751 |
count := 0.
|
|
2752 |
sgn := true.
|
|
2753 |
exp := 0.
|
4395
|
2754 |
precision := precisionIn.
|
|
2755 |
|
4393
|
2756 |
self isInfinite ifTrue:[
|
4395
|
2757 |
self < 0 ifTrue:[
|
|
2758 |
aStream nextPut:$-.
|
|
2759 |
count := 1.
|
|
2760 |
] ifFalse:[
|
|
2761 |
showPositive ifTrue:[
|
|
2762 |
aStream nextPut:$+.
|
|
2763 |
count := 1.
|
|
2764 |
] ifFalse:[
|
|
2765 |
sgn := false.
|
|
2766 |
].
|
|
2767 |
].
|
|
2768 |
uppercase ifTrue:[
|
|
2769 |
aStream nextPutAll:'INF'
|
|
2770 |
] ifFalse:[
|
|
2771 |
aStream nextPutAll:'inf'
|
|
2772 |
].
|
|
2773 |
count := count + 3.
|
4385
|
2774 |
] ifFalse:[
|
4395
|
2775 |
self isNaN ifTrue:[
|
|
2776 |
uppercase ifTrue:[
|
|
2777 |
aStream nextPutAll:'NAN'
|
|
2778 |
] ifFalse:[
|
|
2779 |
aStream nextPutAll:'nan'
|
|
2780 |
].
|
|
2781 |
count := count + 3.
|
|
2782 |
] ifFalse:[
|
|
2783 |
self < 0 ifTrue:[
|
|
2784 |
aStream nextPut:$-.
|
|
2785 |
count := count + 1.
|
|
2786 |
] ifFalse:[
|
|
2787 |
showPositive ifTrue:[
|
|
2788 |
aStream nextPut:$+.
|
|
2789 |
count := count + 1.
|
|
2790 |
] ifFalse:[
|
|
2791 |
sgn := false.
|
|
2792 |
].
|
|
2793 |
].
|
|
2794 |
self = 0.0 ifTrue:[
|
|
2795 |
aStream nextPut:$0.
|
|
2796 |
count := count + 1.
|
|
2797 |
precision > 0 ifTrue:[
|
|
2798 |
aStream nextPut:$..
|
|
2799 |
count := count + 1.
|
|
2800 |
precision timesRepeat:[ aStream nextPut:$0 ].
|
|
2801 |
count := count + precision.
|
|
2802 |
].
|
|
2803 |
self halt.
|
|
2804 |
] ifFalse:[
|
|
2805 |
|off d d_width_extra|
|
|
2806 |
|
|
2807 |
"/ non-zero case
|
|
2808 |
off := fixed
|
|
2809 |
ifTrue:[ 1 + self asFloat abs log10 floor asInteger ]
|
|
2810 |
ifFalse:[1].
|
|
2811 |
d := precision + off.
|
|
2812 |
d_width_extra := d.
|
|
2813 |
fixed ifTrue:[
|
|
2814 |
d_width_extra := 40 max:d.
|
|
2815 |
].
|
|
2816 |
"/ highly special case - fixed mode, precision is zero, abs(*this) < 1.0
|
|
2817 |
"/ without this trap a number like 0.9 printed fixed with 0 precision prints as 0
|
|
2818 |
"/ should be rounded to 1.
|
|
2819 |
(fixed and:[ (precision == 0) and:[ (self abs < 1.0) ]]) ifTrue:[
|
|
2820 |
(self abs >= 0.5) ifTrue:[
|
|
2821 |
aStream nextPut:$1
|
|
2822 |
] ifFalse:[
|
|
2823 |
aStream nextPut:$0
|
|
2824 |
].
|
|
2825 |
^ self
|
|
2826 |
].
|
|
2827 |
|
|
2828 |
"/ handle near zero to working precision (but not exactly zero)
|
|
2829 |
(fixed and:[ d <= 0 ]) ifTrue:[
|
|
2830 |
aStream nextPut:$0.
|
|
2831 |
(precision > 0) ifTrue:[
|
|
2832 |
aStream nextPut:$. .
|
|
2833 |
aStream next:precision put:$0.
|
|
2834 |
]
|
|
2835 |
] ifFalse:[
|
|
2836 |
"/ default
|
|
2837 |
|
|
2838 |
|t j|
|
|
2839 |
|
|
2840 |
t := self digitsWithPrecision:(fixed ifTrue:[d_width_extra] ifFalse:[d])+1.
|
|
2841 |
exp := t second.
|
|
2842 |
t := t first.
|
|
2843 |
|
|
2844 |
fixed ifTrue:[
|
|
2845 |
"/ fix the string if it's been computed incorrectly
|
|
2846 |
"/ round here in the decimal string if required
|
|
2847 |
t := self round_string_qd:t at:(d + 1) offset:off.
|
|
2848 |
precision := t at:3.
|
|
2849 |
off := t at:2.
|
|
2850 |
t := t at:1.
|
|
2851 |
|
|
2852 |
(off > 0) ifTrue:[
|
|
2853 |
aStream next:off putAll:t startingAt:1.
|
|
2854 |
(precision > 0) ifTrue:[
|
|
2855 |
aStream nextPut:$. .
|
|
2856 |
aStream next:precision-1 putAll:t startingAt:off+1.
|
|
2857 |
]
|
|
2858 |
] ifFalse:[
|
|
2859 |
aStream nextPutAll:'0.'.
|
|
2860 |
(off < 0) ifTrue:[
|
|
2861 |
aStream next:off negated put:$0.
|
|
2862 |
].
|
|
2863 |
aStream next:d putAll:t startingAt:0.
|
|
2864 |
]
|
|
2865 |
] ifFalse:[
|
|
2866 |
aStream nextPut:(t at:1).
|
|
2867 |
(precision > 0) ifTrue:[
|
|
2868 |
aStream nextPut:$. .
|
|
2869 |
].
|
|
2870 |
aStream next:precision putAll:t startingAt:2.
|
|
2871 |
]
|
|
2872 |
].
|
|
2873 |
].
|
|
2874 |
]
|
4385
|
2875 |
].
|
4393
|
2876 |
|
|
2877 |
"/ trap for improper offset with large values
|
|
2878 |
"/ without this trap, output of values of the for 10^j - 1 fail for j > 28
|
|
2879 |
"/ and are output with the point in the wrong place, leading to a dramatically off value
|
4385
|
2880 |
|
4393
|
2881 |
"/ (fixed and:[ (precision > 0) ]) ifTrue:[
|
|
2882 |
"/ "/ make sure that the value isn't dramatically larger
|
|
2883 |
"/ from_string = atof(s.c_str());
|
|
2884 |
"/
|
|
2885 |
"/ // if this ratio is large, then we've got problems
|
|
2886 |
"/ if( fabs( from_string / this->x[0] ) > 3.0 ){
|
|
2887 |
"/
|
|
2888 |
"/ int point_position;
|
|
2889 |
"/ char temp;
|
|
2890 |
"/
|
|
2891 |
"/ // loop on the string, find the point, move it up one
|
|
2892 |
"/ // don't act on the first character
|
|
2893 |
"/ for(i=1; i < s.length(); i++){
|
|
2894 |
"/ if(s[i] == '.'){
|
|
2895 |
"/ s[i] = s[i-1] ;
|
|
2896 |
"/ s[i-1] = '.' ;
|
|
2897 |
"/ break;
|
|
2898 |
"/ }
|
|
2899 |
"/ }
|
|
2900 |
"/
|
|
2901 |
"/ from_string = atof(s.c_str());
|
|
2902 |
"/ // if this ratio is large, then the string has not been fixed
|
|
2903 |
"/ if( fabs( from_string / this->x[0] ) > 3.0 ){
|
|
2904 |
"/ dd_real::error("Re-rounding unsuccessful in large number fixed point trap.") ;
|
|
2905 |
"/ }
|
|
2906 |
"/ }
|
|
2907 |
"/ }
|
|
2908 |
"/
|
4395
|
2909 |
fixed ifFalse:[
|
|
2910 |
"/ Fill in exponent part
|
|
2911 |
aStream nextPut:(uppercase ifTrue:[$E] ifFalse:[$e]).
|
|
2912 |
aStream print:exp.
|
|
2913 |
].
|
4393
|
2914 |
|
|
2915 |
"/ fill in the blanks
|
|
2916 |
(delta := width-count) > 0 ifTrue:[
|
4395
|
2917 |
self halt.
|
4393
|
2918 |
"/ if (fmt & ios_base::internal) {
|
|
2919 |
"/ if (sgn)
|
|
2920 |
"/ s.insert(static_cast<string::size_type>(1), delta, fill);
|
|
2921 |
"/ else
|
|
2922 |
"/ s.insert(static_cast<string::size_type>(0), delta, fill);
|
|
2923 |
"/ } else if (fmt & ios_base::left) {
|
|
2924 |
"/ s.append(delta, fill);
|
|
2925 |
"/ } else {
|
|
2926 |
"/ s.insert(static_cast<string::size_type>(0), delta, fill);
|
|
2927 |
"/ }
|
|
2928 |
"/ }
|
|
2929 |
].
|
|
2930 |
|
|
2931 |
"Created: / 15-06-2017 / 02:37:31 / cg"
|
4395
|
2932 |
"Modified (comment): / 16-06-2017 / 14:48:30 / cg"
|
4385
|
2933 |
!
|
|
2934 |
|
4395
|
2935 |
round_string_qd:str at:precisionIn offset:offsetIn
|
4454
|
2936 |
<resource: #obsolete>
|
4395
|
2937 |
"returns a triple of: { new-str . new-offset . new-precision }"
|
|
2938 |
|
|
2939 |
"/
|
|
2940 |
"/ Input string must be all digits or errors will occur.
|
|
2941 |
"/
|
|
2942 |
|
|
2943 |
|i numDigits offsetOut precisionOut|
|
|
2944 |
|
|
2945 |
numDigits := precisionIn.
|
|
2946 |
|
|
2947 |
offsetOut := offsetIn.
|
|
2948 |
precisionOut := precisionIn.
|
|
2949 |
|
|
2950 |
"/ Round, handle carry
|
|
2951 |
((str at:numDigits) >= $5) ifTrue:[
|
|
2952 |
str at:numDigits-1 put:(str at:numDigits-1)+1.
|
|
2953 |
i := numDigits-1.
|
|
2954 |
[ i > 1 and:[ (str at:i) > $9] ] whileTrue:[
|
|
2955 |
str at:i put:(str at:i)-10.
|
|
2956 |
i := i - 1.
|
|
2957 |
str at:i put:(str at:i)+1.
|
|
2958 |
]
|
|
2959 |
].
|
|
2960 |
|
|
2961 |
"/ If first digit is 10, shift everything.
|
|
2962 |
(str at:1) > $9 ifTrue:[
|
|
2963 |
"/ e++; // don't modify exponent here
|
|
2964 |
str replaceFrom:2 with:str startingAt:1.
|
|
2965 |
str at:1 put:$1.
|
|
2966 |
str at:2 put:$0.
|
|
2967 |
offsetOut := offsetOut + 1.
|
|
2968 |
precisionOut := precisionOut + 1.
|
|
2969 |
].
|
|
2970 |
^ { (str copyTo:precisionOut) . offsetOut . precisionOut }
|
|
2971 |
|
|
2972 |
"Created: / 16-06-2017 / 10:12:39 / cg"
|
|
2973 |
"Modified (comment): / 16-06-2017 / 11:22:03 / cg"
|
4380
|
2974 |
! !
|
|
2975 |
|
|
2976 |
!QDouble methodsFor:'private accessing'!
|
|
2977 |
|
|
2978 |
d0
|
4386
|
2979 |
"the most significant (and highest valued) 53 bits of precision"
|
4380
|
2980 |
%{
|
4395
|
2981 |
RETURN ( __MKFLOAT(__QuadDoubleInstPtr(self)->d_quadDoubleValue[0]) );
|
4380
|
2982 |
%}
|
|
2983 |
|
|
2984 |
"Created: / 12-06-2017 / 20:15:12 / cg"
|
4386
|
2985 |
"Modified (comment): / 13-06-2017 / 17:59:47 / cg"
|
4380
|
2986 |
!
|
|
2987 |
|
|
2988 |
d1
|
4386
|
2989 |
"the next most significant (and next highest valued) 53 bits of precision"
|
4380
|
2990 |
%{
|
4395
|
2991 |
RETURN ( __MKFLOAT(__QuadDoubleInstPtr(self)->d_quadDoubleValue[1]) );
|
4380
|
2992 |
%}
|
|
2993 |
|
|
2994 |
"Created: / 12-06-2017 / 20:15:12 / cg"
|
4386
|
2995 |
"Modified (comment): / 13-06-2017 / 18:00:00 / cg"
|
4380
|
2996 |
!
|
|
2997 |
|
|
2998 |
d2
|
|
2999 |
%{
|
4395
|
3000 |
RETURN ( __MKFLOAT(__QuadDoubleInstPtr(self)->d_quadDoubleValue[2]) );
|
4380
|
3001 |
%}
|
|
3002 |
|
|
3003 |
"Created: / 12-06-2017 / 20:15:29 / cg"
|
|
3004 |
!
|
|
3005 |
|
|
3006 |
d3
|
4386
|
3007 |
"the least significant (and smallest valued) 53 bits of precision"
|
4380
|
3008 |
%{
|
4395
|
3009 |
RETURN ( __MKFLOAT(__QuadDoubleInstPtr(self)->d_quadDoubleValue[3]) );
|
4380
|
3010 |
%}
|
|
3011 |
|
|
3012 |
"Created: / 12-06-2017 / 20:15:32 / cg"
|
4386
|
3013 |
"Modified (comment): / 13-06-2017 / 18:00:18 / cg"
|
|
3014 |
!
|
|
3015 |
|
|
3016 |
renorm
|
4393
|
3017 |
"destructive renormalization"
|
4386
|
3018 |
%{
|
4395
|
3019 |
double *a = __QuadDoubleInstPtr(self)->d_quadDoubleValue;
|
4386
|
3020 |
double c0, c1, c2, c3;
|
|
3021 |
|
|
3022 |
c0 = a[0];
|
|
3023 |
c1 = a[1];
|
|
3024 |
c2 = a[2];
|
|
3025 |
c3 = a[3];
|
|
3026 |
m_renorm4(c0, c1, c2, c3);
|
|
3027 |
a[0] = c0;
|
|
3028 |
a[1] = c1;
|
|
3029 |
a[2] = c2;
|
|
3030 |
a[3] = c3;
|
|
3031 |
RETURN( self );
|
|
3032 |
%}.
|
|
3033 |
^ self error.
|
|
3034 |
|
|
3035 |
"
|
|
3036 |
(QDouble fromFloat:1.0) renorm
|
|
3037 |
"
|
|
3038 |
|
|
3039 |
"Created: / 13-06-2017 / 18:05:33 / cg"
|
4393
|
3040 |
"Modified: / 15-06-2017 / 00:12:59 / cg"
|
|
3041 |
! !
|
|
3042 |
|
|
3043 |
!QDouble methodsFor:'testing'!
|
|
3044 |
|
4404
|
3045 |
isFinite
|
|
3046 |
^ self d0 isFinite
|
|
3047 |
|
|
3048 |
"Created: / 17-06-2017 / 03:40:30 / cg"
|
|
3049 |
!
|
|
3050 |
|
4393
|
3051 |
isInfinite
|
|
3052 |
^ self d0 isInfinite
|
|
3053 |
|
|
3054 |
"Created: / 15-06-2017 / 01:57:57 / cg"
|
|
3055 |
!
|
|
3056 |
|
|
3057 |
isNaN
|
|
3058 |
^ self d0 isNaN
|
|
3059 |
|
|
3060 |
"Created: / 15-06-2017 / 01:57:35 / cg"
|
4411
|
3061 |
!
|
|
3062 |
|
|
3063 |
isOne
|
|
3064 |
^ self d0 = 1.0
|
|
3065 |
and:[ self d1 = 0.0
|
|
3066 |
and:[ self d2 = 0.0
|
|
3067 |
and:[ self d3 = 0.0 ]]]
|
|
3068 |
|
|
3069 |
"Created: / 18-06-2017 / 23:29:07 / cg"
|
|
3070 |
!
|
|
3071 |
|
|
3072 |
isZero
|
|
3073 |
^ self d0 = 0.0
|
|
3074 |
|
|
3075 |
"Created: / 18-06-2017 / 23:29:25 / cg"
|
4380
|
3076 |
! !
|
|
3077 |
|
|
3078 |
!QDouble class methodsFor:'documentation'!
|
|
3079 |
|
|
3080 |
version
|
|
3081 |
^ '$Header$'
|
|
3082 |
!
|
|
3083 |
|
|
3084 |
version_CVS
|
|
3085 |
^ '$Header$'
|
|
3086 |
! !
|