5264
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4983
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
LimitedPrecisionReal variableByteSubclass:#QuadFloat
|
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:'QuadFloatZero QuadFloatOne Pi E Epsilon NaN PositiveInfinity
|
5268
|
10 |
NegativeInfinity Halfpi HalfpiNegative Phi DefaultPrintFormat
|
|
11 |
DefaultPrintfFormat'
|
4983
|
12 |
poolDictionaries:''
|
|
13 |
category:'Magnitude-Numbers'
|
|
14 |
!
|
|
15 |
|
4991
|
16 |
!QuadFloat primitiveDefinitions!
|
|
17 |
%{
|
5266
|
18 |
#define SUPPORT_QUADFLOAT
|
5027
|
19 |
|
5266
|
20 |
#include <math.h>
|
4998
|
21 |
|
5266
|
22 |
extern float128_t STX_qadd(float128_t, float128_t, int);
|
5267
|
23 |
extern float128_t STX_qmul(float128_t, float128_t);
|
|
24 |
extern float128_t STX_qdiv(float128_t, float128_t);
|
|
25 |
extern float128_t STX_qneg(float128_t);
|
|
26 |
extern float128_t STX_qabs(float128_t);
|
|
27 |
extern float128_t STX_qfloor(float128_t);
|
|
28 |
extern float128_t STX_qfrexp(float128_t, int*);
|
|
29 |
extern float128_t STX_qceil(float128_t);
|
|
30 |
extern float128_t STX_qlog(float128_t);
|
|
31 |
extern float128_t STX_qlog10(float128_t);
|
|
32 |
extern float128_t STX_qlog2(float128_t);
|
|
33 |
extern float128_t STX_qexp(float128_t);
|
|
34 |
extern float128_t STX_qsin(float128_t);
|
|
35 |
extern float128_t STX_qcos(float128_t);
|
|
36 |
extern float128_t STX_qtan(float128_t);
|
|
37 |
extern float128_t STX_qsinh(float128_t);
|
|
38 |
extern float128_t STX_qcosh(float128_t);
|
|
39 |
extern float128_t STX_qtanh(float128_t);
|
|
40 |
extern float128_t STX_qasin(float128_t);
|
|
41 |
extern float128_t STX_qacos(float128_t);
|
|
42 |
extern float128_t STX_qatan(float128_t);
|
|
43 |
extern float128_t STX_qasinh(float128_t);
|
|
44 |
extern float128_t STX_qacosh(float128_t);
|
|
45 |
extern float128_t STX_qatanh(float128_t);
|
5266
|
46 |
extern float128_t STX_qZero;
|
|
47 |
extern float128_t STX_dbltoq(double);
|
|
48 |
extern float128_t STX_inttoq(long);
|
|
49 |
extern double STX_qtodbl(float128_t);
|
5267
|
50 |
extern int STX_qisNan(float128_t*);
|
|
51 |
extern int STX_qprcmp(float128_t*, float128_t*);
|
5013
|
52 |
|
5266
|
53 |
#define STX_qisfinite(q) (!STX_qisNan(&(q)) && !STX_qisInf(&(q)))
|
|
54 |
#define STX_qeq(x1, x2) (STX_qprcmp (&(x1), &(x2)) == 0)
|
|
55 |
#define STX_qneq(x1, x2) (STX_qprcmp (&(x1), &(x2)) != 0)
|
|
56 |
#define STX_qgt(x1, x2) (STX_qprcmp (&(x1), &(x2)) > 0)
|
|
57 |
#define STX_qge(x1, x2) (STX_qprcmp (&(x1), &(x2)) >= 0)
|
|
58 |
#define STX_qlt(x1, x2) (STX_qprcmp (&(x1), &(x2)) < 0)
|
|
59 |
#define STX_qle(x1, x2) (STX_qprcmp (&(x1), &(x2)) <= 0)
|
4992
|
60 |
|
5266
|
61 |
#ifdef __win32__
|
|
62 |
/*
|
|
63 |
* no finite(x) ?
|
|
64 |
* no isnan(x) ?
|
|
65 |
*/
|
|
66 |
# ifndef isnan
|
|
67 |
# define isnan(x) \
|
|
68 |
((((unsigned int *)(&x))[0] == 0x00000000) && \
|
|
69 |
(((unsigned int *)(&x))[1] == 0xFFF80000))
|
|
70 |
# endif
|
4992
|
71 |
|
5266
|
72 |
# ifndef isPositiveInfinity
|
|
73 |
# define isPositiveInfinity(x) \
|
|
74 |
((((unsigned int *)(&x))[0] == 0x00000000) && \
|
|
75 |
(((unsigned int *)(&x))[1] == 0x7FF00000))
|
|
76 |
# endif
|
4992
|
77 |
|
5266
|
78 |
# ifndef isNegativeInfinity
|
|
79 |
# define isNegativeInfinity(x) \
|
|
80 |
((((unsigned int *)(&x))[0] == 0x00000000) && \
|
|
81 |
(((unsigned int *)(&x))[1] == 0xFFF00000))
|
|
82 |
# endif
|
5014
|
83 |
|
5266
|
84 |
# ifndef isinf
|
|
85 |
# define isinf(x) \
|
|
86 |
((((unsigned int *)(&x))[0] == 0x00000000) && \
|
|
87 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000))
|
|
88 |
# endif
|
4992
|
89 |
|
5266
|
90 |
# ifndef isfinite
|
|
91 |
# define isfinite(x) (!isinf(x) && !isnan(x))
|
|
92 |
# endif
|
|
93 |
|
|
94 |
#else // not win32
|
4992
|
95 |
#endif
|
4993
|
96 |
|
4992
|
97 |
%}
|
|
98 |
! !
|
|
99 |
|
|
100 |
!QuadFloat primitiveVariables!
|
5267
|
101 |
%{
|
|
102 |
%}
|
4991
|
103 |
! !
|
|
104 |
|
4992
|
105 |
!QuadFloat primitiveFunctions!
|
5267
|
106 |
%{
|
|
107 |
%}
|
4992
|
108 |
! !
|
4991
|
109 |
|
4983
|
110 |
!QuadFloat class methodsFor:'documentation'!
|
|
111 |
|
|
112 |
documentation
|
|
113 |
"
|
|
114 |
QuadFloats represent rational numbers with limited precision
|
5007
|
115 |
and are mapped to IEEE quadruple precision format (128bit),
|
|
116 |
also called binary128.
|
|
117 |
|
4983
|
118 |
If the underlying cpu supports them natively, the machine format (long double) is
|
|
119 |
used. Otherwise, a software emulation is done, which is much slower.
|
|
120 |
Thus only use them, if you really need the additional precision;
|
|
121 |
if not, use Float (which are doubles) or LongFloats which usually have IEEE extended precision (80bit).
|
|
122 |
|
|
123 |
QuadFloats give you definite 128 bit quadruple floats,
|
|
124 |
thus, code using quadFloats is guaranteed to be portable from one architecture to another.
|
|
125 |
|
|
126 |
Representation:
|
4991
|
127 |
128bit quadruple IEEE floats (16bytes);
|
|
128 |
112 bit mantissa,
|
|
129 |
16 bit exponent,
|
|
130 |
34 decimal digits (approx.)
|
4983
|
131 |
|
4992
|
132 |
On Sparc CPUs, this is a native supported type (long double) and fast;
|
|
133 |
on x86 CPUs, this is emulated and slow.
|
|
134 |
|
4983
|
135 |
Mixed mode arithmetic:
|
5007
|
136 |
quadFloat op anyFloat -> quadFloat
|
|
137 |
anyFloat op quadFloat -> quadFloat
|
4983
|
138 |
|
|
139 |
Range and precision of storage formats: see LimitedPrecisionReal >> documentation
|
|
140 |
|
|
141 |
[author:]
|
4991
|
142 |
Claus Gittinger
|
4983
|
143 |
|
|
144 |
[see also:]
|
4991
|
145 |
Number
|
|
146 |
Float ShortFloat LongFloat Fraction FixedPoint Integer Complex
|
|
147 |
FloatArray DoubleArray
|
|
148 |
https://en.wikipedia.org/wiki/Extended_precision
|
4983
|
149 |
"
|
|
150 |
! !
|
|
151 |
|
|
152 |
!QuadFloat class methodsFor:'instance creation'!
|
|
153 |
|
|
154 |
basicNew
|
|
155 |
"return a new quadFloat - here we return 0.0
|
|
156 |
- QuadFloats are usually NOT created this way ...
|
|
157 |
Its implemented here to allow things like binary store & load
|
|
158 |
of quadFloats.
|
|
159 |
(but it is not a good idea to store the bits of a float - the reader might have a
|
|
160 |
totally different representation - so floats should be
|
|
161 |
binary stored in a device independent format)."
|
|
162 |
|
|
163 |
%{ /* NOCONTEXT */
|
4993
|
164 |
#ifdef SUPPORT_QUADFLOAT
|
4983
|
165 |
OBJ newFloat;
|
4991
|
166 |
|
|
167 |
if (sizeof(long double) == sizeof(float128_t)) {
|
|
168 |
__qMKLFLOAT(newFloat, 0.0); /* OBJECT ALLOCATION */
|
4983
|
169 |
} else {
|
5267
|
170 |
float128_t qf = STX_qZero;
|
4991
|
171 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
4983
|
172 |
}
|
|
173 |
RETURN (newFloat);
|
4993
|
174 |
#endif /* SUPPORT_QUADFLOAT */
|
|
175 |
%}.
|
|
176 |
self error:'QuadFloats not supported on this patform'
|
4983
|
177 |
|
|
178 |
"Created: / 06-06-2019 / 17:18:58 / Claus Gittinger"
|
|
179 |
!
|
|
180 |
|
|
181 |
fromFloat:aFloat
|
|
182 |
"return a new quadFloat, given a float value"
|
|
183 |
|
|
184 |
%{ /* NOCONTEXT */
|
4993
|
185 |
#ifdef SUPPORT_QUADFLOAT
|
4983
|
186 |
OBJ newFloat;
|
|
187 |
|
|
188 |
if (__isFloatLike(aFloat)) {
|
5267
|
189 |
double f = __floatVal(aFloat);
|
4991
|
190 |
float128_t qf;
|
|
191 |
|
5267
|
192 |
qf = STX_dbltoq (f);
|
4991
|
193 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
|
194 |
RETURN (newFloat);
|
4983
|
195 |
}
|
4993
|
196 |
#endif /* SUPPORT_QUADFLOAT */
|
4983
|
197 |
%}.
|
4993
|
198 |
aFloat isFloat ifTrue:[
|
4997
|
199 |
self errorUnsupported
|
4993
|
200 |
].
|
4997
|
201 |
ArgumentError raise
|
4983
|
202 |
|
|
203 |
"
|
|
204 |
QuadFloat fromFloat:123.0
|
|
205 |
123.0 asQuadFloat
|
|
206 |
123 asQuadFloat
|
|
207 |
"
|
|
208 |
|
|
209 |
"Created: / 06-06-2019 / 18:01:03 / Claus Gittinger"
|
5011
|
210 |
!
|
|
211 |
|
5017
|
212 |
fromInteger:anInteger
|
|
213 |
"return a new quadFloat, given an integer value"
|
|
214 |
|
|
215 |
%{ /* NOCONTEXT */
|
|
216 |
#ifdef SUPPORT_QUADFLOAT
|
|
217 |
OBJ newFloat;
|
|
218 |
|
|
219 |
if (__isSmallInteger(anInteger)) {
|
|
220 |
INT iVal = __intVal(anInteger);
|
|
221 |
float128_t qf;
|
|
222 |
|
5267
|
223 |
qf = STX_inttoq( (long)iVal );
|
5017
|
224 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
|
225 |
RETURN (newFloat);
|
|
226 |
}
|
|
227 |
#endif /* SUPPORT_QUADFLOAT */
|
|
228 |
%}.
|
|
229 |
^ super fromInteger:anInteger
|
|
230 |
|
|
231 |
"
|
|
232 |
QuadFloat fromInteger:123
|
|
233 |
123 asQuadFloat
|
|
234 |
"
|
|
235 |
!
|
|
236 |
|
5014
|
237 |
fromLongFloat:aFloat
|
|
238 |
"return a new quadFloat, given a long float value"
|
|
239 |
|
|
240 |
%{ /* NOCONTEXT */
|
5267
|
241 |
#ifdef xSUPPORT_QUADFLOAT
|
5014
|
242 |
OBJ newFloat;
|
|
243 |
union {
|
|
244 |
LONGFLOAT_t lf; // is long double
|
|
245 |
extFloat80_t ef; // is 80bit ext
|
|
246 |
float128_t qf; // is 128bit quad
|
|
247 |
} u;
|
|
248 |
|
|
249 |
if (__isLongFloat(aFloat)) {
|
|
250 |
u.lf = __longFloatVal(aFloat);
|
|
251 |
|
|
252 |
if (sizeof(LONGFLOAT_t) == 16) {
|
|
253 |
// longFloat is already 128 bits in size (sparc)
|
|
254 |
__qMKQFLOAT(newFloat, u.qf); /* OBJECT ALLOCATION */
|
|
255 |
RETURN (newFloat);
|
|
256 |
}
|
|
257 |
if (sizeof(LONGFLOAT_t) < 16) {
|
|
258 |
// assume 80bit extended float format (amd64, x86_64)
|
|
259 |
u.qf = extF80_to_f128( u.ef);
|
|
260 |
__qMKQFLOAT(newFloat, u.qf); /* OBJECT ALLOCATION */
|
|
261 |
RETURN (newFloat);
|
|
262 |
}
|
|
263 |
// fall into error case
|
|
264 |
}
|
|
265 |
#endif /* SUPPORT_QUADFLOAT */
|
|
266 |
%}.
|
|
267 |
aFloat isLongFloat ifTrue:[
|
|
268 |
self errorUnsupported
|
|
269 |
].
|
|
270 |
ArgumentError raise
|
|
271 |
|
|
272 |
"
|
|
273 |
QuadFloat fromLongFloat:123.0 asLongFloat
|
|
274 |
"
|
|
275 |
!
|
|
276 |
|
5011
|
277 |
fromShortFloat:aShortFloat
|
|
278 |
"return a new quadFloat, given a float value"
|
|
279 |
|
5013
|
280 |
^ self fromFloat:(aShortFloat asFloat)
|
5011
|
281 |
|
|
282 |
"
|
|
283 |
QuadFloat fromShortFloat:123.0 asShortFloat
|
|
284 |
"
|
|
285 |
|
|
286 |
"Created: / 08-06-2019 / 03:28:37 / Claus Gittinger"
|
4983
|
287 |
! !
|
|
288 |
|
5268
|
289 |
!QuadFloat class methodsFor:'accessing'!
|
|
290 |
|
|
291 |
defaultPrintFormat
|
|
292 |
"/ by default, I will print 19 digits
|
|
293 |
"/ ShortFloat pi -> 3.141593
|
|
294 |
"/ Float pi -> 3.14159265358979
|
|
295 |
"/ LongFloat pi -> 3.141592653589793239
|
|
296 |
"/ QuadFloat pi -> 3.141592653589793239
|
|
297 |
|
|
298 |
^ DefaultPrintFormat
|
|
299 |
! !
|
|
300 |
|
|
301 |
!QuadFloat class methodsFor:'class initialization'!
|
|
302 |
|
|
303 |
initialize
|
|
304 |
DefaultPrintFormat := '.34'. "/ 34 valid digits
|
|
305 |
DefaultPrintfFormat := '%34f'.
|
|
306 |
|
|
307 |
"
|
|
308 |
self initialize
|
|
309 |
"
|
|
310 |
|
|
311 |
"
|
|
312 |
DefaultPrintFormat := '.19'.
|
|
313 |
QuadFloat pi printString.
|
|
314 |
|
|
315 |
DefaultPrintFormat := '.9'.
|
|
316 |
QuadFloat pi printString.
|
|
317 |
|
|
318 |
DefaultPrintFormat := '.6'.
|
|
319 |
QuadFloat pi printString.
|
|
320 |
"
|
|
321 |
! !
|
|
322 |
|
4983
|
323 |
!QuadFloat class methodsFor:'coercing & converting'!
|
|
324 |
|
|
325 |
coerce:aNumber
|
|
326 |
"convert the argument aNumber into an instance of the receiver's class and return it."
|
|
327 |
|
|
328 |
^ aNumber asQuadFloat.
|
|
329 |
|
|
330 |
"Created: / 06-06-2019 / 16:51:01 / Claus Gittinger"
|
|
331 |
! !
|
|
332 |
|
|
333 |
!QuadFloat class methodsFor:'constants'!
|
|
334 |
|
|
335 |
NaN
|
5011
|
336 |
"return a quadFloat which represents not-a-Number (i.e. an invalid number)"
|
|
337 |
|
|
338 |
|nan|
|
4983
|
339 |
|
|
340 |
NaN isNil ifTrue:[
|
5011
|
341 |
%{ /* NOCONTEXT */
|
5267
|
342 |
#ifdef xSUPPORT_QUADFLOAT
|
5011
|
343 |
{
|
|
344 |
OBJ newFloat;
|
|
345 |
float128_t qf;
|
|
346 |
|
5014
|
347 |
softfloat_commonNaNToF128M( (uint32_t*)(&qf) );
|
5011
|
348 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
|
349 |
nan = newFloat;
|
|
350 |
}
|
|
351 |
#endif /* SUPPORT_QUADFLOAT */
|
|
352 |
%}.
|
|
353 |
NaN := nan
|
4983
|
354 |
].
|
|
355 |
^ NaN
|
|
356 |
!
|
|
357 |
|
|
358 |
e
|
|
359 |
"return the constant e as quadFloat"
|
|
360 |
|
|
361 |
E isNil ifTrue:[
|
4991
|
362 |
"/ eDigits has enough digits for 128bit IEEE quads
|
|
363 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here...
|
|
364 |
E := self readFrom:(Number eDigits)
|
4983
|
365 |
].
|
|
366 |
^ E
|
|
367 |
|
|
368 |
"Created: / 06-06-2019 / 17:01:54 / Claus Gittinger"
|
|
369 |
!
|
|
370 |
|
5017
|
371 |
infinity
|
|
372 |
"return a quadFloat which represents +INF"
|
|
373 |
|
|
374 |
|inf|
|
|
375 |
|
|
376 |
PositiveInfinity isNil ifTrue:[
|
|
377 |
%{ /* NOCONTEXT */
|
5267
|
378 |
#ifdef xSUPPORT_QUADFLOAT
|
5017
|
379 |
{
|
|
380 |
OBJ newFloat;
|
|
381 |
struct uint128 uiZ;
|
|
382 |
union ui128_f128 uZ;
|
|
383 |
float128_t qf;
|
|
384 |
|
|
385 |
uiZ.v64 = packToF128UI64( 0, 0x7FFF, 0 );
|
|
386 |
uiZ.v0 = 0;
|
|
387 |
uZ.ui = uiZ;
|
|
388 |
qf = uZ.f;
|
|
389 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
|
390 |
inf = newFloat;
|
|
391 |
}
|
|
392 |
#endif /* SUPPORT_QUADFLOAT */
|
|
393 |
%}.
|
|
394 |
PositiveInfinity := inf
|
|
395 |
].
|
|
396 |
^ PositiveInfinity
|
|
397 |
|
|
398 |
"Created: / 08-06-2019 / 14:05:26 / Claus Gittinger"
|
|
399 |
!
|
|
400 |
|
5012
|
401 |
negativeInfinity
|
|
402 |
"return a quadFloat which represents -INF"
|
|
403 |
|
|
404 |
|inf|
|
|
405 |
|
|
406 |
NegativeInfinity isNil ifTrue:[
|
|
407 |
%{ /* NOCONTEXT */
|
5267
|
408 |
#ifdef xSUPPORT_QUADFLOAT
|
5013
|
409 |
{
|
|
410 |
OBJ newFloat;
|
|
411 |
struct uint128 uiZ;
|
|
412 |
union ui128_f128 uZ;
|
|
413 |
float128_t qf;
|
5012
|
414 |
|
5013
|
415 |
uiZ.v64 = packToF128UI64( 1, 0x7FFF, 0 );
|
|
416 |
uiZ.v0 = 0;
|
|
417 |
uZ.ui = uiZ;
|
|
418 |
qf = uZ.f;
|
|
419 |
__qMKQFLOAT(newFloat, qf); /* OBJECT ALLOCATION */
|
|
420 |
inf = newFloat;
|
|
421 |
}
|
5012
|
422 |
#endif /* SUPPORT_QUADFLOAT */
|
|
423 |
%}.
|
5013
|
424 |
NegativeInfinity := inf
|
5012
|
425 |
].
|
|
426 |
^ NegativeInfinity
|
|
427 |
|
|
428 |
"Created: / 08-06-2019 / 14:05:50 / Claus Gittinger"
|
|
429 |
!
|
|
430 |
|
5264
|
431 |
phi
|
|
432 |
"return the constant phi as quadFloat"
|
|
433 |
|
|
434 |
Phi isNil ifTrue:[
|
5267
|
435 |
"/ phiDigits has enough digits for 128bit IEEE quads
|
|
436 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here...
|
|
437 |
Phi := self readFrom:(Number phiDigits)
|
5264
|
438 |
].
|
|
439 |
^ Phi
|
|
440 |
|
|
441 |
|
|
442 |
!
|
|
443 |
|
4983
|
444 |
pi
|
|
445 |
"return the constant pi as quadFloat"
|
|
446 |
|
|
447 |
Pi isNil ifTrue:[
|
4991
|
448 |
"/ piDigits has enough digits for 128bit IEEE quads
|
|
449 |
"/ do not use as a literal constant here - we cannot depend on the underlying C-compiler here...
|
|
450 |
Pi := self readFrom:(Number piDigits)
|
4983
|
451 |
].
|
|
452 |
^ Pi
|
|
453 |
|
|
454 |
"Created: / 06-06-2019 / 17:09:51 / Claus Gittinger"
|
5006
|
455 |
!
|
|
456 |
|
|
457 |
unity
|
|
458 |
"return the neutral element for multiplication (1.0) as QuadFloat"
|
|
459 |
|
|
460 |
QuadFloatOne isNil ifTrue:[
|
5007
|
461 |
QuadFloatOne := 1.0 asQuadFloat.
|
5006
|
462 |
].
|
|
463 |
^ QuadFloatOne
|
|
464 |
|
|
465 |
"Created: / 07-06-2019 / 03:26:38 / Claus Gittinger"
|
5008
|
466 |
!
|
|
467 |
|
|
468 |
zero
|
|
469 |
"return the neutral element for addition (0.0) as QuadFloat"
|
|
470 |
|
|
471 |
QuadFloatZero isNil ifTrue:[
|
5011
|
472 |
QuadFloatZero := 0.0 asQuadFloat
|
5008
|
473 |
].
|
|
474 |
^ QuadFloatZero
|
|
475 |
|
|
476 |
"Created: / 07-06-2019 / 09:22:56 / Claus Gittinger"
|
4983
|
477 |
! !
|
|
478 |
|
5003
|
479 |
!QuadFloat class methodsFor:'error reportng'!
|
4994
|
480 |
|
4997
|
481 |
errorUnsupported
|
|
482 |
self error:'QuadFloats not supported on this patform'
|
|
483 |
|
5003
|
484 |
"Created: / 07-06-2019 / 02:44:39 / Claus Gittinger"
|
4994
|
485 |
! !
|
|
486 |
|
5005
|
487 |
!QuadFloat class methodsFor:'queries'!
|
|
488 |
|
5017
|
489 |
epsilon
|
|
490 |
"return the maximum relative spacing of instances of mySelf
|
|
491 |
(i.e. the value-delta of the least significant bit)"
|
|
492 |
|
|
493 |
Epsilon isNil ifTrue:[
|
5020
|
494 |
Epsilon := self computeEpsilon.
|
5017
|
495 |
].
|
|
496 |
^ Epsilon
|
|
497 |
|
|
498 |
"
|
|
499 |
self epsilon
|
|
500 |
"
|
|
501 |
|
|
502 |
"Created: / 10-06-2019 / 21:21:18 / Claus Gittinger"
|
|
503 |
!
|
|
504 |
|
5018
|
505 |
exponentCharacter
|
|
506 |
"return the character used to print between mantissa an exponent.
|
|
507 |
Also used by the scanner when reading numbers."
|
|
508 |
|
|
509 |
^ $Q
|
|
510 |
|
|
511 |
"Created: / 10-06-2019 / 21:28:04 / Claus Gittinger"
|
|
512 |
!
|
|
513 |
|
5025
|
514 |
numBitsInExponent
|
|
515 |
"answer the number of bits in the exponent
|
|
516 |
the hidden bit is not counted here:
|
|
517 |
|
|
518 |
This is an 128bit quadfloat,
|
5027
|
519 |
where 15 bits are available in the exponent:
|
|
520 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
|
5025
|
521 |
"
|
|
522 |
|
|
523 |
^ 15
|
|
524 |
|
|
525 |
"
|
|
526 |
1.0 class numBitsInExponent -> 11
|
|
527 |
1.0 asShortFloat class numBitsInExponent -> 8
|
|
528 |
1.0 asLongFloat class numBitsInExponent -> 15
|
|
529 |
1.0 asQuadFloat class numBitsInExponent -> 15
|
|
530 |
"
|
|
531 |
|
|
532 |
"Created: / 11-06-2019 / 00:14:55 / Claus Gittinger"
|
|
533 |
!
|
|
534 |
|
5005
|
535 |
numBitsInMantissa
|
|
536 |
"answer the number of bits in the mantissa
|
|
537 |
the hidden bit is not counted here:
|
|
538 |
|
|
539 |
This is an 128bit quadfloat,
|
5007
|
540 |
where 112 bits are available in the mantissa:
|
|
541 |
seeeeeee eeeeeeee mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm...
|
5005
|
542 |
"
|
|
543 |
|
|
544 |
^ 112
|
|
545 |
|
|
546 |
"
|
|
547 |
1.0 class numBitsInMantissa
|
|
548 |
1.0 asShortFloat class numBitsInMantissa
|
|
549 |
1.0 asLongFloat class numBitsInMantissa
|
|
550 |
1.0 asQuadFloat class numBitsInMantissa
|
|
551 |
"
|
|
552 |
|
|
553 |
"Created: / 07-06-2019 / 03:24:20 / Claus Gittinger"
|
5056
|
554 |
!
|
|
555 |
|
|
556 |
radix
|
|
557 |
"answer the radix of a QuadFloat's exponent
|
|
558 |
This is an IEEE float, which is represented as binary"
|
|
559 |
|
|
560 |
^ 2 "must be careful here, whenever ST/X is used on VAX or a 370"
|
|
561 |
|
|
562 |
"Created: / 19-07-2019 / 17:28:00 / Claus Gittinger"
|
5005
|
563 |
! !
|
|
564 |
|
4994
|
565 |
!QuadFloat methodsFor:'arithmetic'!
|
|
566 |
|
|
567 |
* aNumber
|
|
568 |
"return the product of the receiver and the argument."
|
|
569 |
|
5074
|
570 |
aNumber class == QuadFloat ifTrue:[
|
5267
|
571 |
^ aNumber productFromQuadFloat:self
|
5074
|
572 |
].
|
|
573 |
|
5075
|
574 |
thisContext isReallyRecursive ifTrue:[self error].
|
4994
|
575 |
^ aNumber productFromQuadFloat:self
|
|
576 |
!
|
|
577 |
|
|
578 |
+ aNumber
|
|
579 |
"return the sum of the receiver and the argument, aNumber"
|
|
580 |
|
|
581 |
^ aNumber sumFromQuadFloat:self
|
|
582 |
!
|
|
583 |
|
|
584 |
- aNumber
|
|
585 |
"return the difference of the receiver and the argument, aNumber"
|
|
586 |
|
|
587 |
^ aNumber differenceFromQuadFloat:self
|
|
588 |
!
|
|
589 |
|
|
590 |
/ aNumber
|
|
591 |
"return the quotient of the receiver and the argument, aNumber"
|
|
592 |
|
|
593 |
aNumber isZero ifTrue:[
|
|
594 |
"
|
|
595 |
No, you shalt not divide by zero
|
|
596 |
"
|
|
597 |
^ ZeroDivide raiseRequestWith:thisContext.
|
|
598 |
].
|
|
599 |
^ aNumber quotientFromQuadFloat:self
|
|
600 |
!
|
|
601 |
|
5268
|
602 |
abs
|
|
603 |
"return the absolute value of the receiver
|
|
604 |
reimplemented here for speed"
|
5267
|
605 |
|
|
606 |
%{ /* NOCONTEXT */
|
|
607 |
#ifdef SUPPORT_QUADFLOAT
|
|
608 |
OBJ newFloat;
|
|
609 |
float128_t result, myVal;
|
|
610 |
|
|
611 |
myVal = __quadFloatVal(self);
|
5268
|
612 |
result = STX_qabs(myVal);
|
5267
|
613 |
__qMKQFLOAT(newFloat, result);
|
|
614 |
RETURN ( newFloat );
|
|
615 |
#endif
|
|
616 |
%}.
|
|
617 |
!
|
|
618 |
|
|
619 |
ceiling
|
|
620 |
"return the smallest integer which is greater or equal to the receiver."
|
|
621 |
|
|
622 |
|val|
|
|
623 |
|
|
624 |
%{
|
|
625 |
float128_t qVal;
|
|
626 |
float128_t qMinInt;
|
|
627 |
float128_t qMaxInt;
|
|
628 |
|
|
629 |
qVal = __quadFloatVal(self);
|
|
630 |
qVal = STX_qceil(qVal);
|
|
631 |
|
|
632 |
qMinInt = STX_dbltoq((double)_MIN_INT);
|
|
633 |
qMaxInt = STX_dbltoq((double)_MAX_INT);
|
|
634 |
if (STX_qge(qVal, qMinInt) && STX_qle(qVal, qMaxInt)) {
|
|
635 |
double dVal = STX_qtodbl(qVal);
|
|
636 |
RETURN ( __mkSmallInteger( (INT) dVal ) );
|
|
637 |
}
|
|
638 |
__qMKQFLOAT(val, qVal);
|
|
639 |
%}.
|
|
640 |
^ val asInteger
|
|
641 |
|
|
642 |
"
|
|
643 |
0.5 asQuadFloat ceiling
|
|
644 |
0.5 asQuadFloat ceilingAsFloat
|
|
645 |
-0.5 asQuadFloat ceiling
|
|
646 |
-0.5 asQuadFloat ceilingAsFloat
|
|
647 |
"
|
|
648 |
!
|
|
649 |
|
|
650 |
ceilingAsFloat
|
|
651 |
"return the smallest integer-valued float greater or equal to the receiver.
|
|
652 |
This is much like #ceiling, but avoids a (possibly expensive) conversion
|
|
653 |
of the result to an integer.
|
|
654 |
It may be useful, if the result is to be further used in another float-operation."
|
|
655 |
|
|
656 |
%{ /* NOCONTEXT */
|
|
657 |
#ifdef SUPPORT_QUADFLOAT
|
|
658 |
OBJ newFloat;
|
|
659 |
float128_t result, myVal;
|
|
660 |
|
|
661 |
myVal = __quadFloatVal(self);
|
|
662 |
result = STX_qceil(myVal);
|
|
663 |
__qMKQFLOAT(newFloat, result);
|
|
664 |
RETURN ( newFloat );
|
|
665 |
#endif
|
|
666 |
%}.
|
|
667 |
!
|
|
668 |
|
5268
|
669 |
cos
|
|
670 |
"return the cosine of the receiver (interpreted as radians)"
|
|
671 |
|
|
672 |
%{ /* NOCONTEXT */
|
|
673 |
#ifdef SUPPORT_QUADFLOAT
|
|
674 |
OBJ newFloat;
|
|
675 |
float128_t result, myVal;
|
|
676 |
|
|
677 |
myVal = __quadFloatVal(self);
|
|
678 |
result = STX_qcos(myVal);
|
|
679 |
__qMKQFLOAT(newFloat, result);
|
|
680 |
RETURN ( newFloat );
|
|
681 |
#endif
|
|
682 |
%}.
|
|
683 |
!
|
|
684 |
|
|
685 |
cosh
|
|
686 |
"return the hyperbolic cosine of the receiver (interpreted as radians)"
|
|
687 |
|
|
688 |
%{ /* NOCONTEXT */
|
|
689 |
#ifdef SUPPORT_QUADFLOAT
|
|
690 |
OBJ newFloat;
|
|
691 |
float128_t result, myVal;
|
|
692 |
|
|
693 |
myVal = __quadFloatVal(self);
|
|
694 |
result = STX_qcosh(myVal);
|
|
695 |
__qMKQFLOAT(newFloat, result);
|
|
696 |
RETURN ( newFloat );
|
|
697 |
#endif
|
|
698 |
%}.
|
|
699 |
!
|
|
700 |
|
|
701 |
exp
|
|
702 |
"return e raised to the power of the receiver"
|
|
703 |
|
|
704 |
%{ /* NOCONTEXT */
|
|
705 |
#ifdef SUPPORT_QUADFLOAT
|
|
706 |
OBJ newFloat;
|
|
707 |
float128_t result, myVal;
|
|
708 |
|
|
709 |
myVal = __quadFloatVal(self);
|
|
710 |
result = STX_qexp(myVal);
|
|
711 |
__qMKQFLOAT(newFloat, result);
|
|
712 |
RETURN ( newFloat );
|
|
713 |
#endif
|
|
714 |
%}.
|
|
715 |
!
|
|
716 |
|
5267
|
717 |
exponent
|
|
718 |
"extract a normalized float's exponent.
|
|
719 |
The returned value depends on the float-representation of
|
|
720 |
the underlying machine and is therefore highly unportable.
|
|
721 |
This is not for general use.
|
|
722 |
This assumes that the mantissa is normalized to
|
|
723 |
0.5 .. 1.0 and the float's value is: mantissa * 2^exp"
|
|
724 |
|
|
725 |
%{ /* NOCONTEXT */
|
|
726 |
float128_t myVal;
|
|
727 |
float128_t frac;
|
|
728 |
int exp;
|
|
729 |
|
|
730 |
myVal = __quadFloatVal(self);
|
|
731 |
#if 1
|
|
732 |
// should we?
|
|
733 |
if (! (STX_qisNan(&myVal) || STX_qisInf(&myVal)))
|
|
734 |
#endif
|
|
735 |
{
|
|
736 |
frac = STX_qfrexp(myVal, &exp);
|
|
737 |
RETURN (__mkSmallInteger(exp));
|
|
738 |
}
|
|
739 |
%}.
|
|
740 |
^ super exponent
|
|
741 |
|
|
742 |
"
|
|
743 |
1.0 exponent
|
|
744 |
1.0 asQuadFloat exponent
|
|
745 |
2.0 exponent
|
|
746 |
2.0 asQuadFloat exponent
|
|
747 |
3.0 exponent
|
|
748 |
3.0 asQuadFloat exponent
|
|
749 |
4.0 exponent
|
|
750 |
4.0 asQuadFloat exponent
|
|
751 |
0.5 exponent
|
|
752 |
0.5 asQuadFloat exponent
|
|
753 |
0.4 exponent
|
|
754 |
0.4 asQuadFloat exponent
|
|
755 |
0.25 exponent
|
|
756 |
0.25 asQuadFloat exponent
|
|
757 |
0.2 exponent
|
|
758 |
0.2 asQuadFloat exponent
|
|
759 |
0.00000011111 exponent
|
|
760 |
0.00000011111 asQuadFloat exponent
|
|
761 |
0.0 exponent
|
|
762 |
0.0 asQuadFloat exponent
|
|
763 |
|
|
764 |
1e1000 exponent -> error (INF)
|
|
765 |
1e1000 asQuadFloat exponent -> error (INF)
|
|
766 |
"
|
|
767 |
!
|
|
768 |
|
5268
|
769 |
floor
|
|
770 |
"return the integer nearest the receiver towards negative infinity."
|
|
771 |
|
|
772 |
|val|
|
|
773 |
|
|
774 |
%{
|
|
775 |
float128_t qVal;
|
|
776 |
float128_t qMinInt;
|
|
777 |
float128_t qMaxInt;
|
|
778 |
|
|
779 |
qVal = __quadFloatVal(self);
|
|
780 |
qVal = STX_qfloor(qVal);
|
|
781 |
|
|
782 |
qMinInt = STX_dbltoq((double)_MIN_INT);
|
|
783 |
qMaxInt = STX_dbltoq((double)_MAX_INT);
|
|
784 |
if (STX_qge(qVal, qMinInt) && STX_qle(qVal, qMaxInt)) {
|
|
785 |
double dVal = STX_qtodbl(qVal);
|
|
786 |
RETURN ( __mkSmallInteger( (INT) dVal ) );
|
|
787 |
}
|
|
788 |
__qMKQFLOAT(val, qVal);
|
|
789 |
%}.
|
|
790 |
^ val asInteger
|
|
791 |
|
|
792 |
"
|
|
793 |
0.5 asQuadFloat floor
|
|
794 |
0.5 asQuadFloat floorAsFloat
|
|
795 |
-0.5 asQuadFloat floor
|
|
796 |
-0.5 asQuadFloat floorAsFloat
|
|
797 |
"
|
|
798 |
!
|
|
799 |
|
|
800 |
floorAsFloat
|
|
801 |
"return the integer nearest the receiver towards negative infinity as a float.
|
|
802 |
This is much like #floor, but avoids a (possibly expensive) conversion
|
|
803 |
of the result to an integer.
|
|
804 |
It may be useful, if the result is to be further used in another float-operation."
|
|
805 |
|
|
806 |
%{ /* NOCONTEXT */
|
|
807 |
#ifdef SUPPORT_QUADFLOAT
|
|
808 |
OBJ newFloat;
|
|
809 |
float128_t result, myVal;
|
|
810 |
|
|
811 |
myVal = __quadFloatVal(self);
|
|
812 |
result = STX_qfloor(myVal);
|
|
813 |
__qMKQFLOAT(newFloat, result);
|
|
814 |
RETURN ( newFloat );
|
|
815 |
#endif
|
|
816 |
%}.
|
|
817 |
!
|
|
818 |
|
|
819 |
ln
|
|
820 |
"return natural logarithm of the receiver."
|
|
821 |
|
|
822 |
%{ /* NOCONTEXT */
|
|
823 |
#ifdef SUPPORT_QUADFLOAT
|
|
824 |
OBJ newFloat;
|
|
825 |
float128_t result, myVal;
|
|
826 |
|
|
827 |
myVal = __quadFloatVal(self);
|
|
828 |
result = STX_qlog(myVal);
|
|
829 |
__qMKQFLOAT(newFloat, result);
|
|
830 |
RETURN ( newFloat );
|
|
831 |
#endif
|
|
832 |
%}.
|
|
833 |
!
|
|
834 |
|
|
835 |
log
|
|
836 |
"return log base 10 of the receiver.
|
|
837 |
Alias for log:10."
|
|
838 |
|
|
839 |
%{ /* NOCONTEXT */
|
|
840 |
#ifdef SUPPORT_QUADFLOAT
|
|
841 |
OBJ newFloat;
|
|
842 |
float128_t result, myVal;
|
|
843 |
|
|
844 |
myVal = __quadFloatVal(self);
|
|
845 |
result = STX_qlog10(myVal);
|
|
846 |
__qMKQFLOAT(newFloat, result);
|
|
847 |
RETURN ( newFloat );
|
|
848 |
#endif
|
|
849 |
%}.
|
|
850 |
!
|
|
851 |
|
|
852 |
log2
|
|
853 |
"return logarithm dualis of the receiver."
|
|
854 |
|
|
855 |
%{ /* NOCONTEXT */
|
|
856 |
#ifdef SUPPORT_QUADFLOAT
|
|
857 |
OBJ newFloat;
|
|
858 |
float128_t result, myVal;
|
|
859 |
|
|
860 |
myVal = __quadFloatVal(self);
|
|
861 |
result = STX_qlog2(myVal);
|
|
862 |
__qMKQFLOAT(newFloat, result);
|
|
863 |
RETURN ( newFloat );
|
|
864 |
#endif
|
|
865 |
%}.
|
|
866 |
!
|
|
867 |
|
5267
|
868 |
mantissa
|
|
869 |
"extract a normalized float's mantissa.
|
|
870 |
The returned value depends on the float-representation of
|
|
871 |
the underlying machine and is therefore highly unportable.
|
|
872 |
This is not for general use.
|
|
873 |
This assumes that the mantissa is normalized to
|
|
874 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp"
|
|
875 |
|
|
876 |
%{ /* NOCONTEXT */
|
|
877 |
float128_t myVal;
|
|
878 |
float128_t frac;
|
|
879 |
int exp;
|
|
880 |
OBJ newFloat;
|
|
881 |
|
|
882 |
myVal = __quadFloatVal(self);
|
|
883 |
// ouch: math libs seem to not care for NaN here;
|
|
884 |
#if 1
|
|
885 |
// should we?
|
|
886 |
if (! (STX_qisNan(&myVal) || STX_qisInf(&myVal)))
|
|
887 |
#endif
|
|
888 |
{
|
|
889 |
frac = STX_qfrexp(myVal, &exp);
|
|
890 |
__qMKQFLOAT(newFloat, frac);
|
|
891 |
RETURN ( newFloat );
|
|
892 |
}
|
|
893 |
%}.
|
|
894 |
^ super mantissa
|
|
895 |
|
|
896 |
"
|
|
897 |
1.0 exponent
|
|
898 |
1.0 asQuadFloat exponent
|
|
899 |
1.0 mantissa
|
|
900 |
1.0 asQuadFloat mantissa
|
|
901 |
|
|
902 |
0.25 exponent
|
|
903 |
0.25 asQuadFloat exponent
|
|
904 |
0.25 mantissa
|
|
905 |
0.25 asQuadFloat mantissa
|
|
906 |
|
|
907 |
0.00000011111 exponent
|
|
908 |
0.00000011111 mantissa
|
|
909 |
|
|
910 |
1e1000 mantissa
|
|
911 |
"
|
|
912 |
|
|
913 |
"Modified: / 20-06-2017 / 11:37:13 / cg"
|
|
914 |
"Modified (comment): / 26-05-2019 / 03:12:55 / Claus Gittinger"
|
|
915 |
!
|
|
916 |
|
5021
|
917 |
negated
|
|
918 |
"return the receiver negated"
|
|
919 |
|
|
920 |
%{ /* NOCONTEXT */
|
|
921 |
#ifdef SUPPORT_QUADFLOAT
|
|
922 |
OBJ newFloat;
|
|
923 |
float128_t result, myVal;
|
|
924 |
|
|
925 |
myVal = __quadFloatVal(self);
|
5267
|
926 |
result = STX_qneg(myVal);
|
5021
|
927 |
__qMKQFLOAT(newFloat, result);
|
|
928 |
RETURN ( newFloat );
|
|
929 |
#endif
|
|
930 |
%}.
|
5025
|
931 |
!
|
|
932 |
|
|
933 |
rem: aNumber
|
|
934 |
"return the floating point remainder of the receiver and the argument, aNumber"
|
|
935 |
|
|
936 |
aNumber isZero ifTrue:[
|
|
937 |
"
|
|
938 |
No, you shalt not divide by zero
|
|
939 |
"
|
|
940 |
^ ZeroDivide raiseRequestWith:thisContext.
|
|
941 |
].
|
|
942 |
^ aNumber remainderFromLongFloat:self
|
5268
|
943 |
!
|
|
944 |
|
|
945 |
sin
|
|
946 |
"return the sine of the receiver (interpreted as radians)"
|
|
947 |
|
|
948 |
%{ /* NOCONTEXT */
|
|
949 |
#ifdef SUPPORT_QUADFLOAT
|
|
950 |
OBJ newFloat;
|
|
951 |
float128_t result, myVal;
|
|
952 |
|
|
953 |
myVal = __quadFloatVal(self);
|
|
954 |
result = STX_qsin(myVal);
|
|
955 |
__qMKQFLOAT(newFloat, result);
|
|
956 |
RETURN ( newFloat );
|
|
957 |
#endif
|
|
958 |
%}.
|
|
959 |
!
|
|
960 |
|
|
961 |
sinh
|
|
962 |
"return the hyperbolic sine of the receiver (interpreted as radians)"
|
|
963 |
|
|
964 |
%{ /* NOCONTEXT */
|
|
965 |
#ifdef SUPPORT_QUADFLOAT
|
|
966 |
OBJ newFloat;
|
|
967 |
float128_t result, myVal;
|
|
968 |
|
|
969 |
myVal = __quadFloatVal(self);
|
|
970 |
result = STX_qsinh(myVal);
|
|
971 |
__qMKQFLOAT(newFloat, result);
|
|
972 |
RETURN ( newFloat );
|
|
973 |
#endif
|
|
974 |
%}.
|
|
975 |
!
|
|
976 |
|
|
977 |
tan
|
|
978 |
"return the tangent of the receiver (interpreted as radians)"
|
|
979 |
|
|
980 |
%{ /* NOCONTEXT */
|
|
981 |
#ifdef SUPPORT_QUADFLOAT
|
|
982 |
OBJ newFloat;
|
|
983 |
float128_t result, myVal;
|
|
984 |
|
|
985 |
myVal = __quadFloatVal(self);
|
|
986 |
result = STX_qtan(myVal);
|
|
987 |
__qMKQFLOAT(newFloat, result);
|
|
988 |
RETURN ( newFloat );
|
|
989 |
#endif
|
|
990 |
%}.
|
|
991 |
!
|
|
992 |
|
|
993 |
tanh
|
|
994 |
"return the hyperbolic tangent of the receiver (interpreted as radians)"
|
|
995 |
|
|
996 |
%{ /* NOCONTEXT */
|
|
997 |
#ifdef SUPPORT_QUADFLOAT
|
|
998 |
OBJ newFloat;
|
|
999 |
float128_t result, myVal;
|
|
1000 |
|
|
1001 |
myVal = __quadFloatVal(self);
|
|
1002 |
result = STX_qtanh(myVal);
|
|
1003 |
__qMKQFLOAT(newFloat, result);
|
|
1004 |
RETURN ( newFloat );
|
|
1005 |
#endif
|
|
1006 |
%}.
|
4994
|
1007 |
! !
|
|
1008 |
|
5008
|
1009 |
!QuadFloat methodsFor:'coercing & converting'!
|
|
1010 |
|
5267
|
1011 |
asFloat
|
|
1012 |
"return a Float with same value as the receiver.
|
|
1013 |
Raises an error if the receiver exceeds the float range."
|
|
1014 |
|
|
1015 |
%{ /* NOCONTEXT */
|
|
1016 |
|
|
1017 |
OBJ newFloat;
|
|
1018 |
float128_t qVal = __quadFloatVal(self);
|
|
1019 |
double dVal = STX_qtodbl(qVal);
|
|
1020 |
|
|
1021 |
if (isfinite(dVal) || !STX_qisfinite(qVal)) {
|
|
1022 |
__qMKFLOAT(newFloat, dVal);
|
|
1023 |
RETURN ( newFloat );
|
|
1024 |
}
|
|
1025 |
%}.
|
|
1026 |
"
|
|
1027 |
value out of range
|
|
1028 |
if you need -INF for a zero receiver, try Number trapInfinity:[...]
|
|
1029 |
"
|
|
1030 |
^ self class
|
|
1031 |
raise:#infiniteResultSignal
|
|
1032 |
receiver:self
|
|
1033 |
selector:#asFloat
|
|
1034 |
arguments:#()
|
|
1035 |
errorString:'receiver is out of the double-precision float range'
|
|
1036 |
|
|
1037 |
"
|
|
1038 |
1.0 asQuadFloat asFloat
|
|
1039 |
"
|
|
1040 |
!
|
|
1041 |
|
5008
|
1042 |
generality
|
|
1043 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
1044 |
|
|
1045 |
^ 93
|
|
1046 |
|
|
1047 |
"Created: / 07-06-2019 / 09:30:58 / Claus Gittinger"
|
|
1048 |
! !
|
|
1049 |
|
|
1050 |
!QuadFloat methodsFor:'comparing'!
|
|
1051 |
|
|
1052 |
< aNumber
|
|
1053 |
"return true, if the argument is greater"
|
|
1054 |
|
|
1055 |
^ aNumber lessFromQuadFloat:self
|
|
1056 |
|
|
1057 |
"Created: / 07-06-2019 / 09:25:47 / Claus Gittinger"
|
|
1058 |
!
|
|
1059 |
|
|
1060 |
= aNumber
|
|
1061 |
"return true, if the argument represents the same numeric value
|
|
1062 |
as the receiver, false otherwise"
|
|
1063 |
|
|
1064 |
^ aNumber equalFromQuadFloat:self
|
|
1065 |
|
|
1066 |
"Created: / 07-06-2019 / 09:25:27 / Claus Gittinger"
|
|
1067 |
!
|
|
1068 |
|
|
1069 |
hash
|
|
1070 |
"return a number for hashing; redefined, since floats compare
|
|
1071 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same
|
|
1072 |
as 3 hash."
|
|
1073 |
|
|
1074 |
|i|
|
|
1075 |
|
|
1076 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[
|
5011
|
1077 |
i := self asInteger.
|
|
1078 |
self = i ifTrue:[
|
|
1079 |
^ i hash
|
|
1080 |
].
|
5008
|
1081 |
].
|
|
1082 |
|
|
1083 |
^ self asFloat hash
|
|
1084 |
|
|
1085 |
"
|
|
1086 |
1.2345 hash
|
|
1087 |
1.2345 asShortFloat hash
|
|
1088 |
1.2345 asLongFloat hash
|
|
1089 |
1.2345 asQuadFloat hash
|
5011
|
1090 |
|
5008
|
1091 |
1.0 hash
|
|
1092 |
1.0 asShortFloat hash
|
|
1093 |
1.0 asLongFloat hash
|
|
1094 |
1.0 asQuadFloat hash
|
5011
|
1095 |
|
5008
|
1096 |
0.5 asShortFloat hash
|
|
1097 |
0.5 asShortFloat hash
|
|
1098 |
0.5 asLongFloat hash
|
|
1099 |
0.5 asQuadFloat hash
|
|
1100 |
|
|
1101 |
0.25 asShortFloat hash
|
|
1102 |
0.25 asShortFloat hash
|
|
1103 |
0.25 asLongFloat hash
|
|
1104 |
0.25 asQuadFloat hash
|
|
1105 |
"
|
|
1106 |
|
|
1107 |
"Created: / 07-06-2019 / 09:28:07 / Claus Gittinger"
|
|
1108 |
! !
|
|
1109 |
|
5003
|
1110 |
!QuadFloat methodsFor:'double dispatching'!
|
|
1111 |
|
|
1112 |
differenceFromQuadFloat:aQuadFloat
|
|
1113 |
%{
|
|
1114 |
#ifdef SUPPORT_QUADFLOAT
|
|
1115 |
OBJ newFloat;
|
|
1116 |
float128_t result, myVal, argVal;
|
|
1117 |
|
|
1118 |
myVal = __quadFloatVal(self);
|
|
1119 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1120 |
result = STX_qadd( argVal, myVal, 1 );
|
5003
|
1121 |
__qMKQFLOAT(newFloat, result);
|
|
1122 |
RETURN ( newFloat );
|
|
1123 |
#endif // SUPPORT_QUADFLOAT
|
|
1124 |
%}.
|
|
1125 |
self errorUnsupported
|
|
1126 |
!
|
|
1127 |
|
5005
|
1128 |
equalFromQuadFloat:aQuadFloat
|
|
1129 |
"sent when aQuadFloat does not know how to compare agaist the receiver, self"
|
5003
|
1130 |
|
|
1131 |
%{
|
|
1132 |
#ifdef SUPPORT_QUADFLOAT
|
|
1133 |
OBJ newFloat;
|
|
1134 |
float128_t result, myVal, argVal;
|
|
1135 |
|
|
1136 |
myVal = __quadFloatVal(self);
|
|
1137 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1138 |
RETURN (STX_qeq(argVal, myVal) ? true : false);
|
5003
|
1139 |
#endif // SUPPORT_QUADFLOAT
|
|
1140 |
%}.
|
|
1141 |
self errorUnsupported
|
5011
|
1142 |
|
|
1143 |
"Modified: / 08-06-2019 / 13:31:48 / Claus Gittinger"
|
5004
|
1144 |
!
|
|
1145 |
|
5005
|
1146 |
lessFromQuadFloat:aQuadFloat
|
|
1147 |
"sent when aQuadFloat does not know how to compare agaist the receiver, self"
|
|
1148 |
|
|
1149 |
%{
|
|
1150 |
#ifdef SUPPORT_QUADFLOAT
|
|
1151 |
OBJ newFloat;
|
|
1152 |
float128_t result, myVal, argVal;
|
|
1153 |
|
|
1154 |
myVal = __quadFloatVal(self);
|
|
1155 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1156 |
RETURN (STX_qlt(argVal, myVal) ? true : false);
|
5005
|
1157 |
#endif // SUPPORT_QUADFLOAT
|
|
1158 |
%}.
|
|
1159 |
self errorUnsupported
|
|
1160 |
!
|
|
1161 |
|
5004
|
1162 |
productFromQuadFloat:aQuadFloat
|
|
1163 |
"sent when aQuadFloat does not know how to multiply the receiver, self"
|
|
1164 |
|
|
1165 |
%{
|
|
1166 |
#ifdef SUPPORT_QUADFLOAT
|
|
1167 |
OBJ newFloat;
|
|
1168 |
float128_t result, myVal, argVal;
|
|
1169 |
|
|
1170 |
myVal = __quadFloatVal(self);
|
|
1171 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1172 |
result = STX_qmul(myVal, argVal);
|
5004
|
1173 |
__qMKQFLOAT(newFloat, result);
|
|
1174 |
RETURN ( newFloat );
|
|
1175 |
#endif // SUPPORT_QUADFLOAT
|
|
1176 |
%}.
|
|
1177 |
self errorUnsupported
|
|
1178 |
!
|
|
1179 |
|
|
1180 |
quotientFromQuadFloat:aQuadFloat
|
|
1181 |
"sent when aQuadFloat does not know how to multiply the receiver, self"
|
|
1182 |
|
|
1183 |
%{
|
|
1184 |
#ifdef SUPPORT_QUADFLOAT
|
|
1185 |
OBJ newFloat;
|
|
1186 |
float128_t result, myVal, argVal;
|
|
1187 |
|
|
1188 |
myVal = __quadFloatVal(self);
|
|
1189 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1190 |
result = STX_qdiv(argVal, myVal);
|
5004
|
1191 |
__qMKQFLOAT(newFloat, result);
|
|
1192 |
RETURN ( newFloat );
|
|
1193 |
#endif // SUPPORT_QUADFLOAT
|
|
1194 |
%}.
|
|
1195 |
self errorUnsupported
|
|
1196 |
!
|
|
1197 |
|
5005
|
1198 |
sumFromQuadFloat:aQuadFloat
|
|
1199 |
"sent when aQuadFloat does not know how to add the receiver, self"
|
5004
|
1200 |
|
|
1201 |
%{
|
|
1202 |
#ifdef SUPPORT_QUADFLOAT
|
|
1203 |
OBJ newFloat;
|
|
1204 |
float128_t result, myVal, argVal;
|
|
1205 |
|
|
1206 |
myVal = __quadFloatVal(self);
|
|
1207 |
argVal = __quadFloatVal(aQuadFloat);
|
5267
|
1208 |
result = STX_qadd( myVal, argVal, 0 );
|
5005
|
1209 |
__qMKQFLOAT(newFloat, result);
|
5004
|
1210 |
RETURN ( newFloat );
|
|
1211 |
#endif // SUPPORT_QUADFLOAT
|
|
1212 |
%}.
|
|
1213 |
self errorUnsupported
|
5003
|
1214 |
! !
|
|
1215 |
|
|
1216 |
!QuadFloat methodsFor:'error reportng'!
|
|
1217 |
|
|
1218 |
errorUnsupported
|
|
1219 |
self class errorUnsupported
|
|
1220 |
|
|
1221 |
"Modified: / 07-06-2019 / 02:44:51 / Claus Gittinger"
|
|
1222 |
! !
|
|
1223 |
|
5025
|
1224 |
!QuadFloat methodsFor:'printing'!
|
|
1225 |
|
|
1226 |
printOn:aStream
|
5268
|
1227 |
PrintfScanf printf:'%e' on:aStream argument:self
|
5027
|
1228 |
|
5268
|
1229 |
"/ |mantissa exponent|
|
|
1230 |
"/
|
|
1231 |
"/ mantissa := self mantissa.
|
|
1232 |
"/ exponent := self exponent.
|
|
1233 |
"/
|
|
1234 |
"/ self exponent == 0 ifTrue:[
|
|
1235 |
"/ mantissa printOn:aStream.
|
|
1236 |
"/ aStream nextPutAll:'.0'.
|
|
1237 |
"/ ^ self
|
|
1238 |
"/ ].
|
|
1239 |
"/ mantissa == 0 ifTrue:[
|
|
1240 |
"/ "/ a zero mantissa is impossible - except for zero and a few others
|
|
1241 |
"/ exponent == 0 ifTrue:[ aStream nextPutAll:'0.0'. ^ self].
|
|
1242 |
"/ self == NaN ifTrue:[ aStream nextPutAll:'NAN'. ^ self ].
|
|
1243 |
"/ self == NegativeInfinity ifTrue:[ aStream nextPutAll:'-INF'. ^ self].
|
|
1244 |
"/ self == PositiveInfinity ifTrue:[ aStream nextPutAll:'INF'. ^ self].
|
|
1245 |
"/
|
|
1246 |
"/ self error:'invalid largeFloat' mayProceed:true.
|
|
1247 |
"/ aStream nextPutAll:'Invalid'. ^ self.
|
|
1248 |
"/ ].
|
|
1249 |
"/
|
|
1250 |
"/ exponent >= 0 ifTrue:[
|
|
1251 |
"/ (mantissa bitShift:exponent) printOn:aStream.
|
|
1252 |
"/ aStream nextPutAll:'.0'.
|
|
1253 |
"/ ^ self
|
|
1254 |
"/ ].
|
|
1255 |
"/ ((mantissa / (1 bitShift:exponent negated)) asFixedPoint:6) printOn:aStream.
|
5025
|
1256 |
|
|
1257 |
"Created: / 11-06-2019 / 00:13:00 / Claus Gittinger"
|
|
1258 |
! !
|
|
1259 |
|
5011
|
1260 |
!QuadFloat methodsFor:'queries'!
|
|
1261 |
|
|
1262 |
isFinite
|
5194
|
1263 |
"return true, if the receiver is a finite float (not NaN and not +/-INF)"
|
5011
|
1264 |
|
|
1265 |
%{ /* NOCONTEXT */
|
5016
|
1266 |
#ifdef SUPPORT_QUADFLOAT
|
5011
|
1267 |
float128_t myVal;
|
|
1268 |
|
|
1269 |
myVal = __quadFloatVal(self);
|
5267
|
1270 |
RETURN (STX_qisfinite(myVal) ? true : false);
|
5016
|
1271 |
#endif // SUPPORT_QUADFLOAT
|
5011
|
1272 |
%}.
|
|
1273 |
|
|
1274 |
"
|
5267
|
1275 |
1.0 isFinite
|
|
1276 |
self NaN isFinite
|
|
1277 |
self infinity isFinite
|
|
1278 |
self negativeInfinity isFinite
|
|
1279 |
(0.0 uncheckedDivide: 0.0) isFinite
|
|
1280 |
(1.0 uncheckedDivide: 0.0) isFinite
|
5020
|
1281 |
"
|
|
1282 |
!
|
|
1283 |
|
|
1284 |
isInfinite
|
5194
|
1285 |
"return true, if the receiver is an infinite float (+Inf or -Inf)."
|
5020
|
1286 |
|
|
1287 |
%{ /* NOCONTEXT */
|
|
1288 |
#ifdef SUPPORT_QUADFLOAT
|
|
1289 |
float128_t myVal;
|
|
1290 |
|
|
1291 |
myVal = __quadFloatVal(self);
|
5267
|
1292 |
RETURN (STX_qisInf(&myVal) ? true : false);
|
5020
|
1293 |
#endif // SUPPORT_QUADFLOAT
|
|
1294 |
%}.
|
|
1295 |
|
|
1296 |
"
|
5267
|
1297 |
1.0 asQuadFloat isFinite
|
|
1298 |
1.0 asQuadFloat isInfinite
|
|
1299 |
self NaN isFinite
|
|
1300 |
self NaN isInfinite
|
|
1301 |
self infinity isFinite
|
|
1302 |
self infinity isInfinite
|
|
1303 |
self negativeInfinity isFinite
|
|
1304 |
self negativeInfinity isInfinite
|
|
1305 |
(0.0 uncheckedDivide: 0.0) isFinite
|
|
1306 |
(1.0 uncheckedDivide: 0.0) isFinite
|
5011
|
1307 |
"
|
|
1308 |
!
|
|
1309 |
|
|
1310 |
isNaN
|
5194
|
1311 |
"return true, if the receiver is an invalid float (NaN - not a number).
|
|
1312 |
These are not created by ST/X float operations (they raise an exception);
|
|
1313 |
however, inline C-code could produce them."
|
5011
|
1314 |
|
|
1315 |
%{ /* NOCONTEXT */
|
5016
|
1316 |
#ifdef SUPPORT_QUADFLOAT
|
5011
|
1317 |
float128_t myVal;
|
|
1318 |
|
|
1319 |
myVal = __quadFloatVal(self);
|
5267
|
1320 |
RETURN (STX_qisNan(&myVal) ? true : false);
|
5016
|
1321 |
#endif // SUPPORT_QUADFLOAT
|
5011
|
1322 |
%}.
|
|
1323 |
|
|
1324 |
"
|
5267
|
1325 |
1.0 asQuadFloat isFinite
|
|
1326 |
self NaN isFinite
|
|
1327 |
self infinity isFinite
|
|
1328 |
(0.0 uncheckedDivide: 0.0) isFinite
|
|
1329 |
(1.0 uncheckedDivide: 0.0) isFinite
|
5011
|
1330 |
"
|
|
1331 |
! !
|
|
1332 |
|
5266
|
1333 |
!QuadFloat methodsFor:'testing'!
|
|
1334 |
|
|
1335 |
isQuadFloat
|
|
1336 |
"return true, if the receiver is some kind of quad floating point number (iee quad precision)"
|
|
1337 |
|
|
1338 |
^ true
|
|
1339 |
! !
|
|
1340 |
|
4983
|
1341 |
!QuadFloat class methodsFor:'documentation'!
|
|
1342 |
|
|
1343 |
version_CVS
|
|
1344 |
^ '$Header$'
|
|
1345 |
! !
|
5268
|
1346 |
|
|
1347 |
|
|
1348 |
QuadFloat initialize!
|