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