author | Stefan Vogel <sv@exept.de> |
Wed, 07 Jan 1998 13:03:15 +0100 | |
changeset 3147 | 60396c4b82eb |
parent 3139 | a4c5540f924a |
child 3158 | 8e6bec40f6b0 |
permissions | -rw-r--r-- |
1197 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
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 |
" |
|
12 |
||
13 |
||
14 |
||
15 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
|
16 |
instanceVariableNames:'' |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
17 |
classVariableNames:'' |
1197 | 18 |
poolDictionaries:'' |
19 |
category:'Magnitude-Numbers' |
|
20 |
! |
|
21 |
||
2394 | 22 |
!ShortFloat primitiveDefinitions! |
23 |
%{ |
|
24 |
||
25 |
/* |
|
26 |
* includes, defines, structure definitions |
|
27 |
* and typedefs come here. |
|
28 |
*/ |
|
29 |
||
30 |
#if defined (_AIX) |
|
31 |
# include <float.h> |
|
32 |
#endif |
|
2395 | 33 |
#if defined(IRIX) |
34 |
# include <nan.h> |
|
35 |
#endif |
|
2397 | 36 |
#if defined(LINUX) |
37 |
# include <nan.h> |
|
38 |
#endif |
|
2400 | 39 |
#if defined(solaris) || defined(sunos) |
2398 | 40 |
# include <nan.h> |
41 |
#endif |
|
2394 | 42 |
|
43 |
%} |
|
44 |
! ! |
|
45 |
||
1880 | 46 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 47 |
|
48 |
copyright |
|
49 |
" |
|
50 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
51 |
All Rights Reserved |
|
52 |
||
53 |
This software is furnished under a license and may be used |
|
54 |
only in accordance with the terms of that license and with the |
|
55 |
inclusion of the above copyright notice. This software may not |
|
56 |
be provided or otherwise made available to, or used by, any |
|
57 |
other person. No title to or ownership of the software is |
|
58 |
hereby transferred. |
|
59 |
" |
|
60 |
||
61 |
||
62 |
! |
|
63 |
||
64 |
documentation |
|
65 |
" |
|
66 |
ShortFloats represent rational numbers with limited precision. In ST/X, Float uses |
|
67 |
the underlying C-compilers double implementation, while ShortFloats are |
|
68 |
mapped onto C-floats. |
|
69 |
Therefore instances of Float are usually represented by the 8-byte IEE |
|
70 |
double precision float format, while ShortFloats use 4byte IEE format. |
|
71 |
(but there is no guaranty). |
|
72 |
||
73 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
74 |
ST-80's Floats respectively. |
|
75 |
This may change in one of the next versions (at least on machines, which |
|
76 |
provide different float and double types in their C-compiler. |
|
77 |
||
78 |
WARNING: |
|
79 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
|
80 |
you may not add instance variables here. |
|
81 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
82 |
and does its float-checks by an identity compare with the ShortFloat-class. |
|
83 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
84 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
85 |
||
86 |
This may be changed, to use a flag bit in the class. |
|
1295 | 87 |
|
88 |
[author:] |
|
89 |
Claus Gittinger |
|
1556 | 90 |
|
91 |
[see also:] |
|
92 |
Number |
|
1893 | 93 |
Float Fraction FixedPoint Integer |
1197 | 94 |
" |
95 |
||
96 |
! ! |
|
97 |
||
1880 | 98 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 99 |
|
100 |
basicNew |
|
101 |
"return a new shortFloat - here we return 0.0 |
|
102 |
- shortFloats are usually NOT created this way ... |
|
103 |
Its implemented here to allow things like binary store & load |
|
104 |
of shortFloats. (but even this support will go away eventually, its not |
|
105 |
a good idea to store the bits of a float - the reader might have a |
|
106 |
totally different representation - so floats will eventually be |
|
107 |
binary stored in a device independent format." |
|
108 |
||
109 |
%{ /* NOCONTEXT */ |
|
110 |
OBJ newFloat; |
|
111 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
112 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 113 |
RETURN (newFloat); |
114 |
%} |
|
115 |
! ! |
|
116 |
||
1880 | 117 |
!ShortFloat class methodsFor:'constants'! |
1197 | 118 |
|
119 |
pi |
|
1260 | 120 |
"return the constant pi as ShortFloat" |
1197 | 121 |
|
122 |
^ 3.14159 asShortFloat |
|
123 |
||
1260 | 124 |
"Modified: 23.4.1996 / 09:26:31 / cg" |
1197 | 125 |
! |
126 |
||
127 |
unity |
|
1260 | 128 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 129 |
|
130 |
^ 1.0 asShortFloat |
|
131 |
||
1260 | 132 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 133 |
! |
134 |
||
135 |
zero |
|
1260 | 136 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 137 |
|
138 |
^ 0.0 asShortFloat |
|
139 |
||
1260 | 140 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 141 |
! ! |
142 |
||
1880 | 143 |
!ShortFloat class methodsFor:'queries'! |
1197 | 144 |
|
145 |
isBuiltInClass |
|
1264 | 146 |
"return true if this class is known by the run-time-system. |
147 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 148 |
|
149 |
^ self == ShortFloat |
|
150 |
||
1264 | 151 |
"Modified: 23.4.1996 / 16:00:23 / cg" |
1197 | 152 |
! ! |
153 |
||
154 |
!ShortFloat methodsFor:'arithmetic'! |
|
155 |
||
156 |
* aNumber |
|
157 |
"return the product of the receiver and the argument, aNumber" |
|
158 |
||
159 |
%{ /* NOCONTEXT */ |
|
160 |
||
161 |
OBJ newFloat; |
|
162 |
float result; |
|
163 |
double dResult; |
|
164 |
||
165 |
if (__isSmallInteger(aNumber)) { |
|
166 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
|
167 |
retResult: |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
168 |
__qMKSFLOAT(newFloat, result); |
1197 | 169 |
RETURN ( newFloat ); |
170 |
} |
|
171 |
if (__isShortFloat(aNumber)) { |
|
172 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
|
173 |
goto retResult; |
|
174 |
} |
|
175 |
if (__isFloatLike(aNumber)) { |
|
176 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
177 |
__qMKFLOAT(newFloat, dResult); |
1197 | 178 |
RETURN ( newFloat ); |
179 |
||
180 |
} |
|
181 |
%}. |
|
182 |
^ aNumber productFromShortFloat:self |
|
183 |
! |
|
184 |
||
185 |
+ aNumber |
|
186 |
"return the sum of the receiver and the argument, aNumber" |
|
187 |
||
188 |
%{ /* NOCONTEXT */ |
|
189 |
||
190 |
OBJ newFloat; |
|
191 |
float result; |
|
192 |
double dResult; |
|
193 |
||
194 |
if (__isSmallInteger(aNumber)) { |
|
195 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
|
196 |
retResult: |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
197 |
__qMKSFLOAT(newFloat, result); |
1197 | 198 |
RETURN ( newFloat ); |
199 |
} |
|
200 |
if (__isShortFloat(aNumber)) { |
|
201 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
|
202 |
goto retResult; |
|
203 |
} |
|
204 |
if (__isFloatLike(aNumber)) { |
|
205 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
206 |
__qMKFLOAT(newFloat, dResult); |
1197 | 207 |
RETURN ( newFloat ); |
208 |
||
209 |
} |
|
210 |
%}. |
|
211 |
^ aNumber sumFromShortFloat:self |
|
212 |
! |
|
213 |
||
214 |
- aNumber |
|
215 |
"return the difference of the receiver and the argument, aNumber" |
|
216 |
||
217 |
%{ /* NOCONTEXT */ |
|
218 |
||
219 |
OBJ newFloat; |
|
220 |
float result; |
|
221 |
double dResult; |
|
222 |
||
223 |
if (__isSmallInteger(aNumber)) { |
|
224 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
|
225 |
retResult: |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
226 |
__qMKSFLOAT(newFloat, result); |
1197 | 227 |
RETURN ( newFloat ); |
228 |
} |
|
229 |
if (__isShortFloat(aNumber)) { |
|
230 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
|
231 |
goto retResult; |
|
232 |
} |
|
233 |
if (__isFloatLike(aNumber)) { |
|
234 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
235 |
__qMKFLOAT(newFloat, dResult); |
1197 | 236 |
RETURN ( newFloat ); |
237 |
||
238 |
} |
|
239 |
%}. |
|
240 |
^ aNumber differenceFromShortFloat:self |
|
241 |
! |
|
242 |
||
243 |
/ aNumber |
|
244 |
"return the quotient of the receiver and the argument, aNumber" |
|
245 |
||
246 |
%{ /* NOCONTEXT */ |
|
247 |
||
248 |
OBJ newFloat; |
|
249 |
float result, val; |
|
250 |
double dResult, dVal; |
|
251 |
||
252 |
if (__isSmallInteger(aNumber)) { |
|
253 |
if (aNumber != __MKSMALLINT(0)) { |
|
254 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
255 |
retResult: |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
256 |
__qMKSFLOAT(newFloat, result); |
1197 | 257 |
RETURN ( newFloat ); |
258 |
} |
|
259 |
} |
|
260 |
if (__isShortFloat(aNumber)) { |
|
261 |
val = __shortFloatVal(aNumber); |
|
262 |
if (val != 0.0) { |
|
263 |
result = __shortFloatVal(self) / val; |
|
264 |
goto retResult; |
|
265 |
} |
|
266 |
} |
|
267 |
if (__isFloatLike(aNumber)) { |
|
268 |
dVal = __floatVal(aNumber); |
|
269 |
if (dVal != 0.0) { |
|
270 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
271 |
__qMKFLOAT(newFloat, dResult); |
1197 | 272 |
} |
273 |
RETURN ( newFloat ); |
|
274 |
||
275 |
} |
|
276 |
%}. |
|
277 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
278 |
" |
|
279 |
No, you shalt not divide by zero |
|
280 |
" |
|
281 |
^ DivisionByZeroSignal raise. |
|
282 |
]. |
|
283 |
^ aNumber quotientFromFloat:self |
|
284 |
||
285 |
! |
|
286 |
||
287 |
negated |
|
288 |
"return myself negated" |
|
289 |
||
290 |
%{ /* NOCONTEXT */ |
|
291 |
OBJ newFloat; |
|
292 |
float rslt = - __shortFloatVal(self); |
|
293 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
294 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 295 |
RETURN ( newFloat ); |
296 |
%} |
|
297 |
||
3147 | 298 |
! |
299 |
||
300 |
uncheckedDivide:aNumber |
|
301 |
"return the quotient of the receiver and the argument, aNumber |
|
302 |
Do not check for divide by zero (return NaN or infinity)" |
|
303 |
||
304 |
%{ /* NOCONTEXT */ |
|
305 |
||
306 |
OBJ newFloat; |
|
307 |
float result, val; |
|
308 |
double dResult, dVal; |
|
309 |
||
310 |
if (__isSmallInteger(aNumber)) { |
|
311 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
312 |
retResult: |
|
313 |
__qMKSFLOAT(newFloat, result); |
|
314 |
RETURN ( newFloat ); |
|
315 |
} |
|
316 |
if (__isShortFloat(aNumber)) { |
|
317 |
val = __shortFloatVal(aNumber); |
|
318 |
result = __shortFloatVal(self) / val; |
|
319 |
goto retResult; |
|
320 |
} |
|
321 |
if (__isFloatLike(aNumber)) { |
|
322 |
dVal = __floatVal(aNumber); |
|
323 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
324 |
__qMKFLOAT(newFloat, dResult); |
|
325 |
RETURN ( newFloat ); |
|
326 |
} |
|
327 |
%}. |
|
328 |
^ aNumber quotientFromFloat:self |
|
329 |
||
330 |
" |
|
331 |
0.0 asShortFloat uncheckedDivide:0 |
|
332 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
333 |
" |
|
334 |
||
1197 | 335 |
! ! |
336 |
||
337 |
!ShortFloat methodsFor:'coercion and converting'! |
|
338 |
||
3014 | 339 |
asDouble |
340 |
"ST80 compatibility - return a Float with same value as the receiver" |
|
341 |
||
342 |
^ self asFloat |
|
343 |
! |
|
344 |
||
1197 | 345 |
asFloat |
346 |
"return a Float with same value as the receiver" |
|
347 |
||
348 |
%{ /* NOCONTEXT */ |
|
349 |
||
350 |
OBJ newFloat; |
|
351 |
double dVal = (double)__shortFloatVal(self); |
|
352 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
353 |
__qMKFLOAT(newFloat, dVal); |
1197 | 354 |
RETURN ( newFloat ); |
355 |
%} |
|
356 |
||
357 |
" |
|
1583 | 358 |
1.0 asShortFloat asFloat |
1197 | 359 |
" |
360 |
! |
|
361 |
||
362 |
asInteger |
|
363 |
"return an integer with same value - might truncate" |
|
364 |
||
365 |
%{ /* NOCONTEXT */ |
|
1670 | 366 |
float fVal; |
1197 | 367 |
|
1670 | 368 |
fVal = __shortFloatVal(self); |
369 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
370 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
|
1197 | 371 |
} |
372 |
%}. |
|
373 |
^ super asInteger |
|
374 |
||
375 |
" |
|
376 |
12345.0 asShortFloat asInteger |
|
377 |
1e15 asShortFloat asInteger |
|
378 |
" |
|
379 |
! |
|
380 |
||
381 |
asShortFloat |
|
382 |
"return a ShortFloat with same value as the receiver - thats me" |
|
383 |
||
384 |
^ self |
|
1200 | 385 |
! |
386 |
||
387 |
generality |
|
388 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
389 |
||
390 |
^ 70 |
|
391 |
||
392 |
||
1197 | 393 |
! ! |
394 |
||
395 |
!ShortFloat methodsFor:'comparing'! |
|
396 |
||
397 |
< aNumber |
|
398 |
"return true, if the argument is greater" |
|
399 |
||
400 |
%{ /* NOCONTEXT */ |
|
401 |
||
402 |
if (__isSmallInteger(aNumber)) { |
|
403 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
|
404 |
} |
|
405 |
if (__isFloatLike(aNumber)) { |
|
406 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
407 |
} |
|
408 |
if (__isShortFloat(aNumber)) { |
|
409 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
410 |
} |
|
411 |
%}. |
|
412 |
^ aNumber lessFromShortFloat:self |
|
413 |
||
1200 | 414 |
" |
415 |
1.0 asShortFloat > (1/3) |
|
416 |
" |
|
1197 | 417 |
! |
418 |
||
419 |
<= aNumber |
|
420 |
"return true, if the argument is greater or equal" |
|
421 |
||
422 |
%{ /* NOCONTEXT */ |
|
423 |
||
424 |
if (__isSmallInteger(aNumber)) { |
|
425 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
|
426 |
} |
|
427 |
if (__isFloatLike(aNumber)) { |
|
428 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
429 |
} |
|
430 |
if (__isShortFloat(aNumber)) { |
|
431 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
432 |
} |
|
433 |
%}. |
|
434 |
^ self retry:#<= coercing:aNumber |
|
435 |
||
436 |
! |
|
437 |
||
438 |
= aNumber |
|
439 |
"return true, if the arguments value are equal by value" |
|
440 |
||
441 |
%{ /* NOCONTEXT */ |
|
442 |
||
443 |
if (__isSmallInteger(aNumber)) { |
|
444 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
|
445 |
} |
|
446 |
if (__isFloatLike(aNumber)) { |
|
447 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
|
448 |
} |
|
449 |
if (__isShortFloat(aNumber)) { |
|
450 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
|
451 |
} |
|
452 |
%}. |
|
453 |
^ self retry:#= coercing:aNumber |
|
454 |
||
455 |
! |
|
456 |
||
457 |
> aNumber |
|
458 |
"return true, if the argument is less" |
|
459 |
||
460 |
%{ /* NOCONTEXT */ |
|
461 |
||
462 |
if (__isSmallInteger(aNumber)) { |
|
463 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
|
464 |
} |
|
465 |
if (__isFloatLike(aNumber)) { |
|
466 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
467 |
} |
|
468 |
if (__isShortFloat(aNumber)) { |
|
469 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
470 |
} |
|
471 |
%}. |
|
472 |
^ self retry:#> coercing:aNumber |
|
473 |
! |
|
474 |
||
475 |
>= aNumber |
|
476 |
"return true, if the argument is less or equal" |
|
477 |
||
478 |
%{ /* NOCONTEXT */ |
|
479 |
||
480 |
if (__isSmallInteger(aNumber)) { |
|
481 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
|
482 |
} |
|
483 |
if (__isFloatLike(aNumber)) { |
|
484 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
485 |
} |
|
486 |
if (__isShortFloat(aNumber)) { |
|
487 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
488 |
} |
|
489 |
%}. |
|
490 |
^ self retry:#>= coercing:aNumber |
|
491 |
! |
|
492 |
||
493 |
hash |
|
494 |
"return a number for hashing; redefined, since floats compare |
|
495 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
496 |
as 3 hash." |
|
497 |
||
498 |
|i| |
|
499 |
||
500 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
501 |
i := self asInteger. |
|
502 |
self = i ifTrue:[ |
|
503 |
^ i hash |
|
504 |
]. |
|
505 |
]. |
|
506 |
||
507 |
" |
|
508 |
mhmh take some of my value-bits to hash on |
|
509 |
" |
|
510 |
^ (((self basicAt:4) bitAnd:16r3F) bitShift:24) + |
|
511 |
((self basicAt:3) bitShift:16) + |
|
512 |
((self basicAt:2) bitShift:8) + |
|
513 |
(self basicAt:1) |
|
514 |
||
515 |
" |
|
516 |
1.2345 hash |
|
517 |
1.2345 asShortFloat hash |
|
518 |
1.0 hash |
|
519 |
1.0 asShortFloat hash |
|
520 |
" |
|
521 |
! |
|
522 |
||
523 |
~= aNumber |
|
524 |
"return true, if the arguments value are not equal" |
|
525 |
||
526 |
%{ /* NOCONTEXT */ |
|
527 |
||
528 |
if (__isSmallInteger(aNumber)) { |
|
529 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
530 |
} |
|
531 |
if (__isFloatLike(aNumber)) { |
|
532 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
533 |
} |
|
534 |
if (__isShortFloat(aNumber)) { |
|
535 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
536 |
} |
|
537 |
%}. |
|
538 |
^ self retry:#~= coercing:aNumber |
|
539 |
||
540 |
! ! |
|
541 |
||
542 |
!ShortFloat methodsFor:'printing & storing'! |
|
543 |
||
544 |
printString |
|
545 |
"return a printed representation of the receiver" |
|
546 |
||
547 |
%{ /* NOCONTEXT */ |
|
548 |
||
549 |
char buffer[64]; |
|
550 |
REGISTER char *cp; |
|
551 |
OBJ s; |
|
552 |
||
553 |
/* |
|
554 |
* actually only needed on sparc: since thisContext is |
|
555 |
* in a global register, which gets destroyed by printf, |
|
556 |
* manually save it here - very stupid ... |
|
557 |
*/ |
|
558 |
__BEGIN_PROTECT_REGISTERS__ |
|
559 |
||
560 |
#ifdef SYSV |
|
561 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
562 |
#else |
|
563 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
564 |
#endif |
|
565 |
||
566 |
__END_PROTECT_REGISTERS__ |
|
567 |
||
568 |
/* |
|
569 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
570 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
571 |
*/ |
|
572 |
for (cp = buffer; *cp; cp++) { |
|
573 |
if ((*cp == '.') || (*cp == 'e')) break; |
|
574 |
} |
|
575 |
if (! *cp) { |
|
576 |
*cp++ = '.'; |
|
577 |
*cp++ = '0'; |
|
578 |
*cp = '\0'; |
|
579 |
} |
|
580 |
||
581 |
s = __MKSTRING(buffer COMMA_SND); |
|
582 |
if (s != nil) { |
|
583 |
RETURN (s); |
|
584 |
} |
|
585 |
%}. |
|
586 |
" |
|
587 |
memory allocation (for the new string) failed. |
|
588 |
When we arrive here, there was no memory, even after a garbage collect. |
|
589 |
This means, that the VM wanted to get some more memory from the |
|
590 |
OS, which was not kind enough to give it. |
|
591 |
Bad luck - you should increase the swap space on your machine. |
|
592 |
" |
|
593 |
^ ObjectMemory allocationFailureSignal raise. |
|
594 |
! ! |
|
595 |
||
596 |
!ShortFloat methodsFor:'testing'! |
|
597 |
||
3147 | 598 |
isFinite |
599 |
"return true, if the receiver is a finite float |
|
600 |
i.e. not NaN and not infinite." |
|
601 |
||
602 |
%{ /* NOCONTEXT */ |
|
603 |
||
604 |
double dV = (double) __shortFloatVal(self); |
|
605 |
||
606 |
if (finite(dV)) { RETURN (true); } |
|
607 |
%}. |
|
608 |
^false |
|
609 |
||
610 |
" |
|
611 |
1.0 asShortFloat isFinite |
|
612 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
613 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
614 |
" |
|
615 |
||
616 |
||
617 |
! |
|
618 |
||
2387 | 619 |
isNaN |
620 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
621 |
These are not created by ST/X float operations (they raise an exception); |
|
622 |
however, inline C-code could produce them ..." |
|
623 |
||
624 |
%{ /* NOCONTEXT */ |
|
625 |
||
3139 | 626 |
double dV = (double)(__shortFloatVal(self)); |
627 |
||
3147 | 628 |
if (isnan(dV)) { RETURN (true); } |
629 |
||
630 |
#if 0 /* Currently all our systems support isnan() |
|
631 |
* If not, you have to fix librun/jinterpret.c also. |
|
632 |
*/ |
|
633 |
||
2395 | 634 |
/* |
635 |
* sigh - every vendor is playing its own game here ... |
|
636 |
* Q: what are standards worth, anyway ? |
|
637 |
*/ |
|
3139 | 638 |
#ifdef IS_NAN |
639 |
if (IS_NAN(dV)) { RETURN (true); } |
|
640 |
RETURN (false); |
|
641 |
#endif |
|
642 |
||
2393 | 643 |
#ifdef IS_QNAN |
3139 | 644 |
if (IS_QNAN(dV)) { RETURN (true); } |
645 |
RETURN (false); |
|
2393 | 646 |
#endif |
2392 | 647 |
|
2393 | 648 |
#ifdef FLT_SNAN |
3139 | 649 |
if (dV == FLT_SNAN) { RETURN (true); } |
650 |
RETURN (false); |
|
2393 | 651 |
#endif |
3139 | 652 |
|
2393 | 653 |
#ifdef FLT_QNAN |
3139 | 654 |
if (dV == FLT_QNAN) { RETURN (true); } |
655 |
RETURN (false); |
|
2393 | 656 |
#endif |
657 |
||
658 |
#ifdef _SNANF |
|
3139 | 659 |
if (dV == _SNAN) { RETURN (true); } |
660 |
RETURN (false); |
|
2393 | 661 |
#endif |
3139 | 662 |
|
2393 | 663 |
#ifdef _QNANF |
3139 | 664 |
if (dV == _QNAN) { RETURN (true); } |
665 |
RETURN (false); |
|
2393 | 666 |
#endif |
2392 | 667 |
|
2395 | 668 |
#ifdef IsPosNAN |
3139 | 669 |
if IsPosNAN(dV) { RETURN (true); } |
670 |
RETURN (false); |
|
2395 | 671 |
#endif |
3139 | 672 |
|
2395 | 673 |
#ifdef IsNegNAN |
3139 | 674 |
if IsNegNAN(dV) { RETURN (true); } |
675 |
RETURN (false); |
|
2395 | 676 |
#endif |
677 |
||
2393 | 678 |
#ifdef NAN |
3139 | 679 |
if (dV == NAN) { RETURN (true); } |
680 |
RETURN (false); |
|
2400 | 681 |
#endif |
2389 | 682 |
|
3139 | 683 |
#ifdef NaN |
684 |
if (NaN(dV)) { RETURN (true); } |
|
685 |
RETURN (false); |
|
686 |
#endif |
|
3147 | 687 |
|
688 |
#endif /* 0 */ |
|
2392 | 689 |
%}. |
690 |
^ false |
|
2387 | 691 |
|
3147 | 692 |
" |
693 |
1.0 asShortFloat isNaN |
|
694 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
695 |
" |
|
2387 | 696 |
! |
697 |
||
1197 | 698 |
negative |
699 |
"return true if the receiver is less than zero" |
|
700 |
||
701 |
%{ /* NOCONTEXT */ |
|
702 |
||
703 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
704 |
%} |
|
705 |
||
706 |
||
707 |
! |
|
708 |
||
709 |
positive |
|
710 |
"return true if the receiver is greater or equal to zero" |
|
711 |
||
712 |
%{ /* NOCONTEXT */ |
|
713 |
||
714 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
715 |
%} |
|
716 |
||
1880 | 717 |
! |
718 |
||
719 |
strictlyPositive |
|
720 |
"return true if the receiver is greater than zero" |
|
721 |
||
722 |
%{ /* NOCONTEXT */ |
|
723 |
||
724 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
725 |
%} |
|
726 |
||
1197 | 727 |
! ! |
728 |
||
1880 | 729 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 730 |
|
731 |
version |
|
3147 | 732 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.26 1998-01-07 12:03:15 stefan Exp $' |
1197 | 733 |
! ! |